VB.Net Datareader skips first record in results

  • Thread starter Thread starter rob
  • Start date Start date
R

rob

I'm having some trouble with this..

My VB datareader seems to skip the first record in the reader. I've
looked at my query in enterprise manager to confirm what I am suppose
to receive back, and what the data reader starts at is the second
record in the reader.

I've done read test to make sure there is a result in the datareader
("If rd.read Then blah") but does that advance the row position? Can
I set it back?

Here's some code from the application. It outputs to a listview
control.


Do While objRD.Read

lstName.Items.Add(objRD("CustID"))
lstName.Items(I).SubItems.Add(objRD("LastName"))
lstName.Items(I).SubItems.Add(objRD("FirstName"))
lstName.Items(I).SubItems.Add(objRD("Phone"))

I += 1

Loop

I've made sure my query is OK, I made sure the data reader was closed
and opened properly-- I dont know what else to check. I dont want to
use a data grid control if possible.

any thoughts here are appreciated-- thanks

Rob ([email protected])
 
Yes, the Read method moves you to the next record, but

rob said:
I'm having some trouble with this..

My VB datareader seems to skip the first record in the reader. I've
looked at my query in enterprise manager to confirm what I am suppose
to receive back, and what the data reader starts at is the second
record in the reader.

No, the DataReader initializes itself PRIOR to the first record, so you need
to call Read BEFORE accessing any data.

See:
ms-help://MS.VSCC/MS.MSDNQTR.2002OCT.1033/cpref/html/frlrfsystemdataoledbole
dbdatareaderclassreadtopic.htm
I've done read test to make sure there is a result in the datareader
("If rd.read Then blah") but does that advance the row position? Can
I set it back?

A DataReader is the only ADO.NET object that provides connected access to
your data, it's read only and FORWARD only -- no going back unless you
re-initialize the DR.

Here's some code from the application. It outputs to a listview
control.


Do While objRD.Read

lstName.Items.Add(objRD("CustID"))
lstName.Items(I).SubItems.Add(objRD("LastName"))
lstName.Items(I).SubItems.Add(objRD("FirstName"))
lstName.Items(I).SubItems.Add(objRD("Phone"))

I += 1

Loop

Are you sure your variable (I) is initialized at zero prior to this loop
since all collections are zero-based?
Have you considered binding the listbox to the DataReader instead of
populating it manually?
 
Scott- thanks for your help. My "I" wasnt initialized to zero AND I
had an extra read test in there. That fixed it up.

I didnt use data binding b/c I like to see what's in the code-- I dont
know-- I'm still new to VB.net, maybe I'll change my mind in a while.

Thanks for the help though!

Rob
 
Back
Top