Datareader not working

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
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
 
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
 
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
 
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
 
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
 
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.
 
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
 
Thank you the data reader is working correctly now, could you explain why it
was not working before?

Scott Richards
 
Back
Top