0

Hi everyone,

Its weird. Only UPDATE sql statement cannot process and others like INSERT, select can work.. i used UPDATE sql statement in c# but the record that i wanted to update isnt update at all.. here my code.. i am using ms access

OleDbConnection connDBString = objDB.GetDatabaseConnection;
connDBString.Open();
string status = "processing";
string sql = "UPDATE Orders SET Status=@status WHERE OrderID=@id";
OleDbCommand cmd = new OleDbCommand(sql, connDBString);
cmd.Parameters.AddWithValue("@id", "1");
cmd.Parameters.AddWithValue("@status", status);
           
int result = cmd.ExecuteNonQuery();
connDBString.Close();
3
Contributors
5
Replies
6
Views
5 Years
Discussion Span
Last Post by Mitja Bonca
0

OleDb doesn't use @ parameters in the SQL statement, you need to replace them with ? so your statement will look like

string sql = "UPDATE Orders SET Status=? WHERE OrderID=?";

You can add the parameters just like you have them now, you just have to remember to add them in the order you want them to replace the ?

0

Exactly like you have will work fine if you reverse the order. The system actually ignores the name you place in there and just does them in order. The first parameter you add goes to the first ?, the second the second, etc.

0

Thanks for the guidance. I have managed to solve it by doing it in this way =)

string sql = "UPDATE Orders SET Status='" + status + "' WHERE OrderID=1";

0

This is a "bad" way of programming. Do as Momerath told you to.

so:

string sql = "UPDATE Orders SET Status= ? WHERE OrderID = ?";
OleDbCommand cmd = new OleDbCommand(sql, connDBString);
cmd.Parameters.AddWithValue("@id", "1"); //NOTE: is OrderID type of varchar? if its integer, pass an integer, not a string! It would be better!!
cmd.Parameters.AddWithValue("@status", status);
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.