1

I'm trying to modify an specific register on Access by C# but when I run the code, It doesn't modify anything, I have tried the same structure on adding resgisters or deleting and it works well, Help please

using OleDB

dataGridView1.Rows.Clear();
                
                string sql = "UPDATE Medicos SET Id_clinica=@Id_clinica, Id_especialidad=@Id_especialidad, Nombre=@Nombre, Apellidos=@Apellidos WHERE Id_medico=@Id_medico";
                OleDbConnection con = new OleDbConnection(cs);
                con.Open();
                OleDbCommand cmd = new OleDbCommand(sql, con);
                cmd.Parameters.AddWithValue("@Id_medico", textBox1.Text);
                cmd.Parameters.AddWithValue("@Id_clinica", textBox2.Text);
                cmd.Parameters.AddWithValue("@Id_especialidad", textBox3.Text);
                cmd.Parameters.AddWithValue("@Nombre", textBox4.Text);
                cmd.Parameters.AddWithValue("@Apellidos", textBox5.Text);
                cmd.ExecuteNonQuery();
                con.Close();
  • 1
    OleDb parameters are positional. You should add them to the parameters collection in the same order in which they appear in the query text. Move the parameter for the WHERE condition in the last position – Steve Nov 11 '22 at 19:41
  • Also do not use AddWithValue. This method treats the parameter's type as the input value. I doubt that all those ids are strings. Things go very bad with dates and decimals. Instead use the Add method specifying the parameter type – Steve Nov 11 '22 at 19:42
  • The following may be helpful: https://stackoverflow.com/a/69638011/10024425 – Tu deschizi eu inchid Nov 11 '22 at 20:46
  • Thanks!! It was the position on my parameters – César Merodio Nov 11 '22 at 22:44

0 Answers0