Click here to Skip to main content
15,889,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi , I got Memory out Of Exception while download bulk reports . So i alter the code by using DATAREADER instead of DATASET . How to achieve below code in DataReader because am helpless to use any datatables & dataset in my coding.

What I have tried:

C#
if (dataSet.Tables[0].Rows[i + 1]["Sr No"].ToString() == dataSet.Tables[0].Rows[i]["Sr No"].ToString())
Posted
Updated 2-Aug-16 2:24am
v4
Comments
Tomas Takac 2-Aug-16 2:34am    
I don't understand, what is stopping you from using datareader["Sr No"]?
Vicky Siddharth 2-Aug-16 2:41am    
Hi Tomas. while(datareader.read()) { i)loop goes for the first time it takes 1 record from record set
ii) here i want to access the 2 record value .
}
how can we achieve ?
Karthik_Mahalingam 2-Aug-16 11:04am    
Always use  Reply   button to post comments/query to the concerned user, else the user wont get notified.
Vicky Siddharth 3-Aug-16 8:36am    
ok karthik
Karthik_Mahalingam 3-Aug-16 8:38am    
:)

A DataReader only works by reading forward through the data set: you can't specify a random access value to "skip" to a specific row.
The only way to access a row in a DataReader is to use a loop to read each row up to the point where you reach the row you want. But...you can't go "backward" to earlier rows once you have gone past them.
If you are having problems reading all the rows at once, then consider either "paging" the number of rows you read into a DataAdapter, or issue individual read requests for just the rows you are interested in.
 
Share this answer
 
DataReader has no random access. You need to "remember" the value from previous row.
C#
string previous = null;
string current = null;
bool isFirst = true;

while (datareader.Read())
{
    current = datareader["Sr No"].ToString();
    
    if (isFirst)
    {
        // skip the first row because we don't have anything to compare it to
        isFirst = false;
    }
    else if(current == previous)
    {
        // do your thing here
    }
    
    previous = current;
}
 
Share this answer
 

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