sqlDataReader skipping first row in loop

  • Thread starter branton ellerbee
  • Start date
B

branton ellerbee

I am looping through a datareader and building a table. However, no matter
what the resultset, the datareader skips the first row of data, then builds
the rest of the resultset.

I have seen this occur in a lot of different places since using .Net.

HAs anyone else found this problem before?
 
K

Ken Cox [Microsoft MVP]

Can you show the code? It sounds like you are calling/using something in the
wrong place.
 
G

Guest

hey

are you looping between the following code

while(DataReader1.Read




common mistake we make is, use

if(DataReader1.Read

and write the looping here


the problem is, if you use "if" then the reader's position is skipped to the first record and thereafter, if you d
some printing or reading stuff, it will read from the second record

hope it helps




----- branton ellerbee wrote: ----

I am looping through a datareader and building a table. However, no matte
what the resultset, the datareader skips the first row of data, then build
the rest of the resultset

I have seen this occur in a lot of different places since using .Net

HAs anyone else found this problem before
 
T

Teemu Keiski

If it is because of reading the first record as was explained, you can try a
loop like this:

if(SqlDataReader.Read)
{
do
{
//Do processing of the individual row here
}
while(SqlDataReader.Read);
}
else
{
//NO rows in SqlDataReader
}

With this you can check the existence of rows and still loop them one by
one(including the first one)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top