0

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

  • I guess your `connection string` is not proper. Check it in the config file and it seems that your `UPDATE` query is not proper – Nad Mar 07 '15 at 10:52
  • You should probably tag your question "ole", and remove the Visual Studio tag - your problem presumably has nothing to do with Visual Studio. – RenniePet Mar 07 '15 at 12:45
  • ok i will do that... but @NadeemKhan you got me wrong... everything is perfect... except the query... if you guys got any idea regarding that then it would be appreciable... – Project Team Mar 07 '15 at 15:13
  • This question is similar and may contain the answer you are looking for: http://stackoverflow.com/questions/1216271/whats-wrong-with-these-parameters "The OLE DB .NET Provider does not support named parameters for passing parameters to an SQL statement or a stored procedure called by an OleDbCommand when CommandType is set to Text. In this case, the question mark (?) placeholder must be used." – Kevin Nacios Mar 07 '15 at 16:56
  • i am not specifying anyone here, but do you guys understand my question? i want to update a column with the same name itself... so is there any alternatives for this method? – Project Team Mar 08 '15 at 12:32

1 Answers1

0

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);
Kevin Nacios
  • 2,843
  • 19
  • 31