471,325 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 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 7787
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Bernardo | last post: by
4 posts views Thread by VB Programmer | last post: by
20 posts views Thread by Mark | last post: by
6 posts views Thread by Kalim Julia | last post: by
3 posts views Thread by Alex Maghen | last post: by
7 posts views Thread by Diffident | last post: by
3 posts views Thread by Johnny Jörgensen | last post: by
reply views Thread by rosydwin | last post: by

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.