-2

Here is my function.

private void btnUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            con.Open();
            cmd = new SqlCommand("update myEXPENSES set Detail= '" + txtDetail.Text 
                + "',Price='" + txtPrice.Text + "' 
                where Detail='" + txtDetail.Text + "'", con);
            cmd.ExecuteNonQuery();
            MessageBox.Show("Data has been updated");
            con.Close();
            displayData();
        }
        catch (Exception)
        {

        }
    }
    
halfer
  • 19,824
  • 17
  • 99
  • 186
Kelly N
  • 17
  • 4
    Please use parameters instead of building a string. It might not be the source of the problem, but most importantly it protect you from [Sql Injection](https://bobby-tables.com/) – stuartd May 18 '21 at 21:33
  • 2
    And if your command is throwing an error due to bad SQL or any other reason - you won't know because you have an empty`catch` clause.. – stuartd May 18 '21 at 21:35
  • 2
    Please read these 3 articles: [Is it bad practice to catch System.Exception?](https://stackoverflow.com/questions/426346/is-this-a-bad-practice-to-catch-a-non-specific-exception-such-as-system-exceptio) [Why are empty catch blocks a bad idea?](https://stackoverflow.com/questions/1234343/why-are-empty-catch-blocks-a-bad-idea) [Should I create a brand new SqlConnection each time I want to use it?](https://stackoverflow.com/questions/38440974/should-i-create-a-brand-new-sqlconnection-each-time-i-want-to-use-it-or-just-at) – John Wu May 18 '21 at 21:36
  • 2
    There is probably a problem with your SQL statement as well. Your `Set` and `Where` clauses don't make sense together, since you are setting a field to the same value that you know it already is. – John Wu May 18 '21 at 21:38
  • 1
    Does txtDetail.Text contains the new value for the Detail field? If yes then try to explain to the WHERE clause how to find the record to update – Steve May 18 '21 at 21:39
  • please change your empty catch so that it will show you the exception, then include the error message in your question. you can do this by catching the exception in a variable and displaying it in a message box with Exception.Message: catch (Exception es) {MessageBox.Show(es.Message)} – Joswin John May 23 '21 at 00:45
  • also just fyi for your later projects having an empty catch field renders the whole try-catch statement useless since the whole point for it is to catch the exception without crashing the application. having an empty catch block makes it so when there is an error you wont know, which is worse than having no try-catch statement at all. – Joswin John May 23 '21 at 00:55

1 Answers1

0

There is probably a problem with your SQL statement as well. Your Set and Where clauses don't make sense together, since you are setting a field to the same value that you know it already is. – John Wu

Like John Wu said here your SQL Set and Where clauses are colliding.

What your doing right now with your Set and Where clause is first looking for a row with a Detail Value that matches what's in the txtDetail Text Box then your changing the Detail Value to what is in the txtDetail Text Box so the end result is that you didn't actually change anything. For example if I put the string "good" in the textbox then it will look for a row with the Detail value "good" then try and change the value to "good"

don't use the same value for your SET and WHERE clauses, if you use another text box for what you want the value to be changed to, it could be much easier.

            try
        {

            using (SqlConnection con = new SqlConnection())
            {
                con.ConnectionString = ini;
                con.Open();

                SqlCommand cmd = new SqlCommand("update myEXPENSES set Detail= '" + txtChange.Text + "', Price='" + txtPrice.Text + "' where Detail='" + txtDetail.Text + "'", con); 
                cmd.ExecuteNonQuery();
                MessageBox.Show("Data has been updated");
            }
        }
        catch (Exception es)
        {
            MessageBox.Show(es.Message);
        }

the txtChange Value would be another text box that you could add where a user could add what value they would like to change the txtDetail value to, while txtDetail is used to find what to change

also make sure to never leave your catch block empty. a try-catch statement is worse for you than it is good if you leave the catch block empty, since if an exception is thrown you won't know and it will leave with more question on what's wrong.