2

I have been trying to add new information to my database from a Windows Form (Gridview). This is the method I came up with (but it doesn't work):

private void agregarProducto(string id_producto, string id_proveedor, string id_categoria, string cantidad, string precio_actual, string codigo_barras)
{
    MySqlCommand cmd = new MySqlCommand();

    using (cmd = cn.CreateCommand())
    {
        cmd.CommandText = "INSERT INTO productos(id_producto, id_proveedor, id_categoria, cantidad, precio_actual, codigo_barras) VALUES (@id_producto, @id_proveedor, @id_categoria, @cantidad, @precio_actual, @codigo_barras)";
        cmd.CommandType = CommandType.Text;

        cmd.Parameters.AddWithValue("@id_producto", tbId_Prod);
        cmd.Parameters.AddWithValue("@id_categoria", tbId_categoria);
        cmd.Parameters.AddWithValue("@id_proveedor", tb_Id_proveedor);
        cmd.Parameters.AddWithValue("@cantidad", tbCantidad);
        cmd.Parameters.AddWithValue("@precio_actual", tbPrecioActual);
        cmd.Parameters.AddWithValue("@codigo_barras", tbCod_barras);

        cn.Open();
    }
}

This is the event that's is supposedly calling it:

private void btAgregarNuevo_Click(object sender, EventArgs e)
{
    agregarProducto(tbId_Prod.Text, tb_Id_proveedor.Text, tbId_categoria.Text, tbCantidad.Text, tbPrecioActual.Text, tbCod_barras.Text);
}

Am I missing something?

2 Answers 2

2

You haven't executed the sql. Do this after you open the connection

cmd.ExecuteNoneQuery();
Sign up to request clarification or add additional context in comments.

2 Comments

Alright it does insert now but it's showing 0's in the fields. I made a query on MySQL and it's working fine my guess is the conversion or the method.
@GJenyRamirez. You are passing string types to your method that creates and executes the query. Should have this method accept the correct type (presumably int or long) and do the appropriate conversions in your click event. E.g. int.Parse(tbId_Prod.Text)
1
private void agregarProducto(string id_producto, string id_proveedor, string id_categoria, string cantidad, string precio_actual, string codigo_barras)
{
    MySqlCommand cmd = new MySqlCommand();

    using (cmd = cn.CreateCommand())
    {
        cmd.CommandText = "INSERT INTO productos(id_producto, id_proveedor, id_categoria, cantidad, precio_actual, codigo_barras) VALUES (@id_producto, @id_proveedor, @id_categoria, @cantidad, @precio_actual, @codigo_barras)";
        cmd.CommandType = CommandType.Text;

        cmd.Parameters.AddWithValue("@id_producto", tbId_Prod);
        cmd.Parameters.AddWithValue("@id_categoria", tbId_categoria);
        cmd.Parameters.AddWithValue("@id_proveedor", tb_Id_proveedor);
        cmd.Parameters.AddWithValue("@cantidad", tbCantidad);
        cmd.Parameters.AddWithValue("@precio_actual", tbPrecioActual);
        cmd.Parameters.AddWithValue("@codigo_barras", tbCod_barras);

        cn.Open();

        cmd.ExecuteNoneQuery();//This is the line you are missing

        cn.Close();
    }
}

1 Comment

Alright it does insert now but it's showing 0's in the fields. I made a query on MySQL and it's working fine my guess is the conversion or the method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.