Search code examples
c#oledb

How to update an entire selected same column?


I want to update an entire selected same column with another value...

heres a code i have tried, apparently its not working (the error is no changes in DB)

 OleDbConnection con = new OleDbConnection();
        con.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Rock.accdb";
        OleDbCommand cmd = new OleDbCommand();
        cmd.Connection = con;
        con.Open();
        cmd.CommandText = "UPDATE profile SET [year]=@1 WHERE [year]=@2";
        cmd.Parameters.AddWithValue("@1", comboBox1.Text);
        cmd.Parameters.AddWithValue("@2", comboBox2.Text);
        cmd.ExecuteNonQuery();
        con.Close();
        label4.Text = "Updated successfully";
        label4.ForeColor = Color.Green;

Please be respectful, if you guys have any doubts just comment...


Solution

  • try:

    cmd.CommandText = "UPDATE profile SET [year]=? WHERE [year]=?"
    

    and update parameters (the parameters must be added in order of the '?'s in the query)

    cmd.Parameters.AddWithValue("?", comboBox1.Text);
    cmd.Parameters.AddWithValue("?", comboBox2.Text);