user3043233
user3043233

Reputation:

updating a record in Access table

I'm using following query to update a record in an Access table, but I'm getting a syntax error?

How can I do this?

 string str = string.Format("UPDATE [MIX] SET[Stock quantity],[Retail price],[Original price])Values(?,?,?,?) WHERE [Brand name]=@brandname");
 OleDbCommand comd = new OleDbCommand(str, conn);
 comd.Parameters.AddWithValue("@brandname", comboBox3.Text);
 comd.Parameters.AddWithValue("Stock quantity", comboBox1.Text);
 comd.Parameters.AddWithValue("Retail price", comboBox4.Text);
 comd.Parameters.AddWithValue("Original price", comboBox5.Text);
 comd.ExecuteNonQuery();

Upvotes: 1

Views: 83

Answers (2)

Ahmed ilyas
Ahmed ilyas

Reputation: 5832

this is not what an update statement looks like. your statement seems like an INSERT statement. take a look here on how an UPDATE statement differs:

http://msdn.microsoft.com/en-us/library/office/bb221186(v=office.12).aspx

typical statement:

UPDATE [TableName]
SET [FieldName] = @someValueParam
WHERE [SomeFieldName] = [someValue]

remember to parameterise your queries also.

Upvotes: 2

Gord Thompson
Gord Thompson

Reputation: 123849

What you need is something more like this:

string str = 
        "UPDATE [MIX] SET " + 
        "[Stock quantity] = ?, " +
        "[Retail price] = ?, " +
        "[Original price] = ? " +
        "WHERE [Brand name] = ?";
OleDbCommand comd = new OleDbCommand(str, conn);
comd.Parameters.AddWithValue("?", comboBox1.Text);  // [Stock quantity]
comd.Parameters.AddWithValue("?", comboBox4.Text);  // [Retail price]
comd.Parameters.AddWithValue("?", comboBox5.Text);  // [Original price]
comd.Parameters.AddWithValue("?", comboBox3.Text);  // [Brand name]
comd.ExecuteNonQuery();

Note that the parameters are defined in the exact same order that they appear in the CommandText.

Upvotes: 3

Related Questions