本文介绍了当datareader关闭时,SqlDataReader返回无效尝试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
private void BindReader(string storedproc,string[] param)
{
clear();
reader = ppt.ParamReader(storedproc,param ); // Here searching a Branch Name and binding values in matching text box
while (reader.Read())
{
lbl_ID.Text = reader["BRANCH_ID"].ToString();
txt_Address.Text = reader["BRANCH_ADDRESS"].ToString();
txt_Branch_Phone.Text = reader["BRANCH_PHONE"].ToString();
txt_CST_Number.Text = reader["CST_NUMBER"].ToString();
txt_tin_number.Text = reader["TIN_NUMBER"].ToString();
cmb_Branch_Name.Text = reader["BRANCH_NAME"].ToString();
}
reader.Close();
}
public SqlDataReader ParamReader(string sp, string[] pp)
{
if (_connection.State == ConnectionState.Open)
{
_connection.Close();
}
_connection.ConnectionString = setConnection();
try
{
_connection.Open();
cmd = parameters(sp, pp, _connection);// Calls Parameters Method to load the Parameters in the command
reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception e)
{
throw new Exception(e.Message);
}
return reader;
}
<code>
此代码是第一次工作.如果再次调用此方法,则在关闭数据读取器时将返回错误,例如无效尝试.
是什么原因?
<code>
This code is working at very first time. if I call this method again it returns error like invalid attempt when data reader is closed.
what is the reason? what should i do for this to over come this?
推荐答案
if (_connection.State == ConnectionState.Open) { _connection.Close(); }
这就是为什么下次运行代码时会出现此错误的原因.
That is why you get this error when you run your code the next time.
这篇关于当datareader关闭时,SqlDataReader返回无效尝试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!