Click here to Skip to main content
15,031,361 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
can we store datareader/dataset in session.if yes which is better and faster

You can store both in session. Storing DataReader in session is of no use, IMHO, since it gives only one time access.

You can store DataSet too but only if it is not too huge.
Do not store a DataReader in session. A DataReader is only of use for forward only iteration of a query response. As such, it must be open. If you store an open DataReader in session, the connection ( as scarce resource) will be held by the DataReader.

Read the DataReader into some data structure, such as a DataReader, before storing in session.

Now comes the next problem, if the data you are storing is large, each user session could store this data.

I would recomment using the Cache instead. You can set a timeout on how long it is stored and you could share the data between users if they all need the same result for the same query parameters.
Assuming your site will be used by more than 3 people, the approach taken by competent developers would be to never use the session if at all possible. Put an id on the query string and use that to read the data from your data source. This means you can bookmark pages and they will work out how to load themselves with the data that was bookmarked. If you MUST use the session, use it to store an id, and still read it from the data source.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900