G
Guest
Hi,
I have a class A with a public method GetReader:
public OleDbDataReader GetReader()
{
SqlConnection myConnection = new
SqlConnection(ConfigurationSettings.AppSettings["ConnStr"]);
SqlCommand myCommand = new SqlCommand("CMRC_Top10List", myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
myConnection.Open();
SqlDataReader result =
myCommand.ExecuteReader(CommandBehavior.CloseConnection);
return result;
}
Class A is used as following:
DataGrid TopTen;
A top10 = new A();
SqlDataReader result = null;
try
{
result = top10.GetReader();
TopTen.DataSource = result;
TopTen.DataBind();
result.Close();
}
catch(Exception ex)
{
if (!result.IsClosed)
{
result.Close();
}
return;
}
The grid is not bind to the data in database, and I receive the following
error:
“Invalid attempt to FieldCount when reader is closed."
Why is reader closed after the return from the method GetReader? I would
expect the reader being open till I explicitly close him.
Thanks for a help.
Lubomir
I have a class A with a public method GetReader:
public OleDbDataReader GetReader()
{
SqlConnection myConnection = new
SqlConnection(ConfigurationSettings.AppSettings["ConnStr"]);
SqlCommand myCommand = new SqlCommand("CMRC_Top10List", myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
myConnection.Open();
SqlDataReader result =
myCommand.ExecuteReader(CommandBehavior.CloseConnection);
return result;
}
Class A is used as following:
DataGrid TopTen;
A top10 = new A();
SqlDataReader result = null;
try
{
result = top10.GetReader();
TopTen.DataSource = result;
TopTen.DataBind();
result.Close();
}
catch(Exception ex)
{
if (!result.IsClosed)
{
result.Close();
}
return;
}
The grid is not bind to the data in database, and I receive the following
error:
“Invalid attempt to FieldCount when reader is closed."
Why is reader closed after the return from the method GetReader? I would
expect the reader being open till I explicitly close him.
Thanks for a help.
Lubomir