473,721 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Should I use only one public DataReader, or multiple?

In a typical ASP .NET Web Application (website), I'm currently using a class
where I declare some public static objects. For example there's the place
where I initialize the SqlConnection.
Also there's the place where I declare one public static DataReader, but I'm
not sure this is the best way to do it. I use that DataReader in all the
webforms and user controls, whenever I have to read data from the db.

So, should I do it this way or should I create a new DataReader each time I
need it?

By the way, I started to ask myself this questions since I'm experiencing
different problems with using a public DataReader, like for example "There
is already an open DataReader associated with this Connection which must be
closed first" errors, even if I'm closing all of them.

Thanks for your support.
Nov 18 '05 #1
2 1575
In your situation, you should *always* create a new instance of the
datareader. I would also be using a new instance of the connection class
too, or at least make sure you have it closed asap after your DB operations.
Because you have the current datareader static, its single instance can be
used many many concurrent threads, and can experience serious concurrency
issues. You either need to make it thread safe by serialising calls to this
static object (bad for performance and you have to code for this) or simply
create a new instance each time. This way, you can ensure each conurrent
thread has its own copy.

--
- Paul Glavich
Microsoft MVP - ASP.NET
"Andrei Pociu" <an**********@g eekpedia.com> wrote in message
news:ez******** ******@TK2MSFTN GP11.phx.gbl...
In a typical ASP .NET Web Application (website), I'm currently using a class where I declare some public static objects. For example there's the place
where I initialize the SqlConnection.
Also there's the place where I declare one public static DataReader, but I'm not sure this is the best way to do it. I use that DataReader in all the
webforms and user controls, whenever I have to read data from the db.

So, should I do it this way or should I create a new DataReader each time I need it?

By the way, I started to ask myself this questions since I'm experiencing
different problems with using a public DataReader, like for example "There
is already an open DataReader associated with this Connection which must be closed first" errors, even if I'm closing all of them.

Thanks for your support.

Nov 18 '05 #2
"Because you have the current datareader static, its single instance can be
used many many concurrent threads, and can experience serious concurrency
issues."
That's what I was thinking, also. For now I'll use different instances of
the DataReader. I think I can manage the SQLConnection (opening and closing)
but if I experience problems with it I'll use new instances for it too.

Thank you Paul.

"Paul Glavich [MVP - ASP.NET]" <gl**@aspallian ce.com-NOSPAM> wrote in
message news:OD******** *****@TK2MSFTNG P12.phx.gbl... In your situation, you should *always* create a new instance of the
datareader. I would also be using a new instance of the connection class
too, or at least make sure you have it closed asap after your DB
operations.
Because you have the current datareader static, its single instance can be
used many many concurrent threads, and can experience serious concurrency
issues. You either need to make it thread safe by serialising calls to
this
static object (bad for performance and you have to code for this) or
simply
create a new instance each time. This way, you can ensure each conurrent
thread has its own copy.

--
- Paul Glavich
Microsoft MVP - ASP.NET
"Andrei Pociu" <an**********@g eekpedia.com> wrote in message
news:ez******** ******@TK2MSFTN GP11.phx.gbl...
In a typical ASP .NET Web Application (website), I'm currently using a

class
where I declare some public static objects. For example there's the place
where I initialize the SqlConnection.
Also there's the place where I declare one public static DataReader, but

I'm
not sure this is the best way to do it. I use that DataReader in all the
webforms and user controls, whenever I have to read data from the db.

So, should I do it this way or should I create a new DataReader each time

I
need it?

By the way, I started to ask myself this questions since I'm experiencing
different problems with using a public DataReader, like for example
"There
is already an open DataReader associated with this Connection which must

be
closed first" errors, even if I'm closing all of them.

Thanks for your support.


Nov 18 '05 #3

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

Similar topics

2
6522
by: Islam Elkhayat | last post by:
In my C# Web Application I need to fill a textbox with DataReader and use a Next Button to view next value... I created the Datareader in the Page_Load but everytime it get result of the First row. If i use a While or For loop i get result of the last row... How can i check "PostBack" to do so our if there is another way to fill the textbox and use the Next button to see next result rather than DataReader?? thanx
12
467
by: Thomas Scheiderich | last post by:
I have 2 dropdowns that are exactly the same and I don't want to re-read for each. Is there a way to do something like below where I read the data, bind to depCity1 and then bind to destCity2. I find that the second one always is blank. I assume this is because the DataReader is read-forward only. ******************************************************** connect.open() cmdSelect = New OleDbCommand("exec populateCities 'North...
4
3054
by: Maziar Aflatoun | last post by:
Hi everyone, I am working on the 'Delete' section of my program. What I need to do is query my database and for every ID that it finds I want to remove a file+ID.jpg from my file folder and update another table with that information. However, I'm getting the following error message. There is already an open DataReader associated with this Connection which must be closed first. Description: An unhandled exception occurred during the...
2
1731
by: Patreek | last post by:
Hi, I'm writing my first real asp.net app at my job, and I'd like opinions please. In my classic ASP apps that I've written, I'd often have separate files for retreiving data and returning the data to other pages that would call it. I'd return the data as arrays so that I wouldn't have to have my DB connections open all the time while the pages ran. Now in .net, it seems like I have to choose between using a SQldatareader or a...
3
1141
by: bdwgarth | last post by:
I am trying to open a new web page for each item returned by the DataReader. I am using JavaScript to open each new window, and using a session variable to pass the DataReader Item value to the newly created page. The problem I am running into is all the pages are opening with the same value, which is the last item returned by the DataReader. Here is my code: Session("BatchNumber") = sqldr.Item("q_Batch") Response.Write("<script...
1
1666
by: Ivan Weiss | last post by:
Hey all, I have the following code to populate a ListView control from my Access database. The listview is displaying a list of saved projects that the user will be able to open, edit, or delete to work on. I know that the DataReader is more efficient and faster than a dataset, but I was not able to figure out how to implement it. Here is the code I have now which works: Private Sub frmProjects_Load(ByVal sender As System.Object,...
6
1941
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 ?
3
1512
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 with Sub-Tables and Summaries, etc., etc. Here's my question: Is there a way to use any of the ASP.NET 2.0 Data Controls - or any other mechanism - to display the DataReader results without having to define all of the columns and all that in...
20
7211
by: fniles | last post by:
I am using VB.NET 2003, SQL 2000, and SqlDataReader. As I read data from tblA, I want to populate tblB. I use SQLDataReader for both tables. I do not use thread. When I ExecuteReader on tblB, I get the error "There is already an open DataReader associated with this Connection which must be closed first." How can I fix this error ? For each DataReader, do I want to open and close the connection (in this case adoCon) to avoid this error ?...
0
8834
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
8727
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9127
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9056
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5973
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4748
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3186
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2569
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2127
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.