473,405 Members | 2,415 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,405 software developers and data experts.

DataReader Close Issue

Hi,

I have a situation where in a dll a SqlDataReader is created on a
function call but is never closed. The datareader gets passed back to
the asp.net page calling it. How should I close the SqldataReader in
the function where it is being created properly? If I close the
SqlDataReader in the function my datareader returns nothing of course.
So how do I code it properly to pass back a datareader and close it in
the function?

Thanks,
J

Jan 11 '06 #1
5 7879
Can you return the reader in a try statement and close it in the finally.
I believe the finally will be run when the reader is no longer referenced,
but not sure.

public SqlDataReader GetDataReader()
{
SqlDataReader retVal = nul;
try
{
// Initialize data reader
retVal = new SqlDataReader();
return retVal;
}
finally
{
retVal.Close();
}

// in case something happens
return null;
}
Hi,

I have a situation where in a dll a SqlDataReader is created on a
function call but is never closed. The datareader gets passed back to
the asp.net page calling it. How should I close the SqldataReader in
the function where it is being created properly? If I close the
SqlDataReader in the function my datareader returns nothing of course.
So how do I code it properly to pass back a datareader and close it in
the function?

Thanks,
J

Jan 11 '06 #2
You don't close the datareader in the function. But you do pass
CommandBehavior.CloseConnection to the ExecuteReader.

Then, whatever code is calling the function is responsible for calling Close
on the data reader. Since you passed in CloseConnection to ExecuteReader,
closing the datareader will close the connection as well.

Or, you can rethink this, and see if your function can return a DataTable
instead, then you dont' have to deal with any of this, or worry about
remember to close the datareader when you are done.

<jj****@hotmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Hi,

I have a situation where in a dll a SqlDataReader is created on a
function call but is never closed. The datareader gets passed back to
the asp.net page calling it. How should I close the SqldataReader in
the function where it is being created properly? If I close the
SqlDataReader in the function my datareader returns nothing of course.
So how do I code it properly to pass back a datareader and close it in
the function?

Thanks,
J

Jan 11 '06 #3
Hi,

<jj****@hotmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Hi,

I have a situation where in a dll a SqlDataReader is created on a
function call but is never closed. The datareader gets passed back to
the asp.net page calling it. How should I close the SqldataReader in
the function where it is being created properly? If I close the
SqlDataReader in the function my datareader returns nothing of course.
So how do I code it properly to pass back a datareader and close it in
the function?

You should call Close once you are done with the DataReader this will
effectively close it. You can it from anywhere you have access to it.

Now, depending of how it was created ( if a CommandBehavior.CloseConnection
was passed as a parameter) the underlying connection will get closed too.

Use this overload to create the datareader and you should be ok

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Jan 11 '06 #4
This won't work. The reason is that the reader will be closed before
the function returns. Remember, the finally statement gets called no matter
what.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Josh Belden" <jo**********@hotmail.com> wrote in message
news:de*************************@news.microsoft.co m...
Can you return the reader in a try statement and close it in the finally.
I believe the finally will be run when the reader is no longer referenced,
but not sure.

public SqlDataReader GetDataReader()
{
SqlDataReader retVal = nul;
try
{
// Initialize data reader
retVal = new SqlDataReader();
return retVal;
}
finally
{
retVal.Close();
}

// in case something happens
return null;
}
Hi,

I have a situation where in a dll a SqlDataReader is created on a
function call but is never closed. The datareader gets passed back to
the asp.net page calling it. How should I close the SqldataReader in
the function where it is being created properly? If I close the
SqlDataReader in the function my datareader returns nothing of course.
So how do I code it properly to pass back a datareader and close it in
the function?

Thanks,
J


Jan 11 '06 #5
No, the finally block will run after either the try finishes, or the catch
does (if an exception is thrown). This will close the datareader before it
is returned. You might want to look over how try/catch/finally blocks work.

"Josh Belden" <jo**********@hotmail.com> wrote in message
news:de*************************@news.microsoft.co m...
Can you return the reader in a try statement and close it in the finally.
I believe the finally will be run when the reader is no longer referenced,
but not sure.

public SqlDataReader GetDataReader()
{
SqlDataReader retVal = nul;
try
{
// Initialize data reader
retVal = new SqlDataReader();
return retVal;
}
finally
{
retVal.Close();
}

// in case something happens
return null;
}
Hi,

I have a situation where in a dll a SqlDataReader is created on a
function call but is never closed. The datareader gets passed back to
the asp.net page calling it. How should I close the SqldataReader in
the function where it is being created properly? If I close the
SqlDataReader in the function my datareader returns nothing of course.
So how do I code it properly to pass back a datareader and close it in
the function?

Thanks,
J


Jan 11 '06 #6

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

Similar topics

2
by: Bernardo | last post by:
Hi, I have a datareader, and I want for each record in my datareader to make an insert in other table, the problem is that an error occures, it sais that i have to close the datareder first!...
2
by: Jake S | last post by:
Hi all, Is it possibele to set the datasource of a dropdown list to a datareader? When I try to the only column I receive is a column populated by the datasource name repeated the amount of...
4
by: VB Programmer | last post by:
I have a function that returns a datareader. The problem is that in the function's FINALLY I close the datareader and set it to nothing. But, BEFORE the Finally I "Return" the datareader (in the...
20
by: Mark | last post by:
Hi all, quick question , a DataView is memory resident "view" of data in a data table therefore once populated you can close the connection to the database. Garbage collection can then be used to...
6
by: Kalim Julia | last post by:
Is it possible to open more than 1 datareader / dataadapter on one connection ? Is it possible to duplicate / clone connection ?
17
by: Alan Silver | last post by:
Hello, I have a generic method in a utility class that grabs an sqldatareader and returns it. Due to the fact that (AFAIK), you can't close the database connection before you've read the data,...
3
by: Alex Maghen | last post by:
Hi. I have a whole bunch of queries that I'll be running and returning in OleDbDataReader objects. Some of these DataReaders will just be a simple singe table result and others will be more complex...
7
by: Diffident | last post by:
Hello All, I would like to use DataReader based accessing in my Data Access Layer (DAL). What is considered to be a best practice while returning from a DAL method that executes a query and...
3
by: Johnny Jörgensen | last post by:
I've got an error that I simply cannot locate: I've got a form in which I use a datareader object to read information from a db. After the read, I close the reader and I dispose of both the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.