读取器关闭时调用Read的尝试无效.正在获取此错误

本文关键字:无效 获取 错误 Read 调用 读取 | 更新日期: 2023-09-27 17:49:36

读取器关闭时调用Read的尝试无效。用c#在asp.net上得到这个错误吗?我用过这个代码

string catalogNo = string.Empty;
string deleteID = string.Empty;  
Globals.Initialize("Text", "select CatelogNo,DeleteID from tbl_admin_quotation where QuotationID='" + quotation3 + "' order by id asc");
Globals.dr = Globals.cmd.ExecuteReader();
while (Globals.dr.Read() == true)
{
    catalogNo = Globals.dr[0].ToString();
    deleteID = Globals.dr[1].ToString();
    decimal taqty = 0;
    Globals.Initialize("Text", "select qty from tbl_admin_quotation where DeleteID='" + deleteID + "'");
    Globals.dr3 = Globals.cmd.ExecuteReader();
    if (Globals.dr3.Read() == true)
    {
        taqty = Convert.ToDecimal(Globals.dr3[0].ToString());
    }
    Globals.dr3.Dispose();
    Globals.dr3.Close();
    Globals.Initialize("Text", "select Pqty,Hqty from  tbl_admin_stock where CatelogNo='" + catalogNo + "'");
    Globals.dr = Globals.cmd.ExecuteReader();
    if (Globals.dr.Read() == true)
    {
        if (Convert.ToDecimal(Globals.dr[0].ToString()) != 0)
        {
            Globals.Initialize("Text", "update tbl_admin_stock set Pqty=Pqty+'" + Convert.ToDecimal(taqty) + "' where CatelogNo='" + catalogNo + "'");
            Globals.cmd.ExecuteNonQuery();
        }
        else if (Convert.ToDecimal(Globals.dr[1].ToString()) != 0)
        {
            Globals.Initialize("Text", "update tbl_admin_stock set Hqty=Hqty-'" + Convert.ToDecimal(taqty) + "' where CatelogNo='" + catalogNo + "'");
            Globals.cmd.ExecuteNonQuery();
        }
    }
    Globals.dr.Dispose();
    Globals.dr.Close();
}
  Globals.dr.Dispose();
Globals.dr.Close(); 
 Globals.Initialize("Text", "delete from tbl_admin_quotation where  QuotationId=@QuotationId");
Globals.cmd.Parameters.AddWithValue("@QuotationId", quotation3);
Globals.cmd.ExecuteNonQuery();
UpdatePanelMain.Update();
GridviewBind();

读取器关闭时调用Read的尝试无效.正在获取此错误

如果您的代码中有明显的问题和找不到的问题,请先修复明显的问题,然后模糊的问题可能会变得明显:

去掉全局垃圾,将using放在适当的位置,然后从中调用Dispose(),而不是显式调用,也可以解决这个问题。