-AAA-
asked on
MS SQL Update command in C#
Ok I have some Text Boxes that the user is filling out that I want to use to update fields on a MS SQL 2008 Server. I have been beating my head searching all over google for a day and am stumped/burnt out. My code is in C# as follows and any help is appreciated!
thanks!
thanks!
string myConnectionString = @"Data Source=sql2;Initial Catalog=bleh;User Id =bleh;Password=bleh";
SqlConnection myConnection = new SqlConnection(myConnectionString);
String myUpdateQuery = @"UPDATE Vendors SET Vendor=@Vendor,
Address=@Address, City=@City, State=@State, Zip=@Zip
WHERE Vendor=@Vendor";
SqlCommand MyCmd = new SqlCommand(myUpdateQuery);
MyCmd.Parameters.Add("@Vendor", SqlDbType.NVarChar, 100, vendorTextBox.Text);
MyCmd.Parameters.Add("@Address", SqlDbType.NVarChar, 100, addyTextBox.Text);
MyCmd.Parameters.Add("@City", SqlDbType.NVarChar, 100, cityTextBox.Text);
MyCmd.Parameters.Add("@State", SqlDbType.NVarChar, 30, stateTextBox.Text);
MyCmd.Parameters.Add("@Zip", SqlDbType.NVarChar, 20, zipTextBox.Text);
MyCmd.Connection = myConnection;
myConnection.Open();
MyCmd.ExecuteNonQuery();
myConnection.Close();
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Kindly pass your parameter as given here.
SqlParameter param = MyCmd.Parameters.Add("@Ven dor", SqlDbType.NVarChar, 100);
param.Value = vendorTextBox.Text;
param = MyCmd.Parameters.Add("@Add ress", SqlDbType.NVarChar, 100);
param.Value = addyTextBox.Text;
param = MyCmd.Parameters.Add("@Cit y", SqlDbType.NVarChar, 100);
param.Value = cityTextBox.Text;
param = MyCmd.Parameters.Add("@Sta te", SqlDbType.NVarChar, 30);
param.Value = stateTextBox.Text;
param = MyCmd.Parameters.Add("@Zip ", SqlDbType.NVarChar, 20);
param.Value = zipTextBox.Text;
I hope it will help.
SqlParameter param = MyCmd.Parameters.Add("@Ven
param.Value = vendorTextBox.Text;
param = MyCmd.Parameters.Add("@Add
param.Value = addyTextBox.Text;
param = MyCmd.Parameters.Add("@Cit
param.Value = cityTextBox.Text;
param = MyCmd.Parameters.Add("@Sta
param.Value = stateTextBox.Text;
param = MyCmd.Parameters.Add("@Zip
param.Value = zipTextBox.Text;
I hope it will help.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
MyCmd.Parameters.AddWithVa lue("@para m", valueOfAnyType);
Using this method leaves the types and sizes out of the mix in case your database fields change.
Also consider putting the command and connection creation (new) in a using statement so that the connection is always closed and the command is cleaned up in case of failure.
Using this method leaves the types and sizes out of the mix in case your database fields change.
Also consider putting the command and connection creation (new) in a using statement so that the connection is always closed and the command is cleaned up in case of failure.
ASKER
jacko72, your code works when doing the update but only when combined devlab2012 updated SQL command:
So this makes it seem like I will be unable to update the actual vendor name. any ideas for that or does it not makes sense to change the value of what i am actually searching by?
String myUpdateQuery = @"UPDATE Vendors SET Address=@Address, City=@City, state=@State, Zip=@Zip WHERE Vendor=@Vendor";
So this makes it seem like I will be unable to update the actual vendor name. any ideas for that or does it not makes sense to change the value of what i am actually searching by?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
yes I should be doing it by a unique key which solves the problem. Thanks for your help
and you can pass the same value to both of these parameters