Solved

Closing Datareader

Posted on 2003-11-14
10
1,000 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
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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…

807 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