DataReader skips records??

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello all -

I'm at wits end with this and I'm sure I'm doing something wrong but can't
figure out why. I have this loop below.

'OldPlayersReader skips (1st to 3rd to 5th)
While pastDeadlineReader.Read And oldPlayersReader.Read
If oldPlayersReader Is Nothing Then
'If nothing set to blank.
strName = ""
Else
'Set value from OldPlayersReader to strName
strName = Convert.ToInt32(oldPlayersReader.GetValue(1))
End If
End While

So basically I have 2 readers that need to stay in sync with each other and
move along at the same time. Both will have 5 records in them always but I
need to do comparative checks in case values from OldPlayersReader need to be
used. The first time it goes through this loop, the first records are
compared but when it comes back up again, the first (pastDeadlineReader)
reader is on the 2nd record and the OldPlayersReader is on the 3rd record,
past the 2nd record.

What am I doing wrong?

Much thanks,
MN
 
I would change: If oldPlayersReader Is Nothing to If
oldPlayersReader.HasRows checking for nothing checks to see if the object
exists, which of course it does because if it didn't, the line just previous
would cause an exception (you can't read from an object that is nothing).

I think your problem lies with the If condition. Read advances a DataReader
to its next row and you have 2 Reads on that line, but it seems as if only
one is advancing properly. Have you tried this:

If pastDeadlineReader.HasRows And oldPlayersReader.HasRows Then
Do While oldPlayersReader.Read
pastDeadlineReader.Read 'Keep the 2 readers in synch
'Set value from OldPlayersReader to strName
strName = Convert.ToInt32(oldPlayersReader.Item(1))
End While
 
Hi Sahil -

Thanks for responding. I am using ADO.Net 1.1. What's odd though was after
looking at some research, I modified my "While" to "Do While" and the 2nd
reader doesn't skip now and stays in sync. Any reasons for that behavior as
it seems odd that it would be dependent on my condition setup for my loop.

Thanks,
MN
 
Hi Scott -

Thanks for taking the time to respond. I do agree with your statements
about the Is Nothing check but did forget to add this to the equation.
Sometimes, oldPlayersReader may not exist. Therefore, I think this may be
the better option below.

Do While pastDeadlineReader.Read
If oldPlayersReader.HasRows = True Then
oldPlayersReader.Read()
End If
......
Loop

Only because it's possible it doesn't exist and therefore would bomb trying
to read when there's no data.

That way it would insure they would stay in sync. Is there any downsides to
that option?

Much thanks for everyone's advice,
MN
 
Yes, there is a downside to your code. If the reader has no rows (records)
then you will cause an exception on the Do While line and never get to your
If...HasRows check because the Read in the Do While will attempt to advance
the reader to the first record and if there are no records, you get an
exception. You should check for rows before anything else, which is why I
have that check surrounding the whole Do...Loop.

Also (just FYI), you don't need to check HasRows for True, you can just say
If reader.HasRows. This is true (no pun intended) for any Boolean since an
IF statement is always checking for the True condition anyway.
 
Hi Scott,

Your comments are well taken. That reader that we're speaking about,
pastDeadlineReader, will always have data in it though so I don't need to
check for HasRows because it's known already. It was only the
oldPlayersReader that I was concerned about, which I'm doing that similar IF
condition as you explained to do on the outside of the entire Loop itself.

Thanks again for your input and advice on this matter. Certainly appreciated.
MN
 
HTH - Good Luck!

MN said:
Hi Scott,

Your comments are well taken. That reader that we're speaking about,
pastDeadlineReader, will always have data in it though so I don't need to
check for HasRows because it's known already. It was only the
oldPlayersReader that I was concerned about, which I'm doing that similar
IF
condition as you explained to do on the outside of the entire Loop itself.

Thanks again for your input and advice on this matter. Certainly
appreciated.
MN
 
Back
Top