473,382 Members | 1,692 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,382 software developers and data experts.

data access layer and data reader

hey all,
how do you resolve this problem?

i have a public procedure in my DataAccessLayer that gets a SqlDataReader

how do i close the reader from inside the DataAccessLayer if I'm returning
the reader to get bound to my GridView? Any code after my return will be
unreachable.

thanks,
rodchar
Apr 24 '07 #1
3 2680
rodchar,

There are a few ways to do this. The easiest way that I can think of to
do this would be when you call ExecuteReader on your command, you pass the
CloseConnection value from the CommandBehavior enumeration to the method.
Then, you make it a requirement that whatever routine gets the SqlDataReader
calls Dispose on the reader, so that the underlying connection is closed.

This won't clean up the command that is used to create the reader, but
it doesn't do much in its Dispose implementation anyways that is important
(it just releases a managed reference to some metadata, so that's no big
deal), so I think you can get away with not calling Dispose on the
connection (although I don't like the idea of not doing so, you can't call
Dispose on the connection before you return the reader).

If you REALLY want to be clean about things, what you can do is return a
class that implements IDisposable and exposes the SqlDataReader through a
property. You would then pass the command as well to the class (which would
hold onto it in a private field). You would then require that instead of
calling Dispose on the reader, you call Dispose on the class that returns
the reader. In the implementation of the Dispose method, you would call
Dispose on the data reader as well as the command.

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

"rodchar" <ro*****@discussions.microsoft.comwrote in message
news:8F**********************************@microsof t.com...
hey all,
how do you resolve this problem?

i have a public procedure in my DataAccessLayer that gets a SqlDataReader

how do i close the reader from inside the DataAccessLayer if I'm returning
the reader to get bound to my GridView? Any code after my return will be
unreachable.

thanks,
rodchar


Apr 24 '07 #2
Thank you for the thorough explanation, these are my favorite. I learn too.

"Nicholas Paldino [.NET/C# MVP]" wrote:
rodchar,

There are a few ways to do this. The easiest way that I can think of to
do this would be when you call ExecuteReader on your command, you pass the
CloseConnection value from the CommandBehavior enumeration to the method.
Then, you make it a requirement that whatever routine gets the SqlDataReader
calls Dispose on the reader, so that the underlying connection is closed.

This won't clean up the command that is used to create the reader, but
it doesn't do much in its Dispose implementation anyways that is important
(it just releases a managed reference to some metadata, so that's no big
deal), so I think you can get away with not calling Dispose on the
connection (although I don't like the idea of not doing so, you can't call
Dispose on the connection before you return the reader).

If you REALLY want to be clean about things, what you can do is return a
class that implements IDisposable and exposes the SqlDataReader through a
property. You would then pass the command as well to the class (which would
hold onto it in a private field). You would then require that instead of
calling Dispose on the reader, you call Dispose on the class that returns
the reader. In the implementation of the Dispose method, you would call
Dispose on the data reader as well as the command.

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

"rodchar" <ro*****@discussions.microsoft.comwrote in message
news:8F**********************************@microsof t.com...
hey all,
how do you resolve this problem?

i have a public procedure in my DataAccessLayer that gets a SqlDataReader

how do i close the reader from inside the DataAccessLayer if I'm returning
the reader to get bound to my GridView? Any code after my return will be
unreachable.

thanks,
rodchar


Apr 24 '07 #3

You cannot close the datareader, until after it binds.

Thus why using an IDataReader is tricky (in the presentation layer).

This is one reason to go to either DataSets (strong) or Custom Business
Objects.

I'm not saying you have to, I'm saying its "a" reason.

What I usually do is use the IDataReader in the biz layer, and close it
there. And send up to the presentation layer a collection of custom
business objects.
I trust myself (and gui developers who might forget to close the
IDataReader) better this way.

However, on occasion, I close the IDataReader in the presentation layer,
after binding.
I'd put this in a finally statement to make sure its runs.

IDataReader idr = null;
try
{
idr = Something.GetReader();
GridView1.DataSource = idr;
GridView1.DataBind();
}
finally
{
if (null!=idr)
{
idr.Close();
}
}
is typical code in that arena.


"rodchar" <ro*****@discussions.microsoft.comwrote in message
news:8F**********************************@microsof t.com...
hey all,
how do you resolve this problem?

i have a public procedure in my DataAccessLayer that gets a SqlDataReader

how do i close the reader from inside the DataAccessLayer if I'm returning
the reader to get bound to my GridView? Any code after my return will be
unreachable.

thanks,
rodchar


Apr 24 '07 #4

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

Similar topics

6
by: Hamed | last post by:
Hello I have employed as a developer in a software company that its team uses FoxPro / VB 6.0 / VC++ 6.0 as the developing tools and newly is going to migrate to VS.NET. There is a project...
0
by: sedefo | last post by:
I ran into this Microsoft Patterns & Practices Enterprise Library while i was researching how i can write a database independent data access layer. In my company we already use Data Access...
1
by: Stephen | last post by:
I'm having terrible trouble working out where I have gone wrong in my code below. In the data layer I have two methods and i'm basically returning a command and using it and then returning a...
4
by: Oyvind | last post by:
I'm working on a Windows forms/C# database application. My background is 6-7 years of VB 4 - 6, MS Access, VC++, mixed in with a lot of T-SQL and MS SQL Server in general and some OOA/OOD. ...
1
by: Stephen | last post by:
I'm having terrible trouble working out where I have gone wrong in my code below. In the data layer I have two methods and i'm basically returning a command and using it and then returning a...
1
by: Johann Blake | last post by:
I am looking for a good solution on how to implement data access in an application so that there is a clean separation between the data access layer, the business layer and the GUI layer. I am...
2
by: headware | last post by:
I'm relatively new to ASP.NET and ADO.NET, but I have a basic design question regarding the use of web services and APS.NET applications. Right now we have an application that uses web services to...
4
by: pratham | last post by:
Hi! I'm making a database application and i heard from a friend that it is more proffecional and easy to do this with bussines objects. Can anyone tell me where i can find more info on bussines...
1
by: dotnetnovice | last post by:
Hi everybody... I want to show data in the data gridview placed in my form after getting data from my data access layer class through a class... Here is my code in the data access layer class....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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?
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...

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.