最有可能引发异常(subtract_QtyFromStock_Ex <0)。 “过程或函数DeleteFromtb_QtyEx指定了太多参数。”甚至在这种情况下,也不要在循环内调用已声明的过程。
我检查了参数数量 我不使用全局sqlcommand 我分别测试了该功能,效果很好
有问题的存储过程
ALTER proc [dbo].[DeleteFromtb_QtyEx]
@autoCodeIdentifier int
As
Delete from tb_Quantity_Expire
where tb_Quantity_Expire.item_auto_code=@autoCodeIdentifier
DataAccessLayer上的代码
public void ExecuteCommand(string stored_procedure, SqlParameter[] param)
{
SqlCommand cmd = new SqlCommand(stored_procedure, con);
cmd.CommandType = CommandType.StoredProcedure;
if(param != null)
{
cmd.Parameters.AddRange(param);
}
cmd.ExecuteNonQuery();
}
课堂上的代码
public void DeleteFromtb_QtyEx(int autoCodeIdentifier)
{
Cls_DataAccessLayer DAL = new Cls_DataAccessLayer();
DAL.OpenCon();
SqlParameter[] param = new SqlParameter[1];
param[0] = new SqlParameter("@autoCodeIdentifier", SqlDbType.Int);
param[0].Value = autoCodeIdentifier;
DAL.ExecuteCommand("DeleteFromtb_QtyEx", param);
DAL.CloseCon();
}
btn_click上的代码
private void btnTender_Click(object sender, EventArgs e)
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
decimal qty_entered =
Convert.ToDecimal(dataGridView1.Rows[i].Cells[3].Value);
decimal stk =
Convert.ToDecimal(dataGridView1.Rows[i].Cells[5].Value);
decimal subtract = stk - qty_entered;
if (subtract < 0)
{
Cls.DeleteFromtb_QtyEx(Convert.ToInt32(dataGridView1.Rows[i].Cells[0].Value));
Cls.InsertTotb_QtyEx(Convert.ToInt32(dataGridView1.Rows[i].Cells[0].Value), subtract);
}else if(subtract==0)
{
Cls.DeleteFromtb_QtyEx(Convert.ToInt32(dataGridView1.Rows[i].Cells[0].Value));
Cls.InsertTotb_QtyEx(Convert.ToInt32(dataGridView1.Rows[i].Cells[0].Value), subtract);
}else if (subtract > 0)
{
DataTable dt_QtyEx = new DataTable();
dt_QtyEx = Cls.SelectFromtb_QtyEx(Convert.ToInt32(dataGridView1.Rows[i].Cells[0].Value));
decimal subtract_QtyFromStock_Ex = 0;
//decimal qty = Convert.ToDecimal(dataGridView1.Rows[i].Cells[3].Value);
for (int x = 0; x < dt_QtyEx.Rows.Count; x++)
{
if (x == 0)
{
subtract_QtyFromStock_Ex = Convert.ToDecimal(dt_QtyEx.Rows[x][2]) - qty_entered;
}
else
{
subtract_QtyFromStock_Ex = Convert.ToDecimal(dt_QtyEx.Rows[x][2]) - (0 - subtract_QtyFromStock_Ex);
}
if (subtract_QtyFromStock_Ex > 0)
{
Cls.Updatetb_QtyEx(Convert.ToInt32(dt_QtyEx.Rows[x][0]), Convert.ToDateTime(dt_QtyEx.Rows[x][1]), subtract_QtyFromStock_Ex);
break;
}
else if (subtract_QtyFromStock_Ex == 0)
{
Cls.DeleteFromtb_QtyEx_Expire(Convert.ToInt32(dt_QtyEx.Rows[x][0]), Convert.ToDateTime(dt_QtyEx.Rows[x][1]));
break;
}
else if (subtract_QtyFromStock_Ex < 0)
{
Cls.DeleteFromtb_QtyEx_Expire(Convert.ToInt32(dt_QtyEx.Rows[x][0]), Convert.ToDateTime(dt_QtyEx.Rows[x][1]));
}
}
}
}
答案 0 :(得分:0)
谢谢大家.....引发异常是因为我通过调用存储过程“ DeleteFromtb_QtyEx”犯了一个可怕的错误 在其他存储过程“ DeleteFromtb_QtyEx_Expire”的函数中