Click here to Skip to main content
15,887,397 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,

I am facing one issue. Might be its very simple but i unable to solve it.
I am getting exceptions as :
There is already an open DataReader associated with this Command which must be closed first.

My Code block is :
protected void btnSubmit_Click(object sender, EventArgs e)
   {
       try
       {
           bool Ok = false;
           dt.OpenCon();
           string query = "select MA_Joining_Date from master_applicant where MA_App_Code='" + Session["U_Emp_Code"].ToString() + "'";
           SqlDataReader dr = dt.ExecuteReader(query);
           while (dr.Read())
           {
               DateTime date1 = DateTime.Parse(dr["MA_Joining_Date"].ToString());

                if (DateTime.Now.AddMonths(-6) < date1)
                   {
                     drdwnLeaveType.SelectedValue = "4";
                   }
           }

on the same button i put another 3 conditions also, that all are working properly.
C#
double NOD1 = Convert.ToDouble(ViewState["NoOfDays"].ToString());
            string qry = "select Status_ID from applicant_leavedetails where App_Code ='" + Session["U_Emp_Code"].ToString() + "' and Status_ID ='0'";
            string Status = dt.ExecuteScalar(qry);
            if (Status != "")
            {
                ScriptManager.RegisterStartupScript(Page, GetType(), "key1", "alert('Your previous application is pending');", true);
            }
               
            else if (NOD1 == 0)
            {
                ScriptManager.RegisterStartupScript(Page, GetType(), "key1", "alert('You have to select calculate leave days');", true);
            }
            else if (NOD1 <= 2 && drdwnLeaveType.SelectedIndex == 1)
            {
                ScriptManager.RegisterStartupScript(Page, GetType(), "key1", "alert('Please select CL Leave bellow that 2 days');", true);
                Ok = false;
            }

but not while condition is not working properly. what should i do??
Posted

1 solution

Dispose your objects!

The error message is pretty explicit:
"There is already an open DataReader associated with this Command which must be closed first."
What it means is that you can only ever have one SqlDataReader open on any single connection. To open a new one, you must close the old one first.
But you don't - you create it, use it, and then ignore it. So the only way it will be closed at present is by the Garbage Collector when (and if) it gets round to cleaning up after you! So Dispose your objects when you are finished with them!

The easiest way to do this is via a using block:
C#
string query = "select MA_Joining_Date from master_applicant where MA_App_Code='" + Session["U_Emp_Code"].ToString() + "'";
 using (SqlDataReader dr = dt.ExecuteReader(query))
 {
     while (dr.Read())
     {
         DateTime date1 = DateTime.Parse(dr["MA_Joining_Date"].ToString());

          if (DateTime.Now.AddMonths(-6) < date1)
             {
               drdwnLeaveType.SelectedValue = "4";
             }
     }
 }
This will Close, Dispose and remove the SqlDataReader from scope so it can't be accidentally used again.

And please, for your own sake, don't do SQL like that! Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
 
Share this answer
 
Comments
Frankie-C 14-May-15 3:31am    
+5
Member 11221185 14-May-15 3:52am    
Thanks. My problem has been solved on this button.
OriginalGriff 14-May-15 4:07am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900