Search code examples
c#ms-accessoledb

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

Solution

  • 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.