Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Invalid attempt to call Read when reader is closed

Invalid attempt to call Read when reader is closed

Scheduled Pinned Locked Moved ASP.NET
helpannouncement
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    indian22
    wrote on last edited by
    #1

    Hello sir, I am getting error("Invalid attempt to call Read when reader is closed.") even though there should be data present. while executing the reader second time.Can u help me to resolve the problem. MY CODE:

    SqlCommand cmd6 = new SqlCommand("select productid,serialno,qnty,price,tax,total from invoicetran where qotranid='" + ddlqono.SelectedItem.Text + "' and company_id='" + company_id.Text + "' ", myconnection);

            myconnection.Open();
            SqlDataReader myDataReader16 = default(SqlDataReader);
            myDataReader16 = cmd6.ExecuteReader(CommandBehavior.CloseConnection);
          
           while ((myDataReader16.Read()) == true)
            {
                if (myDataReader16.HasRows == true)
                {
                    invproductid.Text = myDataReader16\["productid"\].ToString();
                    invserialno.Text = myDataReader16\["serialno"\].ToString();
                    invtot1.Text = myDataReader16\["total"\].ToString();
                    qnty2.Text = myDataReader16\["qnty"\].ToString();
                    invprice.Text = myDataReader16\["price"\].ToString();
                    invtax.Text = myDataReader16\["tax"\].ToString();
                    untprice.Text = (Convert.ToDecimal(invprice.Text)).ToString();
                    unttax.Text = (Convert.ToDecimal(invtax.Text)).ToString();
                    unttot.Text = (Convert.ToDecimal(untprice.Text) + Convert.ToDecimal(unttax.Text)).ToString();
                    SqlCommand cmd4 = new SqlCommand("select productid,qnty from stock where productid='" + invproductid.Text + "' and status='A' and company\_id='" + company\_id.Text + "'  ", myconnection1);
                    myconnection1.Open();
                    SqlDataReader myDataReader9 = default(SqlDataReader);
                    myDataReader9 = cmd4.ExecuteReader(CommandBehavior.CloseConnection);
                    while ((myDataReader9.Read()) == true)
                    {
                        productid11.Text = myDataReader9\["productid"\].ToString();
                        qnty1.Text = myDataReader9\["qnty"\].ToString();
                    }
                    myDataReader9.Close();
                    myconnection1.Close();
                    if (qnty1.Text == qnty2.Text)
                    {
                        SqlCommand cmd5 = new SqlCommand("update  stock set status='NA' where productid='" + productid11.Text + "'and company\_id='" + company\_id.Text + "'  ", myconnection2);
                        m
    
    A 1 Reply Last reply
    0
    • I indian22

      Hello sir, I am getting error("Invalid attempt to call Read when reader is closed.") even though there should be data present. while executing the reader second time.Can u help me to resolve the problem. MY CODE:

      SqlCommand cmd6 = new SqlCommand("select productid,serialno,qnty,price,tax,total from invoicetran where qotranid='" + ddlqono.SelectedItem.Text + "' and company_id='" + company_id.Text + "' ", myconnection);

              myconnection.Open();
              SqlDataReader myDataReader16 = default(SqlDataReader);
              myDataReader16 = cmd6.ExecuteReader(CommandBehavior.CloseConnection);
            
             while ((myDataReader16.Read()) == true)
              {
                  if (myDataReader16.HasRows == true)
                  {
                      invproductid.Text = myDataReader16\["productid"\].ToString();
                      invserialno.Text = myDataReader16\["serialno"\].ToString();
                      invtot1.Text = myDataReader16\["total"\].ToString();
                      qnty2.Text = myDataReader16\["qnty"\].ToString();
                      invprice.Text = myDataReader16\["price"\].ToString();
                      invtax.Text = myDataReader16\["tax"\].ToString();
                      untprice.Text = (Convert.ToDecimal(invprice.Text)).ToString();
                      unttax.Text = (Convert.ToDecimal(invtax.Text)).ToString();
                      unttot.Text = (Convert.ToDecimal(untprice.Text) + Convert.ToDecimal(unttax.Text)).ToString();
                      SqlCommand cmd4 = new SqlCommand("select productid,qnty from stock where productid='" + invproductid.Text + "' and status='A' and company\_id='" + company\_id.Text + "'  ", myconnection1);
                      myconnection1.Open();
                      SqlDataReader myDataReader9 = default(SqlDataReader);
                      myDataReader9 = cmd4.ExecuteReader(CommandBehavior.CloseConnection);
                      while ((myDataReader9.Read()) == true)
                      {
                          productid11.Text = myDataReader9\["productid"\].ToString();
                          qnty1.Text = myDataReader9\["qnty"\].ToString();
                      }
                      myDataReader9.Close();
                      myconnection1.Close();
                      if (qnty1.Text == qnty2.Text)
                      {
                          SqlCommand cmd5 = new SqlCommand("update  stock set status='NA' where productid='" + productid11.Text + "'and company\_id='" + company\_id.Text + "'  ", myconnection2);
                          m
      
      A Offline
      A Offline
      Arun Jacob
      wrote on last edited by
      #2

      Error itself is self explanatory.You are closing the reader before the execution is completed.

      Arun Jacob http://codepronet.blogspot.com/

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups