Search code examples
c#sqlms-accessoledb

OleDbCommand throw exception on valid command text


I have OleDbConnection to connect to access database. But when I execute a query, OleDbCommand throw exception saying that command is invalid. I am perfectly sure that command is valid.

 string ret = "";

string command = "UPDATE tblUser set username = ?, password = ? where ID = 1";
dbConn.Open();

using (var dbCommand = dbConn.CreateCommand())
{
    dbCommand.Connection = dbConn;
    dbCommand.CommandText = command;
    dbCommand.Parameters.AddWithValue("?", username);
    dbCommand.Parameters.AddWithValue("?", password);

    int temp = dbCommand.ExecuteNonQuery();
    if (temp > 0)
        ret = "sukses";
    dbConn.Close();                
}
return ret;

However when code above is executed, it throw exception saying Syntax error in UPDATE statement. at dbCommand.ExecuteNonQuery(). How to deal with this problem?


Solution

  • you need to escape PASSWORD with brackets since it is a reserved keyword,

    UPDATE tblUser set username = ?, [password] = ? where ID = 1