locked
Invalid attempt to Read when reader is closed RRS feed

  • General discussion

  • Can anyone please tell me the solution of this error
    I am sending you the whole code. PLease tell me where i had done mistake

    private

     

    void SetData()

     

    BoundField boundfieldEmpID = new BoundField();

    boundfieldEmpID.HeaderText =

     

    "Employee ID";

    boundfieldEmpID.DataField =

     

    "EmployeeID";

     

    BoundField boundfieldEmpName = new BoundField();

    boundfieldEmpName.HeaderText =

     

    "Employee Name";

    boundfieldEmpName.DataField =

     

    "EmployeeName";

     

    SqlConnection conn = new SqlConnection();

    conn.ConnectionString =

     

    ConfigurationManager.ConnectionStrings["EmpConnectionString"].ConnectionString;

     

    SqlCommand cmdData = new SqlCommand("select EmployeeID, EmployeeName from Emp_PersonalInformation", conn);

     

     

    SqlDataReader sdrData;

    sdrData = cmdData.ExecuteReader(

     

    CommandBehavior.CloseConnection);

     

    DataTable dtData = new DataTable();

     

     

    DataColumn dcEmpID = new DataColumn();

    dcEmpID.ColumnName =

     

    "EmployeeID";

    dcEmpID.DataType = System.

     

    Type.GetType("System.Int32");

     

    DataColumn dcEmpName = new DataColumn();

    dcEmpName.ColumnName =

     

    "EmployeeName";

    dcEmpID.DataType = System.

     

    Type.GetType("System.String");

     

    if (sdrData != null)

     

    while (sdrData.Read()) -----here i am getting error

     

    DataRow drData = dtData.NewRow();

     

     

    for (int i = 0; i < sdrData.FieldCount; i++)

     

    //drData[0] = 1 + i;

     

    //drData[1] = 2 + i;

    drData[i] = sdrData.GetValue(sdrData.GetOrdinal(sdrData.GetName(i)));

    dtData.Rows.Add(drData);

    }

    }

    }

    ucEventBubbling.BindGrid(dtData);

    sdrData.Close();

    }

    {

     

    {

     

    {

     

    dtData.Columns.Add(dcEmpName);

    dtData.Load(sdrData);

     

    dtData.Columns.Add(dcEmpID);

     

     

     

    conn.Open();

     

    ucEventBubbling.Columns.Add(boundfieldEmpName);

     

    ucEventBubbling.Columns.Add(boundfieldEmpID);

     

    {

    ucEventBubbling.Columns.Clear();  //user control

     

    Monday, August 31, 2009 9:28 AM