Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL-Server SQL-Server-2008 , +
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 Smile | :)
Posted 10-Dec-12 5:14am
Joy1979269
Comments
ryanb31 at 10-Dec-12 10:51am
   
Do you know SQL? Do you know how to write a SELECT statement?
Joy1979 at 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
ryanb31 at 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.
ryanb31 at 10-Dec-12 11:16am
   
One way is to replace the ' but using parameters is better.
Joy1979 at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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

try to change your code to this :

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
  Permalink  
v2
Comments
Joy1979 at 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 at 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
VitorHugoGarcia at 10-Dec-12 12:20pm
   
This is a second question of yours, isn't it ? I provided a solution for the first one.
VitorHugoGarcia at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 418
1 Sascha Lefévre 270
2 Abhinav S 170
3 Maciej Los 160
4 Richard Deeming 160
0 Sergey Alexandrovich Kryukov 7,096
1 OriginalGriff 6,395
2 Maciej Los 2,782
3 Abhinav S 2,732
4 Peter Leow 2,694


Advertise | Privacy | Mobile
Web04 | 2.8.150414.1 | Last Updated 10 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100