Solved

Closing Datareader

Posted on 2003-11-14
10
997 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now