Click here to Skip to main content
16,018,442 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to find whether the text entered in textbox is present in sql database table in vb.net. I have a text box.if i enter value in textbox while moving to next textbox, i need to find whether the value entered is present in table or not. If yes diplay msgbox or display record not found..

Thank you.
Posted
Updated 20-Jun-11 22:17pm
v4
Comments
mahalakshmi.p19 11-Jun-11 1:21am    
hi am getting error message
unhandled exception occured in your application..
system argument exception format of initializing string does not conform to specification starting index 0.....
could you please help me to resolve this issue...

1 solution

You need to create a function that returns a boolean. This function would check the database to see if the string is present.
Something like this

VB
Private Function CheckIfPresent(ByVal textToCheck As String)

        Using con As SqlConnection = New SqlConnection("your connection string")
            Dim cmd As SqlCommand = New SqlCommand("SELECT textField FROM yourTable WHERE textField = @textField", con)
            cmd.Parameters.AddWithValue("@textField", textToCheck)
            con.Open()
            Dim dr As SqlDataReader = cmd.ExecuteReader()
            Dim IsThere As Boolean = False
            If dr.HasRows Then
                IsThere = True
            End If
            dr.Close()
            Return IsThere
        End Using

    End Function

and then in your TextBox's LostFocus event you just call this function

VB
Private Sub TextBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.LostFocus
        Dim textToCheck As String = TextBox1.Text
        If CheckIfPresent(textToCheck) Then
            'Do work if text is there
        Else
            'Do work if text not found
        End If
    End Sub



Hope this helps
 
Share this answer
 
v2
Comments
Tarun.K.S 10-Jun-11 4:06am    
Excellent answer. 5+
Wayne Gaylard 10-Jun-11 4:07am    
Thank you.
mahalakshmi.p19 11-Jun-11 1:18am    
hi am getting error.Unhandled exception has occured in your application.. System argument exception:format of intializing string does not conform to specification starting index 0....
please help me how to resolve this issue......
mahalakshmi.p19 10-Jun-11 9:06am    
thanks a lot....
Sandeep Mewara 10-Jun-11 11:52am    
Good answer Wayne. My 5!

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