Datareader not working

G

Guest

Hi I am getting a exception while using a datareader here is the code

Me.SqlConnection1.Open()
Me.SqlReservationCheck.Parameters("@Ref").Value = Ref
Dim Reader As SqlClient.SqlDataReader = _
Me.SqlReservationCheck.ExecuteReader()
Dim I As Integer()
Dim Index As Integer = 0
If Reader.HasRows Then
While Reader.Read()
ReDim Preserve I(Index)
I(Index) = Reader.Item(0)
Index += 1
End While
End If
Reader.Close()
Me.SqlConnection1.Close()

When I execute the While Reader.Read() command the reader gives false
But the If Reader.HasRows Then returns true could someone help me with this
 
J

John Saunders

Scott Richards said:
Hi I am getting a exception while using a datareader here is the code

Which exception do you get, and what line does it happen on?

I really don't know how you expect anyone to be able to help you given the
small amount of information you've provided.

John Saunders
 
G

Guest

Sorry for the lack of information John, by the time I wrote this I had spent
5 hours researching the problem & was quite frustrated & tired.

The problem is that when I run the datareader.read command the loop does not
intiate because the command gives false

I have checked it by running the datareader.read command without without the
while loop & the information is in the reader, what do you suggest I do?

From all the reading on the subject I have done I have found nothing like
this.

Thank you for your help
 
J

John Saunders

Scott Richards said:
Sorry for the lack of information John, by the time I wrote this I had
spent
5 hours researching the problem & was quite frustrated & tired.

The problem is that when I run the datareader.read command the loop does
not
intiate because the command gives false

In a case like this, I would first assume that the DataReader is working
correctly. If you start with that assumption, you're left to wonder what you
could possibly have done to make a correctly-functioning DataReader to
return false right away. For instance, are you certain that your query is
correct? If it requires parameters, are they correct? For instance, maybe
you've got a string parameter which has trailing spaces.

John Saunders
 
G

Guest

Hi John I have checked that the query is returning the correct data by
running the program without the while loop. Is it possible that my datareader
isn't functioning correctly? If so what would you sugest I do. I can find no
other reason why the datareader is returning false straight away.

Scott Richards
 
J

John Saunders

Scott Richards said:
Hi John I have checked that the query is returning the correct data by
running the program without the while loop. Is it possible that my
datareader
isn't functioning correctly? If so what would you sugest I do. I can find
no
other reason why the datareader is returning false straight away.

How did running the program without a while loop determine that the data
were correct?

John Saunders
 
G

Guest

I put a watch on I(0) & got the number that I expected.
I also put a watch on the reader & the next number was in their too.
 
J

John Saunders

Scott Richards said:
I put a watch on I(0) & got the number that I expected.
I also put a watch on the reader & the next number was in their too.

Thank you.

I suggest that you next add a couple of Read calls, not in a loop, and see
if they get the first two records.

John Saunders
 
J

John Saunders

Scott Richards said:
Thats works fine perfictly fine

Giving me the expected results

Ok, since a couple of reads work, try a simple while loop.

John Saunders
 
G

Guest

Thank you the data reader is working correctly now, could you explain why it
was not working before?

Scott Richards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top