Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Hello Friends!!

I have window Form with 'text box' for display and 'command button' to find customer number.

Can you please help me how to perform search command in Vb.net?

Im using SQL Server 2008r2 as my Database.... i Can now Connect to my Database, and insert some of my records with 'SAVE' button. now i want to search some records but i dont know how to do that. can you please give me some sample codes or links to a tutorial? it would be a great help for me if you can teach me...

Thank you :)
Posted
Updated 24-Oct-18 6:06am
Comments
ZurdoDev 10-Dec-12 10:51am    
Do you know SQL? Do you know how to write a SELECT statement?
Joy1979 10-Dec-12 10:54am    
Yes I Do know. I was trying with following code but somehow it is giving me error..I guess I am missing something here.

Private Sub Searchbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Searchbtn.Click
' Dim
' Str = "Select * From Design_Parameters Where DesignID,ParameterName,ParameterValue="" & Trim(txtname.text) & " '"
' ds = New DataSet()
' da = New SqlClient.SqlDataAdapter(Str, conn)
' da.fill(ds)
' If (ds.Tables(0).Rows.Count > 0) Then
' MessageBox("Record Found")
' 'u can also fetch data from this dataset aslo
' Else
' MessageBox("No Record Found")
' End If
' End Sub
'End Class
ZurdoDev 10-Dec-12 10:57am    
Your where statement is incorrect. SELECT * FROM Design_Parameters WHERE ParameterValue = txtName.text, not where all 3 columns equal it. If you want to search all 3 columns you would use OR. However, you should look into SQL Injections because anyone who knows a little bit of SQL could use your application to delete your database or steal data.
ZurdoDev 10-Dec-12 11:16am    
One way is to replace the ' but using parameters is better.
Joy1979 10-Dec-12 11:17am    
If Not IsDbNull(strValue) Then
If intLevel > 0 Then
strValue = replace(strValue, "'", "''")
strValue = replace(strValue, "--", "")
strValue = replace(strValue, "[", "[[]")
strValue = replace(strValue, "%", "[%]")
End If

If intLevel > 1 Then
Dim myArray As Array
myArray = Split("xp_ ;update ;insert ;select ;drop ;alter ;create ;rename ;delete ;replace ", ";")
Dim i, i2, intLenghtLeft As Integer
For i = LBound(myArray) To UBound(myArray)
Dim rx As New Regex(myArray(i), RegexOptions.Compiled Or RegexOptions.IgnoreCase)
Dim matches As MatchCollection = rx.Matches(strValue)
i2 = 0
For Each match As Match In matches
Dim groups As GroupCollection = match.Groups
intLenghtLeft = groups.Item(0).Index + len(myArray(i)) + i2
strValue = Left(strValue, intLenghtLeft - 1) & " " & right(strValue, len(strValue) - intLenghtLeft)
i2 += 5
Next
Next
End If

1 solution

Your problem is within sql sintaxe as you can't compare that way at where clause.

try to change your code to this :

VB
Private Sub Searchbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Searchbtn.Click
            Dim str as string = ""
            Str = "Select DesignID From Design_Parameters Where ParameterValue='" & Trim(txtname.text) & "'"
            ds = New DataSet()
            da = New SqlClient.SqlDataAdapter(Str, conn)
            da.fill(ds)
            If (ds.Tables(0).Rows.Count > 0) Then
                MessageBox("Record Found")
                'u can also fetch data from this dataset aslo
            Else
                MessageBox("No Record Found")
            End If
        End Sub
 
Share this answer
 
v2
Comments
Joy1979 10-Dec-12 12:16pm    
Hi, Thank you for your help. I need to display data in a from by using DesignID. (In text box when I enter DesignID like, AB12345) It should return records from table.) Am I missing anything in Select Query? When I try to load form and enter DesignID in Text box, Nothing is happening but screen gets stuck and then I need to start task manager to cancel it. Any try/catch would help?
Joy1979 10-Dec-12 12:17pm    
My code is like this:

Private Sub Searchbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Searchbtn.Click

Dim str As String = ""
Dim ds = New DataSet
Dim da = New SqlDataAdapter

str = "Select DesignID From Design_Parameters Where ParameterValue='" & Trim(TextBox4.Text) & "'"
ds = New DataSet()
da = New SqlClient.SqlDataAdapter(str, conn)
da.Fill(ds)
If (ds.Tables(0).Rows.Count > 0) Then
MessageBox.Show("Record Found")
'u can also fetch data from this dataset aslo

Else
MessageBox.Show("No Record Found")
End If

End Sub
_Vitor Garcia_ 10-Dec-12 12:20pm    
This is a second question of yours, isn't it ? I provided a solution for the first one.
_Vitor Garcia_ 10-Dec-12 12:21pm    
Now you managed to get DesignID in ds.tables(0).rows object.
You could select the records you want to show from that other table by DesignID.

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