Click here to Skip to main content
15,890,185 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

While retrieving data from database getting this error "Invalid attempt to call MetaData when reader is closed". (Windows application with c# coding)

C#
private void textBox10_TextChanged(object sender, EventArgs e)
{
    //Retrieve Antenatal Details
    //Here textbox10 = PersonID field in antenatal form.
    SqlConnection con = new SqlConnection(strConn);
    con.Open();
    SqlCommand cmd = new SqlCommand("sp_get_antenatal", con);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@person_id", textBox10.Text);
    SqlDataReader dr = cmd.ExecuteReader();
    if (dr.Read())
    {
        cbPlaceoforder.SelectedValue = dr["antenatal_care_id"].ToString();
        txtAgeofantenatalmother.Text = dr["age"].ToString();
        if (dr["regular_anc"].ToString().Equals("Yes"))
        {
            rbtnRegularancyes.Select();
        }
        else
        {
            rbtnRegularancno.Select();
        }
    }
    con.Close();
    //Retrieve Lactating details
    con.Open();
    SqlCommand cmd1 = new SqlCommand("sp_get_lactating", con);
    cmd1.CommandType = CommandType.StoredProcedure;
    cmd1.Parameters.AddWithValue("@person_id", textBox10.Text);
    SqlDataReader dr1 = cmd1.ExecuteReader();
    if (dr1.Read())
    {
        cbPlaceofanc.SelectedValue = dr1["delivery_place_id"].ToString();
        cbTypeofdelivery.SelectedValue = dr1["delivery_type_id"].ToString();
        cbMilksupplement.SelectedValue = dr1["milk_suppliment_id"].ToString();
        if (dr["breast_feeding"].ToString().Equals("Yes"))
        {
            rbtnBreastfeeding1stsixmonthsyes.Select();
        }
        else
        {
            rbtnBreastfeeding1stsixmonthsno.Select();
        }
    }
    con.Close();
}


How to resolve this issue?

Thanks and Regards,
Murali.K
Posted
Comments
Jibesh 2-Jan-13 6:36am    
which line you are experiencing this error? post the exception stack for better understanding. As explained its always better to close the dataReader when you are done with the data Reader.
[no name] 2-Jan-13 6:45am    
if (dr["breast_feeding"].ToString().Equals("Yes"))

Above line the error getting.
Jibesh 2-Jan-13 7:02am    
datareader dr is already closed and you are trying to access a closed connection. check my solution.

hi,

Use this
SqlDataReader dr = cmd.ExecuteReader();
if (dr!=null && dr.HasRows)
{
  While(dr.Read())
  {

   // Do what ever you want
  }
  dr.Close();
}
 
Share this answer
 
Hi,

Here in the following code you are accessing the data reader and not closing it explicitly.
C#
if (dr.Read())
            {
                cbPlaceoforder.SelectedValue = dr["antenatal_care_id"].ToString();
                txtAgeofantenatalmother.Text = dr["age"].ToString();
                if (dr["regular_anc"].ToString().Equals("Yes"))
                {
                    rbtnRegularancyes.Select();
                }
                else
                {
                    rbtnRegularancno.Select();
                }
            }
            dr.Close();
            con.Close();


So change the code by closing the dara reader before the con object is closed.
This will resolve your problem.

Thanks
 
Share this answer
 
v2
Comments
[no name] 2-Jan-13 6:55am    
I tried like you, but getting same error in below line.
if (dr["breast_feeding"].ToString().Equals("Yes"))
[no name] 2-Jan-13 6:59am    
Keep your dr["breast_feeding"].ToString() value in a variable and then use it in the lower block. The error is comming only due to the datareader dr is closed. So change your code accordingly.
You are trying to access the reader value on a closed object. i.e

Quote:
if (dr["breast_feeding"].ToString().Equals("Yes"))
is called after the prev connection is closed. what you can do here is copy this value into a local variable and use that here instead of using the dataReader.

C#
bool breastFeeding = false;
if (dr.Read())
 {
      cbPlaceoforder.SelectedValue = dr["antenatal_care_id"].ToString();
      txtAgeofantenatalmother.Text = dr["age"].ToString();
      if (dr["regular_anc"].ToString().Equals("Yes"))
      {
         rbtnRegularancyes.Select();
      }
       else
       {
         rbtnRegularancno.Select();
        }
      breastFeeding = dr["breast_feeding"].ToString().Equals("Yes")?true:false;
   }
  con.Close();


then use it like below
C#
if (dr1.Read())
{
        cbPlaceofanc.SelectedValue = dr1["delivery_place_id"].ToString();
        cbTypeofdelivery.SelectedValue = dr1["delivery_type_id"].ToString();
        cbMilksupplement.SelectedValue = dr1["milk_suppliment_id"].ToString();
        if (breastFeeding)
        {
               rbtnBreastfeeding1stsixmonthsyes.Select();
          }
         else
           {
               rbtnBreastfeeding1stsixmonthsno.Select();
            }          
 }
 
Share this answer
 
Comments
[no name] 2-Jan-13 7:52am    
This time getting error below line.
breastFeeding = dr["breast_feeding"].ToString().Equals("Yes")?true:false;

"IndexOutof range exception was unhanded".
Jibesh 2-Jan-13 7:54am    
can you update your code using Improve Question link. post the updated code. so that we can see what went wrong.
[no name] 2-Jan-13 23:27pm    
I have modified my code like you.

private void textBox10_TextChanged(object sender, EventArgs e)
{
bool breastFeeding = false;
//Retrieve Antenatal Details
//Here textbox10 = PersonID field in antenatal form.
SqlConnection con = new SqlConnection(strConn);
con.Open();
SqlCommand cmd = new SqlCommand("sp_get_antenatal", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@person_id", textBox10.Text);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
cbPlaceoforder.SelectedValue = dr["antenatal_care_id"].ToString();
txtAgeofantenatalmother.Text = dr["age"].ToString();
if (dr["regular_anc"].ToString().Equals("Yes"))
{
rbtnRegularancyes.Select();
}
else
{
rbtnRegularancno.Select();
}
breastFeeding = dr["breast_feeding"].ToString().Equals("Yes") ? true : false;
}
//dr.Close();
con.Close();
//Retrieve Lactating details
con.Open();
SqlCommand cmd1 = new SqlCommand("sp_get_lactating", con);
cmd1.CommandType = CommandType.StoredProcedure;
cmd1.Parameters.AddWithValue("@person_id", textBox10.Text);
SqlDataReader dr1 = cmd1.ExecuteReader();
if (dr1.Read())
{
cbPlaceofanc.SelectedValue = dr1["delivery_place_id"].ToString();
cbTypeofdelivery.SelectedValue = dr1["delivery_type_id"].ToString();
cbMilksupplement.SelectedValue = dr1["milk_suppliment_id"].ToString();
if (dr["breast_feeding"].ToString().Equals("Yes"))
{
rbtnBreastfeeding1stsixmonthsyes.Select();
}
else
{
rbtnBreastfeeding1stsixmonthsno.Select();
}
}
//dr1.Close();
con.Close();
}

Error: breast_feeding "Index out of Range Exception was unhandled.
Jibesh 2-Jan-13 23:49pm    
did you check 'breast_feeding' field is being selected or available in the selected list? check for any misspell.

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