More ADO.NET in VB.NET

  • Thread starter Thread starter Sean
  • Start date Start date
S

Sean

Maybe I'm just not understanding these SQLReaders. When I
get to trying to read a column's value:

Result = SQLReader.Item("ColumnName")

, I get an exception of trying to read value when no value
is present.

Here is the code....

Public Function GetEncounterFormID(ByVal strName As
String) As Integer
Dim SQLReader As SqlClient.SqlDataReader ' SQL
Reader
Dim strSQL As String ' Transacat-SQL Statement
Dim intResult As Integer ' Function result value

intResult = -1 ' Initialize result value

' Transact-SQL Select Statement
strSQL = "SELECT EncounterFormID FROM
EncounterForm WHERE " _
& "Name = '" & strName & "'"

' Assign SQL Statement
m_SQLCommand.CommandText = strSQL

Try ' Catch any SQL Exceptions

' Create the Reader (holds result dataset)
SQLReader = m_SQLCommand.ExecuteReader()

' If a record was found
If SQLReader.HasRows = True Then


' ************* Blows up on this line *******************
intResult = SQLReader.Item ("EncounterFormID")
End If

SQLReader.Close() ' Close the SQL Reader

' Handle SQL Exception
Catch er As SqlClient.SqlException
intResult = -1
End Try

Return intResult ' Return ID or -1 on Failure
End Function
 
Here is the error:

An unhandled exception of
type 'System.InvalidOperationException' occurred in
system.data.dll

Additional information: Invalid attempt to read when no
data is present.
 
Ok, nevermind, figured it out....have to call a

SQLReader.Read() first....
 
You have to call the Read method to read each row out of the reader...
 
I agree. We've all been there and done that, so we just chuckle and nudge
them along a bit from time to time.

Rob
 
No doubt. Most of us made that mistake or overlooked it at one point or
another.
 
Most?

In my opinion only the ones who never makes a program are never making a
program error.

(Although mostly they can tell what stupid it is to make them)

Cor
 

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

Back
Top