Click here to Skip to main content
16,021,211 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi. i am trying to execute an update statement in asp.net webform and ms-access db but it says syntax error in update statment. thanks for help. below is code:
        OleDbConnection connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|DB.accdb;");
          connection.Open();
int kid = MYVALUE;
int no = MYVALUE;
string fid= MYVALUE;
            cmd = new OleDbCommand("update mytable set no=@no where FID=@fid and KID=@kid",connection);
            cmd.Parameters.AddWithValue("@no", no);
            cmd.Parameters.AddWithValue("@fid", fid);
            cmd.Parameters.AddWithValue("@kid", kid);
            cmd.ExecuteNonQuery();
            connection.Close();


What I have tried:

everything seams ok but it show syntax error for update.
Posted
Comments
0x01AA 6-Aug-24 3:05am    
And the exact error message is?
[no name] 6-Aug-24 3:52am    
You are setting all three parameters to the same value; is that what you meant to do?

"NO" is a keyword in Access: List of reserved words in Access - Microsoft 365 Apps | Microsoft Learn[^] - it's used for "not equal to" apparently, though I've never heard of it.

Wrap it in square brackets and the problem should go away:
cmd = new OleDbCommand("update mytable set [no]=@no where FID=@fid and KID=@kid",connection);
 
Share this answer
 
Comments
majid saidi 18-Aug-24 12:50pm    
Thanks. It works.
OriginalGriff 19-Aug-24 0:42am    
You're welcome!
'OleDb' does not support named parameters, it requires positional parameters. See MS Learn | OleDbCommand.Parameters Property[^].

Your SQL query should use '?' instead of named parameters like '@no', '@fid', and '@kid'. Your code will then look like -

NET
using System.Data.OleDb;

OleDbConnection connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|DB.accdb;");
connection.Open();

int kid = MYVALUE;
int no = MYVALUE;
string fid = MYVALUE;

string query = "UPDATE mytable SET no = ? WHERE FID = ? AND KID = ?";
using (OleDbCommand cmd = new OleDbCommand(query, connection))
{
    //Add ALL parameters in the order they appear in your query...
    cmd.Parameters.AddWithValue("?", no);
    cmd.Parameters.AddWithValue("?", fid);
    cmd.Parameters.AddWithValue("?", kid);

    cmd.ExecuteNonQuery();
}

connection.Close();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900