More ADO.NET in VB.NET

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
 
S

Sean

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.
 
R

Rob Richardson

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
 
C

Cor

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

Top