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();

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 ?

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.

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";

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 article has been dead for over six months. Start a new discussion instead.