Click here to Skip to main content
16,012,759 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Help with checking existence of entry in SQL db using vb.net

I am trying to write a subroutine in vb.net to check an SQL database to see if a record exists in table 1, and if found, go to label 1, otherwise check in table 2, and if found, go to Label 2. If not found in either table 1 or table 2 then go to Label 3. What I have at the moment always goes to the Not Found scenario (Label 3), even though the item is present in at least one of the two tables. Evidently I have a logic problem in what I have written, but I can't figure out what is wrong. Can somebody help solve my dilemma please? It will be greatly appreciated.

This is the troublesome part of the subroutine I have at the moment:

Dim exists As Boolean

sql = "SELECT ASSETTAG from Assets where ASSETTAG ='" & TextBox1.Text & "',"
DBConnect(sql)
While reader.Read
If exists = False Then
GoTo CheckALL_ASSETS
Else
GoTo FoundAssets
End If
DBClose()
End While

CheckALL_ASSETS:
sql = "SELECT ASSETTAG from ALL_ASSETS where ASSETTAG ='" & TextBox1.Text & "',"
DBConnect(sql)
While reader.Read
If exists = False Then
GoTo NotFound
Else
GoTo LookupAllAssets
End If
DBClose()
End While
Posted

1 solution

You can use COUNT(*) in sql to return record count to determine the existence of record. Better to trim the textbox input too. You should also use parameterized query to prevent sql injection. Refer: SqlCommand.Parameters[^]
Dim count As Int16 = 0

Dim commandText As String = _
     "SELECT COUNT(*)  from Assets   where ASSETTAG = @para" 

Using connection As New SqlConnection(connectionString)
        Dim command As New SqlCommand(commandText, connection)

        command.Parameters.AddWithValue("@para", Trim(TextBox1.Text) )

count = command.ExecuteScalar  
   
End Using 

If count = 0 Then
GoTo CheckALL_ASSETS
Else
GoTo FoundAssets
End If
 
Share this answer
 
v3
Comments
Member 10503959 22-Jun-14 19:35pm    
Thank you for your suggestion Peter, however, I tried it and get the following error message when I run the code: "ExecuteScalar requires an open and available connection. The connection's current state is closed." It looks to me like it hasn't opened the SQL connection from that message, but I am not very good with VB.net yet.

I Have included the entire relevant code here to help analyse why:

Public Class EHC
Private Const CB_FINDSTRING = &H14C
Private Const CB_SHOWDROPDOWN = &H14F
Private Const LB_FINDSTRING = &H18F
Private Const CB_ERR = (-1)
Private reader As SqlDataReader = Nothing
Private sql As String = Nothing
Private cmd As SqlCommand = Nothing
Private conn As SqlConnection = Nothing
Private connectionstring As String = Nothing
----------------------------------------------------------------------------------------------------------
Private Sub DBConnect(ByVal sql As String)
'Connection string to Server
connectionstring = "Server=URAOLY-TS01\ODASSETS;Database=ODASSETS;User Id=sa;Password=$t0p@((e$$"
conn = New SqlConnection
conn.ConnectionString = connectionstring 'Set the Connection String
conn.Open()
cmd = New SqlCommand
cmd.Connection = conn 'Sets the Connection to use with the SQL Command
cmd.CommandText = sql 'Sets the SQL String
End Sub
----------------------------------------------------------------------------------------------------------
Private Sub LoadFromDBToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadFromDBToolStripMenuItem.Click

If TextBox1.Text = "" Then
MsgBox("You must type in an asset to load.")
Exit Sub
End If
ClearAllNotAsset()

Dim count As Int16 = 0
Dim commandText As String = _
"SELECT COUNT(*) from Assets where ASSETTAG = @para"

Using connection As New SqlConnection(connectionstring)
Dim command As New SqlCommand(commandText, connection)
command.Parameters.AddWithValue("@para", Trim(TextBox1.Text))
count = command.ExecuteScalar
Try
connection.Open()
Catch ex As Exception

If count = 0 Then
GoTo NotFound
Else
GoTo FoundAssets
End If
End Try
End Using

Thank you again for your assistance.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900