Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

How to read records one bye one from sqldatareader by clicking Next and Previous button?

Thanks in advance.
Posted
Comments
Mehdi Gholam 23-Sep-11 7:50am    
It's not advisable to keep a connection open like that, what have you tried?

Note: datareader is f?arward only
But, even if it wasn't, you need to either store the results of your query in some kind of state, between requests, or requery the database to get the previous or the next result.
 
Share this answer
 
Hi Ramakrishna,

Load your collection results into a object(genericlist or the likes) that you would like to store the disconnected data in. From there databind the control(s) to the object, increasing or decreasing your object(list) index with next/previous.

Riaan
 
Share this answer
 
v2
Don't do that. Since datareader works in a connected mode, as long as the user is browsing the records with the next and previous buttons, the connenction with the SQL server will be active. Later on the SQL server will reject the new connections once the connection limit exceeds.

Again, datareader will not allow you to read a previous record.It will allow only the next record.

Instead use a dataset and read each records on next and previous button.
 
Share this answer
 
v2
Hi Ramakrishna,
datareader is useful only in the case where you need a limit amount data and more over it is active connection oriented, that is it will retain the connection unless and until we close it.. instead of that 1 we can use disconnected data binders such as DataAdapters,
dataadapter will retrieves the data completely and closes connections automatically...so we can use that one as our own object(Managed code).

even though if you want that one
then
Where cmd is an object of SqlCommand,
<pre lang="c#">
SqlDataReader dr=cmd.ExecuteReader();
dr.Read();//use methode to Advance the System.Data.SqlClient.SqlDataReader to the next record.
//and returns bool type(whether next record exist or not).
 
Share this answer
 
v2

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