How to know if the query is running correctly?

0

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 do not know if I am correctly executing the use of parameters, besides I do not know if the query is actually running.

I have a MesssageBox to display that the data was updated, said MessageBox is displayed 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();
        }

Then 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 they occupy any other info I will be pending. Thank you very much.

    
asked by Luis Fernando 11.01.2018 в 04:29
source

1 answer

2

Good Luís,

As per página de OleDbCommand.Parameters The OleDb provider does not support named parameters, in which case '?' must be used and the parameters must be added to the parameter collection in exactly the same order as in query .

Knowing this, your code would look like this:

    try
    {
        conexion.Open();
        string update = "UPDATE usuarios set nombre = ?, tipo_usuario = ?, iniciales = ?, identidad = ?, direccion = ?, telefono = ?, celular = ?, correo = ? WHERE codigo = ?";
        OleDbCommand comando7 = new OleDbCommand(update, conexion);
        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.Parameters.AddWithValue("@codigo", txtId.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();
    }

To check if the query has been executed correctly and to display the MessageBox , I recommend you always use the value returned by ExecuteNonQuery to check the affected lines, in the following way:

if (comando7.ExecuteNonQuery() != 0)
    MessageBox.Show("Usuario actualizado con exito", "Exito", MessageBoxButtons.OK, MessageBoxIcon.Information);
    
answered by 11.01.2018 / 08:33
source