PC Review


Reply
Thread Tools Rate Thread

DataReader skips records??

 
 
=?Utf-8?B?TU4=?=
Guest
Posts: n/a
 
      23rd Nov 2004
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
 
Reply With Quote
 
 
 
 
Sahil Malik
Guest
Posts: n/a
 
      23rd Nov 2004
It is hard to answer this question without looking at the surrounding
code.Is this ADO.NET 1.1? Because if it is not, you can't have two
datareaders coming from the same connection.
What are the chances that you might be able to replicate the below in a
simple ConsoleApp, and paste that on the newsgroup here.

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik
http://blogs.apress.com/authors.php?author=Sahil Malik


"MN" <(E-Mail Removed)> wrote in message
news:1139760F-7B7F-475D-802A-(E-Mail Removed)...
> 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



 
Reply With Quote
 
Scott M.
Guest
Posts: n/a
 
      23rd Nov 2004
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


"MN" <(E-Mail Removed)> wrote in message
news:1139760F-7B7F-475D-802A-(E-Mail Removed)...
> 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



 
Reply With Quote
 
=?Utf-8?B?TU4=?=
Guest
Posts: n/a
 
      23rd Nov 2004
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

"Sahil Malik" wrote:

> It is hard to answer this question without looking at the surrounding
> code.Is this ADO.NET 1.1? Because if it is not, you can't have two
> datareaders coming from the same connection.
> What are the chances that you might be able to replicate the below in a
> simple ConsoleApp, and paste that on the newsgroup here.
>
> - Sahil Malik
> http://dotnetjunkies.com/weblog/sahilmalik
> http://blogs.apress.com/authors.php?author=Sahil Malik
>
>
> "MN" <(E-Mail Removed)> wrote in message
> news:1139760F-7B7F-475D-802A-(E-Mail Removed)...
> > 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

>
>
>

 
Reply With Quote
 
=?Utf-8?B?TU4=?=
Guest
Posts: n/a
 
      23rd Nov 2004
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


"Scott M." wrote:

> 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
>
>
> "MN" <(E-Mail Removed)> wrote in message
> news:1139760F-7B7F-475D-802A-(E-Mail Removed)...
> > 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

>
>
>

 
Reply With Quote
 
Scott M.
Guest
Posts: n/a
 
      23rd Nov 2004
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.


"MN" <(E-Mail Removed)> wrote in message
news:39152776-7D02-416E-9442-(E-Mail Removed)...
> 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
>
>
> "Scott M." wrote:
>
>> 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
>>
>>
>> "MN" <(E-Mail Removed)> wrote in message
>> news:1139760F-7B7F-475D-802A-(E-Mail Removed)...
>> > 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

>>
>>
>>



 
Reply With Quote
 
=?Utf-8?B?TU4=?=
Guest
Posts: n/a
 
      23rd Nov 2004
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

"Scott M." wrote:

> 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.
>
>
> "MN" <(E-Mail Removed)> wrote in message
> news:39152776-7D02-416E-9442-(E-Mail Removed)...
> > 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
> >
> >
> > "Scott M." wrote:
> >
> >> 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
> >>
> >>
> >> "MN" <(E-Mail Removed)> wrote in message
> >> news:1139760F-7B7F-475D-802A-(E-Mail Removed)...
> >> > 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
> >>
> >>
> >>

>
>
>

 
Reply With Quote
 
Scott M.
Guest
Posts: n/a
 
      23rd Nov 2004
HTH - Good Luck!

"MN" <(E-Mail Removed)> wrote in message
news:61025C3A-7781-4491-A125-(E-Mail Removed)...
> 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
>
> "Scott M." wrote:
>
>> 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.
>>
>>
>> "MN" <(E-Mail Removed)> wrote in message
>> news:39152776-7D02-416E-9442-(E-Mail Removed)...
>> > 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
>> >
>> >
>> > "Scott M." wrote:
>> >
>> >> 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
>> >>
>> >>
>> >> "MN" <(E-Mail Removed)> wrote in message
>> >> news:1139760F-7B7F-475D-802A-(E-Mail Removed)...
>> >> > 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
>> >>
>> >>
>> >>

>>
>>
>>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Mail Merge Skips Some Records kmordecai Microsoft Word Document Management 1 11th Feb 2010 11:13 PM
AutoNumber skips 36,000,000 records Roach Microsoft Access Forms 3 24th Feb 2005 05:14 PM
Mail Merge from Outlook Skips Records Yvonne Microsoft Outlook Contacts 1 25th Oct 2003 01:37 AM
spool 32 - skips records Patrick Windows XP Print / Fax 2 16th Oct 2003 05:12 PM
VB.Net Datareader skips first record in results rob Microsoft Dot NET 2 3rd Sep 2003 06:03 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:52 AM.