0

Invalid attempt to call Read when reader is closed. getting this error asp.net with c#? i have used this code

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();
John Saunders
  • 160,644
  • 26
  • 247
  • 397
user3259635
  • 51
  • 1
  • 12
  • 4
    To be honest, i would throw your `Globals` class into the garbage can since it's just a source of nasty errors. http://stackoverflow.com/questions/9705637/executereader-requires-an-open-and-available-connection-the-connections-curren – Tim Schmelter Mar 12 '14 at 10:33
  • 3
    Yeah. Wholy crap - you use a global holder for non global objects? Seen a lot of bad code, this one hits first place. And no use of using. What about you get your code cleaned up and start following best practices - then errors based on not so nice code will disappear. – TomTom Mar 12 '14 at 10:34
  • You should have this code reviewed and the design reconsidered. Rewrite FTW. – Saro Taşciyan Mar 12 '14 at 10:40
  • @Tim Schmelter -Thank u lot – user3259635 Mar 12 '14 at 10:53

1 Answers1

0

If you've code with obvious problems and problems you can't find, fix the obvious problems first and then the obscure problems will likely become obvious:

Get rid of the globals rubbish and put using in the appropriate places and then having the Dispose() called from that rather than explicitly will also fix this problem.

Jon Hanna
  • 110,372
  • 10
  • 146
  • 251