IDataReader closed on return from proc

L

Larry R

Whenever I try the following, the reader that is returned is always
closed. What am I missing ?

When I look at the reader in the ExecuteReader, it is fine. THen it
gets closed on the returm.


Thanks!
============================


public void Test()
{
IDBGateway gateway = new
DBGateway(ConnectionChoice.DOM);


using (IDataReader reader =
gateway.ExecuteReader( Utility.GroupSql,null))
{
while (reader.Read())
{
Debug.WriteLine(reader[0].ToString());
}
}
}


===================
Gateway code: ( and all of the factory stuff is fine )


public IDataReader ExecuteReader(string selectStatement,
IDataParameterCollection parameters)
{
try
{
using (IDbConnection connection =
factory.CreateConnection())
{
IDbCommand command = connection.CreateCommand();
command.CommandText = selectStatement;
command.CommandType = CommandType.Text;


try
{


connection.Open();
}
catch
{
throw;
}


IDataReader reader =
command.ExecuteReader( CommandBehavior.CloseConnection);
return reader;


}
}
catch
{
throw;
}


}
 
J

Jesse Houwing

* Larry R wrote, On 22-6-2007 18:56:
Whenever I try the following, the reader that is returned is always
closed. What am I missing ?

When I look at the reader in the ExecuteReader, it is fine. THen it
gets closed on the returm.

The using clause around your connection will close the connection as
soon as that code block is left. It will close the reader with it.

There is a property you can set on the reader to close the connection
when the reader itself is being disposed. Which you are already setting.
So not disposing the connection immediately should solve your issue.

Jesse Houwing

Updated code:

public void Test()
{
IDBGateway gateway = new
DBGateway(ConnectionChoice.DOM);


using (IDataReader reader =
gateway.ExecuteReader( Utility.GroupSql,null))
{
while (reader.Read())
{
Debug.WriteLine(reader[0].ToString());
}
}
}


===================
Gateway code: ( and all of the factory stuff is fine )


public IDataReader ExecuteReader(string selectStatement,
IDataParameterCollection parameters)
{
IDbConnection connection;
try
{
connection = factory.CreateConnection()
{
IDbCommand command = connection.CreateCommand();
command.CommandText = selectStatement;
command.CommandType = CommandType.Text;


try
{
connection.Open();
}
catch
{
throw;
}

IDataReader reader =
command.ExecuteReader( CommandBehavior.CloseConnection);
return reader;
}
}
catch
{
if (connection != null)
{
connection.Dispose();
}
throw;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top