Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Closing Datareader

Posted on 2003-11-14
10
Medium Priority
?
1,005 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 150 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
Quick Cloud Training

Looking for some quick training on the cloud in 2 hours or less? Check out these how-to guides in AWS, Linux, OpenStack, Azure, and more!

 
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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

670 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