Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1008
  • Last Modified:

Closing Datareader

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
kapil_mohta
Asked:
kapil_mohta
1 Solution
 
mmarinovCommented:
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
 
iboutchkineCommented:
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
 
Joe_GriffithCommented:
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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
DranizzCommented:
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
 
kapil_mohtaAuthor Commented:
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
 
kapil_mohtaAuthor Commented:
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
 
DranizzCommented:
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
 
kapil_mohtaAuthor Commented:
thanks Dranizz
Could you plz suggest some links or send me some articles on ado.net.

Kapil
Email : mohta_kapil@hotmail.com
0
 
DranizzCommented:
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
 
DranizzCommented:
You have to give points if nswer is good
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now