Invalid attempt to call Read when reader is closed
-
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
-
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
Error itself is self explanatory.You are closing the reader before the execution is completed.
Arun Jacob http://codepronet.blogspot.com/