PDA

View Full Version : سوال: خطای Invalid attempt to call Read when reader is closed هنگام اتصال به پایگاه داده



pooriajojo
پنج شنبه 14 اسفند 1393, 11:53 صبح
سلام من تو اتصال به پایگاه داده مشگل دارم . این قسمت :
من اینا نوشتم

SqlConnection connection = new SqlConnection();
connection.ConnectionString = @"Data Source=(local);Initial Catalog=khadamat;Integrated Security=True";
SqlCommand command = new SqlCommand("select namep,neshanp,idmeli,daraje from personel1 where id='" + txid.Text + "'", connection);
connection.Open();
SqlDataReader dr = command.ExecuteReader();
while (dr.Read())
{
txname.Text = (dr["namep"].ToString());
txneshan.Text = (dr["neshanp"].ToString());
txidmeli.Text = (dr["idmeli"].ToString());
txdaraje.Text = (dr["daraje"].ToString());

connection.Close();


این موردا error میده : while (dr.Read())
این error : Invalid attempt to call Read when reader is closed.

golbafan
پنج شنبه 14 اسفند 1393, 12:07 عصر
SqlConnection connection = new SqlConnection();
connection.ConnectionString = @"Data Source=(local);Initial Catalog=khadamat;Integrated Security=True";
SqlCommand command = new SqlCommand("select namep,neshanp,idmeli,daraje from personel1 where id='" + txid.Text + "'", connection);
connection.Open();
SqlDataReader dr = command.ExecuteReader();
while (dr.Read())
{
txname.Text = (dr["namep"].ToString());
txneshan.Text = (dr["neshanp"].ToString());
txidmeli.Text = (dr["idmeli"].ToString());
txdaraje.Text = (dr["daraje"].ToString());
} //این یادت رفته بود؟
connection.Close();


فکر کنم در اجرای دوم حلقه ، کانکشن بسته شده
به قسمت "//این یادت رفته بود؟" توجه کنید