|
-
Feb 13th, 2007, 03:23 PM
#1
Thread Starter
Lively Member
[RESOLVED] Update Statement
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.
Last edited by Hack; Feb 14th, 2007 at 09:13 AM.
Reason: Added [RESOLVED] to thread title and green "resolved" checkmark
-
Feb 13th, 2007, 05:35 PM
#2
Hyperactive Member
Re: Update Statement
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.
Currently Using: VS 2005 Professional
-
Feb 13th, 2007, 05:35 PM
#3
Re: Update Statement
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.
-
Feb 13th, 2007, 05:43 PM
#4
Thread Starter
Lively Member
Re: Update Statement
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
-
Feb 13th, 2007, 05:44 PM
#5
Thread Starter
Lively Member
Re: Update Statement
I also changed the statement to include wild card characters in the where clause and still no action.
thanks,
notrosh
-
Feb 13th, 2007, 07:14 PM
#6
Re: Update Statement
How exactly are you determining that no data was changed?
-
Feb 13th, 2007, 08:15 PM
#7
Thread Starter
Lively Member
Re: Update Statement
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|