SQL Reader problem

  • Thread starter Thread starter Nikolay Petrov
  • Start date Start date
N

Nikolay Petrov

Hi,
I have the following code.

Dim RD As System.Data.SqlClient.SqlDataReader
Try 'Try2
OpenConnection()
RD = cmdSQL.ExecuteReader
While RD.Read = True
UserIDSQL = CInt(RD.Item("UserID"))
EncUserNameSQL = CStr(RD.Item("UserName"))
EncPasswordSQL = CStr(RD.Item("Password"))
PassExpirationDateSQL =
CDate(RD.Item("PasswordExpirationDate"))
IPAddressSQL = CStr(RD.Item("IPAddress"))
AllowOnlyThisIPAddress =
CBool(RD.Item("AllowOnlyThisIPAddress"))
AllowLoginFromWeb =
CBool(RD.Item("AllowLoginFromWeb"))
End While
Catch ex As Exception
msgbox(ex.Message)
Finally
CloseConnection()
End Try

I get some strange behavior.
When Run, it does not set values to variables in 'While' block.
When Debug it goes to line 'While RD.Read' and the next line executed is
'Finally'
Why?

TIA
 
Nikolay Petrov said:
Dim RD As System.Data.SqlClient.SqlDataReader
Try 'Try2
OpenConnection()
RD = cmdSQL.ExecuteReader
While RD.Read = True
UserIDSQL = CInt(RD.Item("UserID"))
EncUserNameSQL = CStr(RD.Item("UserName"))
EncPasswordSQL = CStr(RD.Item("Password"))
PassExpirationDateSQL =
CDate(RD.Item("PasswordExpirationDate"))
IPAddressSQL = CStr(RD.Item("IPAddress"))
AllowOnlyThisIPAddress =
CBool(RD.Item("AllowOnlyThisIPAddress"))
AllowLoginFromWeb =
CBool(RD.Item("AllowLoginFromWeb"))
End While
Catch ex As Exception
msgbox(ex.Message)
Finally
CloseConnection()
End Try

I get some strange behavior.
When Run, it does not set values to variables in 'While' block.
When Debug it goes to line 'While RD.Read' and the next line executed is
'Finally'
Why?

Maybe the 'Read' method returns 'False', then the 'While' loop is skipped
and the 'Finally' part is executed. 'Finally' will be executed even if no
exception was thrown.
 
Any idea why SQLDataReader returns false
I am using a stored procedure and I am sure it returns data.
 
Nicolay,

Than why not in your finallyblock
Messagebox.show(EncUserNameSQL)

Or something like that.

It is a little thing to test, it will not be the first time that people are
"sure".

Cor
 
The
While RD.Read is FALSE at the first loop
it nevers assign anything to variables

when run in server explorer the stored procedure retruns data.

how can i see the returned data from SQL while debuging?
 
As I wrote
When it is a webpage you can create a label on that and set than this
varialble too that.

You can of course as well debug it, your stored procedure is only a
description what has to be readed.

Cor
 
how can i see the returned data from SQL while debuging?

If you run SQL Profiler, you can see what the query sent to SQL Server and
verify if it is correct.

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
 
Back
Top