Solved

Closing Datareader

Posted on 2003-11-14
10
1,003 Views
Last Modified: 2009-12-16
If I forget to close a reader with the connecion and try to open another reader somewere else (with the same connection object), i get the following error.

There is already an open DataReader associated with this Connection which must be closed first.

I would like to know that is there any way of knowing if there is any reader open with the connection , and is yes , how to close it to proceede.

regards

Kapil
0
Comment
Question by:kapil_mohta
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 28

Expert Comment

by:mmarinov
ID: 9747019
You can check the state of the connection object but not if there is a reader object connected with this connection and that is opened.

Before exit the function just close the reader and after that open the new one
you can check if the connection is open like this

if (conn.State == ConnectionState.Open) conn.Close();

B..G
0
 
LVL 28

Expert Comment

by:iboutchkine
ID: 9747482
You can check if datareader is empty. And if it is you can close it
If dr.Read Then
    'datareader not empty, perform something here on dr before it advances
to the next line
    While dr.Read()
        'datareader till not empty, keep looping until eof
    End While
Else
    'datareader is empty
     dr.Close
End If
0
 
LVL 7

Accepted Solution

by:
Joe_Griffith earned 50 total points
ID: 9750266
I think iboutchkine's method will work but it is not very elegant and with a lot of data it might take a while.  I think the problem is that you are trying to find a global answer to the problem of not writing clean code in the first place.

The secret is to always close the datareader when you are done with it.  I've run into this error and its been my fault every time.  In a large program it can be pretty difficult to find out where you left it open and I do wish there were someway to backtrack into the code that opened the last datareader.
0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 8

Expert Comment

by:Dranizz
ID: 9751032
I agreed with Joe_Griffith, since you have to close the DataReader before using the connection in any other way, there is no other way to proceed. DataReader whould be use for "Fill-in" situations such as enumeration or list filling. To use data of a database with less time programming and fats data manipulation, it is recommanded to use the dataset wich is and disconnected representation of you database data and schema.

Good luck!
0
 

Author Comment

by:kapil_mohta
ID: 9753051
I suppose u r right , but at time it becomes so irritating when u hav to search for the open datareader some were in the entire program , considering all the steps u took to reach the place and all the conditional constructs ur code might hav been through. it so happens that if from one function , depending on a condition , u call another function that also needs to do some database operation, u need to close the reader first even if the first function still needs the reader.  I generally tend to write related things together and so write all these statements (closing the reader etc) at the end of the sub/function and don't find it easy finding all the places to paste the code every at nook & corner

Anyways, i suppose i hav to get aquinted with this and try to stick to "CLEAN" coding

Thanks

Kapil
0
 

Author Comment

by:kapil_mohta
ID: 9753052
I suppose u r right , but at time it becomes so irritating when u hav to search for the open datareader some were in the entire program , considering all the steps u took to reach the place and all the conditional constructs ur code might hav been through. it so happens that if from one function , depending on a condition , u call another function that also needs to do some database operation, u need to close the reader first even if the first function still needs the reader.  I generally tend to write related things together and so write all these statements (closing the reader etc) at the end of the sub/function and don't find it easy finding all the places to paste the code every at nook & corner

Anyways, i suppose i hav to get aquinted with this and try to stick to "CLEAN" coding

Thanks

Kapil
0
 
LVL 8

Expert Comment

by:Dranizz
ID: 9755215
I understand some of your problems, I met the same when I first started to program with ADO.NET. Like I said, the main puspose of a datareader is reading straigth foward data to read or fill some control with it. You should use a dataset or datatable and pass throught those records to make other operations on the database possible. You can use many methods of the dataset/reader and datarow's class. Like the select method and the find method, and also you can use the dataview to pick up the recors you want, than use those recors in your other calls.

That should be clean enought and easy to code. I suggest that you read some articles about ADO.NET, that could be usefull and guiding.

If you have any questions, ask, I will try my best to help you.
0
 

Author Comment

by:kapil_mohta
ID: 9761596
thanks Dranizz
Could you plz suggest some links or send me some articles on ado.net.

Kapil
Email : mohta_kapil@hotmail.com
0
 
LVL 8

Expert Comment

by:Dranizz
ID: 9763213
Well, I don't know any articles worth it, I learned it by myself and with books. I believe Microsoft MSDN is a start and will help you a little. Learning class and checking code example. I suggest some books, those are pretty neat and helpfull.

http://www.microsoft.com/MSPress/books/4825.asp
http://www.murach.com/books/adon/index.htm

0
 
LVL 8

Expert Comment

by:Dranizz
ID: 9766340
You have to give points if nswer is good
0

Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question