Reputation: 793
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?
Upvotes: 2
Views: 110
Reputation: 11201
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();
}
}
Upvotes: 1
Reputation: 24526
You haven't executed the sql. Do this after you open the connection
cmd.ExecuteNoneQuery();
Upvotes: 2