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:
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.