DataReader has Rows, but can't be read?

B

Bmack500

I have the following function, which simply fetches records from a
database. I use the line "if dr.hasrows then".... continue the
function. However, when I try to read a record, I get the error:
"Invalid attempt to read when no data is present". I've never seen this
before. I know the record is in the table, but the datareader says it
can't be read - yet it has rows.

Any help would be greatly appreciated. Thanks!

Here's the code:

Function FindNic(ByVal strPNPshort As String, ByVal strService As
String, ByVal svrInfo As ServerInfo, ByVal wrkrBkgnd As
System.ComponentModel.BackgroundWorker) As nicStruct
Dim Index As Integer
Dim nicInfo As New nicStruct
Dim drComm As SqlCommand
Dim dr As SqlDataReader

nicInfo.PNPid = strPNPshort
nicInfo.Service = strService

Dim strSQL As String
strSQL = "SELECT * FROM CAAMDB.dbo.pma_SVR_GUIDREF WHERE
(PNPSHORT = @pnpshort) AND (SERVICE = @service)"
drComm = New SqlCommand(strSQL, sqlConnection)

drComm.Parameters.Add(New SqlParameter("@pnpshort",
SqlDbType.VarChar, 250)).Value = strPNPshort
drComm.Parameters.Add(New SqlParameter("@service",
SqlDbType.VarChar, 20)).Value = strService

opensql()

Try
dr = drComm.ExecuteReader()
Catch ex As Exception
logger.logit("FIND NIC INFO sql error: " & ex.Message,
configParams, wrkrBkgnd, 1)
logger.logit("FIND NIC INFO sql string: " &
drComm.CommandText, configParams, wrkrBkgnd, 1)
Finally
'sqlCMD.Dispose()
End Try
Try
If dr.HasRows Then
'Here's where I'll get the error... row 4 has data in
every row.......
If Not dr.GetString(4) = "NA" Then
While dr.Read()
'sqlRdr.Item(0).ToString
nicInfo.CLASSGUID = dr.Item(2).ToString
nicInfo.Subkey = dr.Item(3).ToString
nicInfo.Value = dr.Item(4).ToString
nicInfo.productName = dr.Item(5).ToString
nicInfo.paramSubkey = dr.Item(6).ToString
Index += 1
End While
Else
nicInfo.bError = True
End If
End If
Catch ex As Exception
'Debugger.Break()
logger.logit("NIC NOT FOUND, OR UNABLE TO READ: " _
& ControlChars.CrLf & "PNPSHORT: " & nicInfo.PNPid &
ControlChars.CrLf & "SERVICE: " & nicInfo.Service _
& ControlChars.CrLf & "SERVER: " & svrInfo.oDNSHOSTNAME,
configParams, wrkrBkgnd, 1)
logger.logit("Exception occurred: " & ex.Message,
configParams, wrkrBkgnd, 1)
nicInfo.bError = True
End Try
dr.Close()
closesql()
Return nicInfo
End Function
 
T

tdavisjr

You must first call dr.Read() to start the reading process. So the two lines
below:
If Not dr.GetString(4) = "NA" Then
While dr.Read()

Must be swapped

While dr.Read()
If Not dr.GetString(4) = "NA" Then


Then make sure your End While and EndIf matches up and things should be
cool.
 
B

Bmack500

DOH!!!!!!!

Thanks!
You must first call dr.Read() to start the reading process. So the two lines
below:
If Not dr.GetString(4) = "NA" Then
While dr.Read()

Must be swapped

While dr.Read()
If Not dr.GetString(4) = "NA" Then


Then make sure your End While and EndIf matches up and things should be
cool.
 

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