Have a good day, today I come with a problem that I have not been able to solve. I am working with C# .Net and a base in Microsoft Access, I am wanting to do an Update to a table.
The problem is that I don't know if I'm executing the use of parameters correctly, besides that I don't know if the query is really being executed.
I have a MessageBox to display that the data was updated, said MessageBox is shown but it does not update the value in the table.
Here is the code for the "Save" button:
try
{
conexion.Open();
string update = "UPDATE usuarios set nombre = @nombre, tipo_usuario = @tipo_usuario, iniciales = @iniciales, identidad = @identidad, direccion = @direccion, telefono = @telefono, celular = @celular, correo = @correo WHERE codigo=@codigo";
OleDbCommand comando7 = new OleDbCommand(update, conexion);
comando7.Parameters.AddWithValue("@codigo", txtId.Text);
comando7.Parameters.AddWithValue("@nombre", txtNombre.Text);
comando7.Parameters.AddWithValue("@tipo_usuario", txtTipo.Text);
comando7.Parameters.AddWithValue("@iniciales", txtInciales.Text);
comando7.Parameters.AddWithValue("@identidad", txtIdentidad.Text);
comando7.Parameters.AddWithValue("@direccion", txtDireccion.Text);
comando7.Parameters.AddWithValue("@telefono", txtTelefono.Text);
comando7.Parameters.AddWithValue("@celular", txtCelular.Text);
comando7.Parameters.AddWithValue("@correo", txtCorreo.Text);
comando7.ExecuteNonQuery();
MessageBox.Show("Usuario actualizado con exito", "Exito", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
conexion.Close();
}
So I would like to know if it is that way or if I have to change something to update the value since it is not updating. If you need any other info I will be pending. Thank you very much.
Hi Louis,
As
página de OleDbCommand.Parameters
stated in the OleDb provider does not support named parameters, in your case it must be used'?'
and the parameters must be added to the parameters collection in exactly the same order as they are in thequery
.Knowing this, your code would look like this:
To check if the
query
has been executed correctly and to display theMessageBox
, I recommend you always use the value returned byExecuteNonQuery
to check the affected lines, as follows: