It is possible to update a stock with stored procedure using a foreach that is, I have this procedure execution:
SqlConnection conec = new SqlConnection();
SqlCommand cm = new SqlCommand();
cm.Connection = con;
cm.CommandText = "SP_ActualizaInventario";
cm.CommandType = CommandType.StoredProcedure;
cm.Parameters.Add(new SqlParameter("@cantidad", SqlDbType.Int));
cm.Parameters["@cantidad"].Value = Convert.ToInt32(row.Cells["Column13"].Value);
cm.Parameters.Add(new SqlParameter("@Producto", SqlDbType.VarChar));
cm.Parameters["@Producto"].Value = Convert.ToString(row.Cells["Column7"].Value);
cm.Parameters.Add(new SqlParameter("@Tipo", SqlDbType.Int));
cm.Parameters["@Tipo"].Value = 1;
is going to take the value of the columns that you indicate to execute the procedure but the procedure apparently does not take each line but the last line only as I fix it? I put it inside the foreach or outside?
store procedure
ALTER PROCEDURE [dbo].[SP_ActualizaInventario]
@cantidad INT
,@Producto VARCHAR
,@Tipo INT
AS
BEGIN
DECLARE @Existencia INT
SET @Existencia = (SELECT Existencia FROM maestraproductoinventario WHERE [Codigo De Articulo] = @Producto)
if(@Tipo = 1)
begin
UPDATE
maestraproductoinventario
SET
Existencia = @Existencia - @cantidad
WHERE [Codigo De Articulo] = @Producto
end
else
begin
UPDATE
maestraproductoinventario
SET
Existencia = @Existencia + @cantidad
WHERE [Codigo De Articulo] = @Producto
end
END