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

Recommended Answers

All 5 Replies

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 ?

Oh ok... then how do i do the cmd.Parameter.AddWithValues();

Please guide

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);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.