Missing first record - datareader.read

stustarz

Well-known member
Joined
Jan 10, 2003
Messages
246
Location
Earth
I am using:

Code:
If DataRead.Read() Then
                While DataRead.Read
                    cbCompList.Items.Add(DataRead.Item("competition"))
                End While
End If

The problem is when it runs, the datareader populates the combo box but skips the first record in the table. Does anyone know a way I can have all records returned.

Thanks
 
Try

Try getting rid of the line with the If statement

In other words try just:

While DataRead.Read
cbCompList.Items.Add(DataRead.Item("competition"))
End While
 
That will work, and maybe I never explained fully enough - the reason I have the IF statement is to check for EOF. The While statement doesnt, as far as I can see, check for this. So if my table has no records it will return 0 records but I want a way of catching this and displaying a msgbox e.t.c.
 
Then possibly try:

Code:
If DataRead.Read() Then
                cbCompList.Items.Add(DataRead.Item("competition"))
                While DataRead.Read
                    cbCompList.Items.Add(DataRead.Item("competition"))
                End While
End If
 
bear in mind every time anything uses the dataread.read the pointer for the data will move forward one record.

Andy
 
You should use the following. You dont need the "If"
Code:
While DataRead.Read
    cbCompList.Items.Add(DataRead.Item("competition"))
End While

According to the Help, the DataReader begins before the first record. The first call to Read advances the pointer to the first record but only if it exists. If it doesnt the Read method returns false and youll never get in your While loop.

-Nerseus
 
Back
Top