473,385 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

IDataReader closed on return from proc

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;
}
}

Jun 22 '07 #1
2 2070
* 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;
}
}
Jun 22 '07 #2
Thanks so much!

Jun 23 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Robin Hammond | last post by:
Can anybody tell me why a) when running a stored proc from an asp page to return a recordset the command succeeds if the sp queries an existing table directly, but b) if the stored proc populates...
5
by: Tamir Khason | last post by:
As far as I understand, IDataReader, based on command recieves isClosed property when the connection closes. How to prevent it?
5
by: Gelios | last post by:
Hello All! I am going to crazy and feeling myself so stupid but I don't understand such behaviour. I have code: public int getNextAgentId() { Int32 agent_id = 0; IDataReader dr =...
10
by: Brent | last post by:
I'm quickly running out of available db connections with the code below. I like to think I'm closing everything as I go along, but I watch the connections shoot up as soon as the the method getIDs...
3
by: js | last post by:
Hi, I want to debug the return idatareader, how to print out some fields value? many thanks.
2
by: יוני גולדברג | last post by:
Hi, In few places within my code the business object pass IDataReader to the GUI. Suddenly i noticed that nowhere in the code the IDataReader is being closed. Does the data binding operation...
0
by: rsdev | last post by:
Hi, I am new to ASP.NET and I am trying to build a CMS application using theBeerHouse SK. I am adapting the Datareader to collect information from an SQL database and send it to an HtmlHeader. ...
2
by: RP | last post by:
I saw a code in a class where IDataReader is used to retrieve result from query, even if only one column is to be retrieved. It uses a CommandObject with ExecuteReader but not using ExecuteScalar....
0
by: Kevac Marko | last post by:
Hi. I have to send UDP packets very often. Approx twice in a second. But socket is not closed after sending packet. So soon i bump into open sockets\files limit. How to close socket after...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.