读取器关闭时无效尝试调用MetaData。 [英] Invalid attempt to call MetaData when reader is closed.?
问题描述
大家好,
从数据库中检索数据时收到此错误读取器关闭时无效尝试调用MetaData。 (使用c#编码的Windows应用程序)
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();
}
如何解决这个问题?
谢谢和问候,
Murali.K
How to resolve this issue?
Thanks and Regards,
Murali.K
推荐答案
使用此
Use this
SqlDataReader dr = cmd.ExecuteReader();
if (dr!=null && dr.HasRows)
{
While(dr.Read())
{
// Do what ever you want
}
dr.Close();
}
您好,
在以下代码中,您访问的是数据阅读器,而不是明确地关闭它。
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();
因此,在关闭con对象之前关闭dara阅读器来更改代码。 br />
这将解决您的问题。
谢谢
So change the code by closing the dara reader before the con object is closed.
This will resolve your problem.
Thanks
您正试图访问读者的价值一个封闭的对象。即
You are trying to access the reader value on a closed object. i.e
if(dr [breast_feeding]。ToString( )。关闭prev连接后调用.Equals(Yes))
if (dr["breast_feeding"].ToString().Equals("Yes"))
。你可以在这里做的是将这个值复制到一个局部变量并使用它而不是使用dataReader。
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();
}
}
这篇关于读取器关闭时无效尝试调用MetaData。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!