Activate elements of a DATAGRIDVIEW in C # using a Database and a stored procedure

1

This is my problem, I have created a database which has 2 tables, the information of both tables shows it in a DATAGRIDVIEW that believes in visual studio, all right up there, now what I want to do is update the records I have in my DATAGRIDVIEW, create a stored procedure:

DROP PROCEDURE IF EXISTS sp_ActualizarContacto; CREATE PROCEDURE
sp_ActualizarContacto(

 IN p_nombre VARCHAR (500), IN p_telefono INT (20), IN p_email VARCHAR
(500), IN p_tipocontacto VARCHAR (500))

BEGIN
> 
UPDATE contacto SET nombre = p_nombre, telefono = p_telefono, email =
p_email, tipoContacto = p_tipocontacto WHERE p_id = id_contacto;

END

Well, with that procedure I call it from visualstudio on a button that thinks it's called updating, this is the code:

private void btnActualizar_Click(object sender, EventArgs e)
    {
        string query_actualizar = "CALL sp_ActualizarContacto";

        try
        {
            MySqlCommand cmd = new MySqlCommand(query_actualizar, conectar);
            conectar.Open();

            cmd.ExecuteNonQuery();

            MessageBox.Show("El contacto seleccionado se ha modificado exitosamente");
            mostrarContactos();
        }
        catch (MySqlException ex)
        {
            MessageBox.Show(ex.Message + "" + ex.Number);
        }
        finally
        {
            conectar.Close();
        }

    }

But it does not update anything in the GRIDVIEW and it throws me the sig, error:

INCORRECT NUMBER OF ARGUMENTS FOR PROCEDURE
CONTACTOS.SP_ACTUALIZARCONTACTO; EXPECTED 4 GOT 0 1318
    
asked by Emanuel Mejia 25.10.2017 в 01:18
source

3 answers

0

I have a question about whether you need to use CALL to call the SP or not. At least in SQL server and Oracle you do not need it.

Beyond that, you must pass the parameters using:

MySqlCommand cmd = new MySqlCommand(query_actualizar, conectar);
cmd .Parameters.AddWithValue("p_nombre", variablenombre);
....
conectar.Open();
cmd.ExecuteNonQuery()
MessageBox.Show("El contacto seleccionado se ha modificado exitosamente");
mostrarContactos();
    
answered by 25.10.2017 в 01:50
0

Try this, update and sort the data in the database downwards:

private void ActualizarDatos()
    {

        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = "Data Source=(LocalDB)\MSSQLLocalDB; AttachDbFilename=|DataDirectory|\Database1.mdf; Integrated Security=True"; //Aqui pones tus parámetros
        conn.Open();
        DataTable dt = new DataTable();
        string consulta = "select * from MiSensualTabla ORDER BY ID DESC ";
        SqlCommand cmd = new SqlCommand(consulta, conn);
        SqlDataAdapter adaptador = new SqlDataAdapter(cmd);
        adaptador.Fill(dt);

        MiAmorosoDataGrid.DataSource = dt;

        conn.Close();

        MiAmorosoDataGrid.Update();
        MiAmorosoDataGrid.Refresh();

    }
    
answered by 25.10.2017 в 01:54
0

To execute a stored procedure, it is necessary to indicate it to the command using the CommandType property, that may be your problem:

MySqlCommand cmd  = new MySqlCommand("sp_ActualizarContacto", conn);

// debe poner como tipo del comando StoredProcedure para que se ejecute correctamente
cmd.CommandType = CommandType.StoredProcedure;
//Añades los parámetros necesarios
cmd.Parameters.AddWithValue("p_nombre", txtNombre.Text); 
cmd.Parameters.AddWithValue("p_telefono", maskedTextBox1.Text); 
cmd.Parameters.AddWithValue("p_email", txtCorreo.Text); 
cmd.Parameters.AddWithValue("p_tipocontacto", comboBox1.Text);
conectar.Open();
cmd.ExecuteNonQuery();
    
answered by 25.10.2017 в 10:01