Click to See Complete Forum and Search --> : [RESOLVED] Update Statement
zdavis
Feb 13th, 2007, 02:23 PM
hi everyone. I am trying to execute an update statement. I have tried what seems like 20 different ways and nothing seems to be working. Can someone please tell me what i am doing wrong.
int Employeenum = int.Parse(this.txtEmployeeNum.Text);
string BranchName = this.cboBranch.Text;
string sql = "update branch set num_employees = " + Employeenum + " where branch_name = '" + BranchName + "'";
SqlCommand cmd = new SqlCommand(sql, conn);
try
{
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
After that happens i get no update on the table and no errors.
:(
tacoman667
Feb 13th, 2007, 04:35 PM
Make sure that you even have a match in that WHERE statement in the table. It is possible you aren't hitting true on that conditional at all.
jmcilhinney
Feb 13th, 2007, 04:35 PM
ExecuteNonQuery is a function that returns an integer containing the number of records affected by the operation. What does ExecuteNonQuery return? If it's zero then there are no matching records. If it's greater than zero then the operation is succeeding and that number of records were updated.
zdavis
Feb 13th, 2007, 04:43 PM
Thanks everyone,
I have pulled the sql statement from my command and ran it directly against the database. It updates and completes successfully with no problems.
I also tried the following
int x = cmd.ExecuteNonQuery();
I stepped through and noticed that one row was changed. However, the transaction was never committed to the database. I am confused right now.
any help would be greatly appreciated
thanks,
notrosh
zdavis
Feb 13th, 2007, 04:44 PM
I also changed the statement to include wild card characters in the where clause and still no action.
thanks,
notrosh
jmcilhinney
Feb 13th, 2007, 06:14 PM
How exactly are you determining that no data was changed?
zdavis
Feb 13th, 2007, 07:15 PM
i have actually fixed the issue. it was very weird. I changed the connection string to a general connection string. Where before i was pulling the connection string form the app.config file. Seems to work fine now. However, to answer the previous question. I knew the data wasn't changing because of two reasons. One i manually checked the database for changes and two when i restarted the application it showed no changes.
thanks for all the help everyone
notrosh
:)
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.