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.