No Error, Just stops when no records
Hello, I am trying to verify if a record exists before letting my user continue. This code works great if the item they are looking for exists in the database. However, If the record doesn't exist it just stops on the line
drCheckItem = cmdCheckItem.ExecuteReader(CommandBehavior.SingleR esult)
In the immediate window, it displays the error
A first chance exception of type 'System.InvalidOperationException' occurred in System.Data.dll
Any Ideas what I am doing wrong? I am totally new to ADO as I have always programmed in VB6 and Access using DAO. Now I am trying to do this in Visual Studio 2005 and ADO.net.
Thanks,
Imports System.Data
Imports System.Data.SqlClient
Dim conn As New SqlConnection("Data Source=MYIP;" + "Initial Catalog=Tracker;" + "User ID=USEID;")
Function CheckItem(ByVal ItemIn As String) As String
Dim cmdCheckItem As New SqlCommand()
Dim drCheckItem As SqlDataReader
cmdCheckItem.CommandTimeout = 10
cmdCheckItem.Connection = conn
cmdCheckItem.CommandType = CommandType.Text
cmdCheckItem.CommandText = "Select Item_Number from Items where Item_Number = '" & ItemIn & "' or UPC = '" & ItemIn & "'"
'Make sure that the connection is Open
If conn.State <> ConnectionState.Open Then conn.Open()
drCheckItem = cmdCheckItem.ExecuteReader(CommandBehavior.SingleR esult)
'Read the first line
drCheckItem.Read()
'Did we find something?
If drCheckItem(0).ToString <> "" Then
CheckItem = ItemIn
Else
CheckItem = ""
End If
End Function
|