Click here to Skip to main content
11,496,146 members (410 online)
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
See more: C#
Hi All,

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

 

        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 2-Jan-13 1:22am
Comments
jibesh at 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.
jibesh at 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();
}
  Permalink  
Hi,

Here in the following code you are accessing the data reader and not closing it explicitly.
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
  Permalink  
v2
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.

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
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();
            }          
 }
  Permalink  
Comments
jibesh at 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.
jibesh at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 10,401
1 OriginalGriff 8,910
2 Sascha Lefèvre 3,899
3 Maciej Los 3,422
4 Richard Deeming 2,600


Advertise | Privacy | Mobile
Web01 | 2.8.150520.1 | Last Updated 2 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100