Click here to Skip to main content
15,898,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi guys, here's my code for seraching data in sql and display it to datagridview.
But I want to make it suggesting the possible data(s) in datagridview base on what i'm typing in the textbox?
Help me please..
Thank you in advance.. :)

<br />
<br />
 ' ''-------Search records from database<br />
        Dim cmds As New SqlCommand<br />
        Dim cons As New SqlConnection<br />
<br />
        Dim cts As String = "SELECT names,(dept + ' ' + div + ' ' + pos) as dept_div_pos, tel, local FROM directory1 where (names=@names or dept=@dept or div=@div or pos=@pos or tel=@tel or local=@local)"<br />
        cons.ConnectionString = "Data Source=192.168.xx.xx;Initial Catalog=information;Persist Security Info=True;User ID=me;Password=cute"<br />
        cmds.Connection = cons<br />
        cmds.CommandText = cts<br />
        cons.Open()<br />
<br />
        Try<br />
<br />
            If TextBox1.Text = "" Then<br />
                MsgBox("Inavalid Input!")<br />
<br />
            Else<br />
                ' ''-------- codes for textbox that can accept ascii/special characters<br />
                Dim param12 As New SqlParameter()<br />
                param12.ParameterName = "@names"<br />
                param12.Value = TextBox1.Text.Trim()<br />
                cmds.Parameters.Add(param12)<br />
                Dim param13 As New SqlParameter()<br />
                param13.ParameterName = "@dept"<br />
                param13.Value = TextBox1.Text.Trim()<br />
                cmds.Parameters.Add(param13)<br />
                Dim param14 As New SqlParameter()<br />
                param14.ParameterName = "@div"<br />
                param14.Value = TextBox1.Text.Trim()<br />
                cmds.Parameters.Add(param14)<br />
                Dim param15 As New SqlParameter()<br />
                param15.ParameterName = "@pos"<br />
                param15.Value = TextBox1.Text.Trim()<br />
                cmds.Parameters.Add(param15)<br />
                Dim param16 As New SqlParameter()<br />
                param16.ParameterName = "@tel"<br />
                param16.Value = TextBox1.Text.Trim()<br />
                cmds.Parameters.Add(param16)<br />
                Dim param17 As New SqlParameter()<br />
                param17.ParameterName = "@local"<br />
                param17.Value = TextBox1.Text.Trim()<br />
                cmds.Parameters.Add(param17)<br />
<br />
                Dim dss As New DataSet<br />
                Dim das As New SqlDataAdapter(cmds)<br />
                das.Fill(dss, "directory1")<br />
                DataGridView1.DataSource = dss<br />
                DataGridView1.DataMember = "directory1"<br />
            End If<br />
<br />
        Catch ex As Exception<br />
            MsgBox("Invalid Input")<br />
            MessageBox.Show(ex.Message)<br />
<br />
        End Try<br />
        cons.Close()<br />
<br />
Posted
Updated 1-May-14 16:38pm
v2
Comments
DamithSL 1-May-14 22:49pm    
in which event you put above code? is this winform application?
khei-chan007 1-May-14 22:58pm    
in search button

1 solution

you can do it on TextChanged event

VB
Private Sub TextBox1_TextChanged(sender As Object, _ 
  e As EventArgs) Handles TextBox1.TextChanged
    ' your code goes here .....
End Sub


Quote:
To make a new TextChanged event, open Visual Studio—and follow these steps.

Click on the TextBox and focus it to see the correct designer UI. On the right bottom, you should see the Properties pane. Look for the lightning bolt icon in the pane. After you find the lightning bolt, click on it.

Scroll down to where the word TextChanged appears. Double-click in the space next to the TextChanged label.
 
Share this answer
 
v3
Comments
khei-chan007 1-May-14 23:34pm    
i will transfer my codes in search button to textbox1? or i'm going to make a new codes?
DamithSL 1-May-14 23:40pm    
you better create new method for search records, then you can call that from any other event or method
khei-chan007 1-May-14 23:45pm    
okay.. thanks for your help.. :)

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