By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,950 Members | 986 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,950 IT Pros & Developers. It's quick & easy.

VB.Net Datareader skips first record in results

P: n/a
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 (id******@msn.com)
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Yes, the Read method moves you to the next record, but

"rob" <id******@msn.com> wrote in message
news:e7**************************@posting.google.c om...
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?
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 (id******@msn.com)

Jul 19 '05 #2

P: n/a
rob
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
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.