While objdr.read() problem

P

Problematic coder

Dim objdr As Data.OracleClient.OracleDataReader = Nothing
Dim objcnn As Data.OracleClient.OracleConnection = Nothing
Dim objcom As Data.OracleClient.OracleCommand
objcom = New
Data.OracleClient.OracleCommand(BuildSQL("Notes", id), objcnn)
objdr = objcom.ExecuteReader
objdr.Read()
If objdr.HasRows Then 'Everything OK so far
While objdr.Read() 'Jumps straight to End If here
'do something, it never gets here
End While
Else
'do something else
End If

I am using the code above, it passes through "If objdr.HasRows Then"
so obviously has rows, however as soon as it gets to the next line it
jumps to "End If"

I also tried it using "Do while objdr.read() - Loop" and got the same
behavour, I don't understand why it won't loop through the results,
there is definately at least one row, but it needs to be able to
handle several rows, hence the loop
 
K

Kevin Spencer

What makes you so certain it has any rows?

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
Networking Components, Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Problematic said:
Dim objdr As Data.OracleClient.OracleDataReader = Nothing
Dim objcnn As Data.OracleClient.OracleConnection = Nothing
Dim objcom As Data.OracleClient.OracleCommand
objcom = New
Data.OracleClient.OracleCommand(BuildSQL("Notes", id), objcnn)
objdr = objcom.ExecuteReader
objdr.Read()
If objdr.HasRows Then 'Everything OK so far
While objdr.Read() 'Jumps straight to End If here
'do something, it never gets here
End While
Else
'do something else
End If

I am using the code above, it passes through "If objdr.HasRows Then"
so obviously has rows, however as soon as it gets to the next line it
jumps to "End If"

I also tried it using "Do while objdr.read() - Loop" and got the same
behavour, I don't understand why it won't loop through the results,
there is definately at least one row, but it needs to be able to
handle several rows, hence the loop

In this case it has one row, and you skip over that row.

THe first call to Read makes the first row accessible throught the
reader, but you don't do anything with that row, instead you go to the
loop where you start by skipping to the next row and do something with
that. That means that you always skip the first row, and if there is
only one row, you don't do anything more.

Remove the first call to Read.
 
P

Patrice

You read the row before testing HasRows and entering the loop. Removing this
extra read should solve the problem....
 
P

Problematic coder

What makes you so certain it has any rows?

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
Networking Components, Controls, much more.
DSI PrintManager, Miradyne Component Libraries:http://www.miradyne.net








- Show quoted text -

Two reasons, one I confirmed it with a sql query and two the fact that
it doesn't go to "else"
 
P

Problematic coder

In this case it has one row, and you skip over that row.

THe first call to Read makes the first row accessible throught the
reader, but you don't do anything with that row, instead you go to the
loop where you start by skipping to the next row and do something with
that. That means that you always skip the first row, and if there is
only one row, you don't do anything more.

Remove the first call to Read.

--
Göran Andersson
_____http://www.guffa.com- Hide quoted text -

- Show quoted text -

I see what you are saying but that doesn't seem to be the case, to
test this I comented out the first call and was left with this:

objdr.Read()
While objdr.Read() ' It jumped from here to the
next line of code
'do something, it never gets here
End While
'Next line of code

You are on the right lines, the next test I did was add another
record, and the loop now works, however only the second record shows
up, ie it only loops once, if there is only one record it doesn't loop
at all, so it is ignoring the first record.

Here is the code inside the loop:

textBox.text = textBox.text & objdr.item("column_name") & "<br>"

So, any ideas how to make it also include the first line?

Thanks for feedback already given
 
P

Problematic coder

I see what you are saying but that doesn't seem to be the case, to
test this I comented out the first call and was left with this:

objdr.Read()
While objdr.Read() ' It jumped from here to the
next line of code
'do something, it never gets here
End While
'Next line of code

You are on the right lines, the next test I did was add another
record, and the loop now works, however only the second record shows
up, ie it only loops once, if there is only one record it doesn't loop
at all, so it is ignoring the first record.

Here is the code inside the loop:

textBox.text = textBox.text & objdr.item("column_name") & "<br>"

So, any ideas how to make it also include the first line?

Thanks for feedback already given- Hide quoted text -

- Show quoted text -

Got it, the answer was given above and I misread it - the additional
objdr.read() was the problem, I had assumed I had to do that before
testing for hasrows - apparently not - deleted that and all is good -
thanks again
 
K

Kevin Spencer

Glad you got it solved!

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
Networking Components, Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 

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

Top