C# SQL Web Application - My Web App will not perform necessary update/deletes when method is called











up vote
0
down vote

favorite












I am literally stumped. I've spent two days working at this. I have tried scrolling through the internet but nothing I seem to try worked like try/catch blocks, using etc.



So here goes..



I have a Web Application asp c# , which is connected to an sql database that I have written within Visual Studio 'Database.mdf' .



The Database contains a number of Football Players and their details, I want to be able to update and delete these details however my sql commands are not coming into affect (I am new to this so it's probably something ridiculously simple to most of you but nonetheless)



I have a number of buttons in which I use to both sort the data on screen in order. They work fine, I just use a static variable string for that. However the methods I have written for the SQL Commands are not working for me. The methods are called also on Button_Click but in the case of Update/Delete nothing changes, it's as if the sql query never updated the database



Please if you have any idea how I can fix this let me know. My head's fried.



Here are a few of the methods including display data :



   protected void Page_Load(object sender, EventArgs e)
{
string str = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename="C:\Users\David\Desktop\WebApplication5\WebApplication5\App_Data\Database2.mdf";Integrated Security=True";
cn = new SqlConnection(str);
cn.Open();
mycount();
displayData();
// updateData();
// deleteData();


}



protected void mycount()
{ // count no of els in table
max = 0;
var cmd = cn.CreateCommand();


cmd.CommandText = sqlQuery;
var reader = cmd.ExecuteReader();
while (reader.Read()) max++;
reader.Close();
}

protected void displayData()
{


var cmd = cn.CreateCommand();


cmd.CommandText = sqlQuery;
var reader = cmd.ExecuteReader();
for (int i = 0; i < count; i++) reader.Read();
TextBox1.Text = "" + reader[0];
TextBox2.Text = "" + reader[1];
TextBox5.Text = "" + reader[2];
TextBox6.Text = "" + reader[3];
TextBox7.Text = "" + reader[4];
TextBox8.Text = "" + reader[5];
reader.Close();


}

protected void updateData()
{
var cmd = cn.CreateCommand();
string query = "UPDATE [Footballer] SET [Appearances] = @appear , [NumberOfGoals] = @goals Where [PlayerName] = @name ";
cmd.CommandText = query;

int appear = int.Parse(TextBox6.Text);
int goals = int.Parse(TextBox8.Text);
string name = TextBox1.Text;


cmd.Parameters.AddWithValue("@Player_ID", name);
cmd.Parameters.AddWithValue("@app", appear);
cmd.Parameters.AddWithValue("@goals", goals);


cmd.ExecuteNonQuery();

}


protected void deleteData()
{
string searchName = TextBox4.Text;
TextBox1.Text = "Deleted";
TextBox2.Text = "Deleted";
TextBox5.Text = "Deleted";
TextBox6.Text = "Deleted";
TextBox7.Text = "Deleted";
TextBox8.Text = "Deleted";
var cmd = cn.CreateCommand();
string query = "Delete from [Footballer] where [PlayerName] = @PlayerName_ID";

cmd.CommandText = query;
cmd.Parameters.AddWithValue("@PlayerName_ID", searchName);

cmd.ExecuteNonQuery();

}


Here is a screen of what I got visually if it helps:



Gui View










share|improve this question




















  • 1




    i see the tag mysql but don't see mysql mentioned anywhere in the code - am i missing something?
    – JohnB
    Nov 20 at 0:06










  • did you get any error messages anywhere, did you try to debug the issue to make sure that it actually does the intended thing.
    – JohnB
    Nov 20 at 0:07










  • @JohnB you're right, error on my part. Not thinking straight. There aren't any errors or warnings. As far as debugging all I've done is pass the same commands to the database using add query. I'll try the debugger now.
    – David Dunleavy
    Nov 20 at 0:20















up vote
0
down vote

favorite












I am literally stumped. I've spent two days working at this. I have tried scrolling through the internet but nothing I seem to try worked like try/catch blocks, using etc.



So here goes..



I have a Web Application asp c# , which is connected to an sql database that I have written within Visual Studio 'Database.mdf' .



The Database contains a number of Football Players and their details, I want to be able to update and delete these details however my sql commands are not coming into affect (I am new to this so it's probably something ridiculously simple to most of you but nonetheless)



I have a number of buttons in which I use to both sort the data on screen in order. They work fine, I just use a static variable string for that. However the methods I have written for the SQL Commands are not working for me. The methods are called also on Button_Click but in the case of Update/Delete nothing changes, it's as if the sql query never updated the database



Please if you have any idea how I can fix this let me know. My head's fried.



Here are a few of the methods including display data :



   protected void Page_Load(object sender, EventArgs e)
{
string str = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename="C:\Users\David\Desktop\WebApplication5\WebApplication5\App_Data\Database2.mdf";Integrated Security=True";
cn = new SqlConnection(str);
cn.Open();
mycount();
displayData();
// updateData();
// deleteData();


}



protected void mycount()
{ // count no of els in table
max = 0;
var cmd = cn.CreateCommand();


cmd.CommandText = sqlQuery;
var reader = cmd.ExecuteReader();
while (reader.Read()) max++;
reader.Close();
}

protected void displayData()
{


var cmd = cn.CreateCommand();


cmd.CommandText = sqlQuery;
var reader = cmd.ExecuteReader();
for (int i = 0; i < count; i++) reader.Read();
TextBox1.Text = "" + reader[0];
TextBox2.Text = "" + reader[1];
TextBox5.Text = "" + reader[2];
TextBox6.Text = "" + reader[3];
TextBox7.Text = "" + reader[4];
TextBox8.Text = "" + reader[5];
reader.Close();


}

protected void updateData()
{
var cmd = cn.CreateCommand();
string query = "UPDATE [Footballer] SET [Appearances] = @appear , [NumberOfGoals] = @goals Where [PlayerName] = @name ";
cmd.CommandText = query;

int appear = int.Parse(TextBox6.Text);
int goals = int.Parse(TextBox8.Text);
string name = TextBox1.Text;


cmd.Parameters.AddWithValue("@Player_ID", name);
cmd.Parameters.AddWithValue("@app", appear);
cmd.Parameters.AddWithValue("@goals", goals);


cmd.ExecuteNonQuery();

}


protected void deleteData()
{
string searchName = TextBox4.Text;
TextBox1.Text = "Deleted";
TextBox2.Text = "Deleted";
TextBox5.Text = "Deleted";
TextBox6.Text = "Deleted";
TextBox7.Text = "Deleted";
TextBox8.Text = "Deleted";
var cmd = cn.CreateCommand();
string query = "Delete from [Footballer] where [PlayerName] = @PlayerName_ID";

cmd.CommandText = query;
cmd.Parameters.AddWithValue("@PlayerName_ID", searchName);

cmd.ExecuteNonQuery();

}


Here is a screen of what I got visually if it helps:



Gui View










share|improve this question




















  • 1




    i see the tag mysql but don't see mysql mentioned anywhere in the code - am i missing something?
    – JohnB
    Nov 20 at 0:06










  • did you get any error messages anywhere, did you try to debug the issue to make sure that it actually does the intended thing.
    – JohnB
    Nov 20 at 0:07










  • @JohnB you're right, error on my part. Not thinking straight. There aren't any errors or warnings. As far as debugging all I've done is pass the same commands to the database using add query. I'll try the debugger now.
    – David Dunleavy
    Nov 20 at 0:20













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am literally stumped. I've spent two days working at this. I have tried scrolling through the internet but nothing I seem to try worked like try/catch blocks, using etc.



So here goes..



I have a Web Application asp c# , which is connected to an sql database that I have written within Visual Studio 'Database.mdf' .



The Database contains a number of Football Players and their details, I want to be able to update and delete these details however my sql commands are not coming into affect (I am new to this so it's probably something ridiculously simple to most of you but nonetheless)



I have a number of buttons in which I use to both sort the data on screen in order. They work fine, I just use a static variable string for that. However the methods I have written for the SQL Commands are not working for me. The methods are called also on Button_Click but in the case of Update/Delete nothing changes, it's as if the sql query never updated the database



Please if you have any idea how I can fix this let me know. My head's fried.



Here are a few of the methods including display data :



   protected void Page_Load(object sender, EventArgs e)
{
string str = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename="C:\Users\David\Desktop\WebApplication5\WebApplication5\App_Data\Database2.mdf";Integrated Security=True";
cn = new SqlConnection(str);
cn.Open();
mycount();
displayData();
// updateData();
// deleteData();


}



protected void mycount()
{ // count no of els in table
max = 0;
var cmd = cn.CreateCommand();


cmd.CommandText = sqlQuery;
var reader = cmd.ExecuteReader();
while (reader.Read()) max++;
reader.Close();
}

protected void displayData()
{


var cmd = cn.CreateCommand();


cmd.CommandText = sqlQuery;
var reader = cmd.ExecuteReader();
for (int i = 0; i < count; i++) reader.Read();
TextBox1.Text = "" + reader[0];
TextBox2.Text = "" + reader[1];
TextBox5.Text = "" + reader[2];
TextBox6.Text = "" + reader[3];
TextBox7.Text = "" + reader[4];
TextBox8.Text = "" + reader[5];
reader.Close();


}

protected void updateData()
{
var cmd = cn.CreateCommand();
string query = "UPDATE [Footballer] SET [Appearances] = @appear , [NumberOfGoals] = @goals Where [PlayerName] = @name ";
cmd.CommandText = query;

int appear = int.Parse(TextBox6.Text);
int goals = int.Parse(TextBox8.Text);
string name = TextBox1.Text;


cmd.Parameters.AddWithValue("@Player_ID", name);
cmd.Parameters.AddWithValue("@app", appear);
cmd.Parameters.AddWithValue("@goals", goals);


cmd.ExecuteNonQuery();

}


protected void deleteData()
{
string searchName = TextBox4.Text;
TextBox1.Text = "Deleted";
TextBox2.Text = "Deleted";
TextBox5.Text = "Deleted";
TextBox6.Text = "Deleted";
TextBox7.Text = "Deleted";
TextBox8.Text = "Deleted";
var cmd = cn.CreateCommand();
string query = "Delete from [Footballer] where [PlayerName] = @PlayerName_ID";

cmd.CommandText = query;
cmd.Parameters.AddWithValue("@PlayerName_ID", searchName);

cmd.ExecuteNonQuery();

}


Here is a screen of what I got visually if it helps:



Gui View










share|improve this question















I am literally stumped. I've spent two days working at this. I have tried scrolling through the internet but nothing I seem to try worked like try/catch blocks, using etc.



So here goes..



I have a Web Application asp c# , which is connected to an sql database that I have written within Visual Studio 'Database.mdf' .



The Database contains a number of Football Players and their details, I want to be able to update and delete these details however my sql commands are not coming into affect (I am new to this so it's probably something ridiculously simple to most of you but nonetheless)



I have a number of buttons in which I use to both sort the data on screen in order. They work fine, I just use a static variable string for that. However the methods I have written for the SQL Commands are not working for me. The methods are called also on Button_Click but in the case of Update/Delete nothing changes, it's as if the sql query never updated the database



Please if you have any idea how I can fix this let me know. My head's fried.



Here are a few of the methods including display data :



   protected void Page_Load(object sender, EventArgs e)
{
string str = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename="C:\Users\David\Desktop\WebApplication5\WebApplication5\App_Data\Database2.mdf";Integrated Security=True";
cn = new SqlConnection(str);
cn.Open();
mycount();
displayData();
// updateData();
// deleteData();


}



protected void mycount()
{ // count no of els in table
max = 0;
var cmd = cn.CreateCommand();


cmd.CommandText = sqlQuery;
var reader = cmd.ExecuteReader();
while (reader.Read()) max++;
reader.Close();
}

protected void displayData()
{


var cmd = cn.CreateCommand();


cmd.CommandText = sqlQuery;
var reader = cmd.ExecuteReader();
for (int i = 0; i < count; i++) reader.Read();
TextBox1.Text = "" + reader[0];
TextBox2.Text = "" + reader[1];
TextBox5.Text = "" + reader[2];
TextBox6.Text = "" + reader[3];
TextBox7.Text = "" + reader[4];
TextBox8.Text = "" + reader[5];
reader.Close();


}

protected void updateData()
{
var cmd = cn.CreateCommand();
string query = "UPDATE [Footballer] SET [Appearances] = @appear , [NumberOfGoals] = @goals Where [PlayerName] = @name ";
cmd.CommandText = query;

int appear = int.Parse(TextBox6.Text);
int goals = int.Parse(TextBox8.Text);
string name = TextBox1.Text;


cmd.Parameters.AddWithValue("@Player_ID", name);
cmd.Parameters.AddWithValue("@app", appear);
cmd.Parameters.AddWithValue("@goals", goals);


cmd.ExecuteNonQuery();

}


protected void deleteData()
{
string searchName = TextBox4.Text;
TextBox1.Text = "Deleted";
TextBox2.Text = "Deleted";
TextBox5.Text = "Deleted";
TextBox6.Text = "Deleted";
TextBox7.Text = "Deleted";
TextBox8.Text = "Deleted";
var cmd = cn.CreateCommand();
string query = "Delete from [Footballer] where [PlayerName] = @PlayerName_ID";

cmd.CommandText = query;
cmd.Parameters.AddWithValue("@PlayerName_ID", searchName);

cmd.ExecuteNonQuery();

}


Here is a screen of what I got visually if it helps:



Gui View







c# sql asp.net






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 0:18

























asked Nov 20 at 0:02









David Dunleavy

12




12








  • 1




    i see the tag mysql but don't see mysql mentioned anywhere in the code - am i missing something?
    – JohnB
    Nov 20 at 0:06










  • did you get any error messages anywhere, did you try to debug the issue to make sure that it actually does the intended thing.
    – JohnB
    Nov 20 at 0:07










  • @JohnB you're right, error on my part. Not thinking straight. There aren't any errors or warnings. As far as debugging all I've done is pass the same commands to the database using add query. I'll try the debugger now.
    – David Dunleavy
    Nov 20 at 0:20














  • 1




    i see the tag mysql but don't see mysql mentioned anywhere in the code - am i missing something?
    – JohnB
    Nov 20 at 0:06










  • did you get any error messages anywhere, did you try to debug the issue to make sure that it actually does the intended thing.
    – JohnB
    Nov 20 at 0:07










  • @JohnB you're right, error on my part. Not thinking straight. There aren't any errors or warnings. As far as debugging all I've done is pass the same commands to the database using add query. I'll try the debugger now.
    – David Dunleavy
    Nov 20 at 0:20








1




1




i see the tag mysql but don't see mysql mentioned anywhere in the code - am i missing something?
– JohnB
Nov 20 at 0:06




i see the tag mysql but don't see mysql mentioned anywhere in the code - am i missing something?
– JohnB
Nov 20 at 0:06












did you get any error messages anywhere, did you try to debug the issue to make sure that it actually does the intended thing.
– JohnB
Nov 20 at 0:07




did you get any error messages anywhere, did you try to debug the issue to make sure that it actually does the intended thing.
– JohnB
Nov 20 at 0:07












@JohnB you're right, error on my part. Not thinking straight. There aren't any errors or warnings. As far as debugging all I've done is pass the same commands to the database using add query. I'll try the debugger now.
– David Dunleavy
Nov 20 at 0:20




@JohnB you're right, error on my part. Not thinking straight. There aren't any errors or warnings. As far as debugging all I've done is pass the same commands to the database using add query. I'll try the debugger now.
– David Dunleavy
Nov 20 at 0:20












1 Answer
1






active

oldest

votes

















up vote
0
down vote













Most probably because you do not commit the transaction. There is an example here https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqltransaction.commit






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384369%2fc-sharp-sql-web-application-my-web-app-will-not-perform-necessary-update-delet%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    Most probably because you do not commit the transaction. There is an example here https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqltransaction.commit






    share|improve this answer

























      up vote
      0
      down vote













      Most probably because you do not commit the transaction. There is an example here https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqltransaction.commit






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        Most probably because you do not commit the transaction. There is an example here https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqltransaction.commit






        share|improve this answer












        Most probably because you do not commit the transaction. There is an example here https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqltransaction.commit







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 at 0:50









        burkay

        321212




        321212






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384369%2fc-sharp-sql-web-application-my-web-app-will-not-perform-necessary-update-delet%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Wiesbaden

            Marschland

            Dieringhausen