This is my search button and textbox code, but i will got error on pelanggandatagridview and searchdatas, i need to search data on SQL DB like google! just type any word with almost same with the data and it will show on pelanggandatagridview, please help me...

Private Sub Button5_Click(ByVal searchword As String)
        Dim strsql As String
        strsql = "select * from pelanggan where name like '" & searchword & "%'"
        Dim sqlcmd As New SqlClient.SqlCommand
        Dim SQLConn As New SqlClient.SqlConnection()
        Dim sqlda As New SqlClient.SqlDataAdapter
        Dim sqldr As SqlClient.SqlDataReader
        sqlcmd.commandtext = strsql
        sqlcmd.connection = sqlconn
        sqlda.selectcommand = sqlcmd
        sqldr = sqlcmd.executereader()
        PelangganDataGridView.Item.clear()
        While (sqldr.read())
            With PelangganDataGridView.Item.add(sqldr("Ic"))
                .subitems.add(sqldr("Nama"))
                .subitems.add(sqldr("Hand"))
                .subitems.add(sqldr("Home"))
            End With
        End While
        sqldr.close()


    End Sub

    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
        searchdatas(TextBox2.Text)
    End Sub
End Class

Recommended Answers

All 5 Replies

what does the searchdatas() function do??? it is not mentioned in the code???

try to write the below code in Textbox change event rather than button click and see

open connection first

         Try

            Dim myCommand As New SqlCommand
            With myCommand
                .CommandText = "SELECT * FROM tablename where columnid like '" & Textbox2.Text & "%" + "'"
                .CommandType = CommandType.Text
                .Connection = Connection
            End With
            Dim dt As New DataTable
            dt.Load(myCommand.ExecuteReader)
            With PelangganDataGridView
                .AutoGenerateColumns = True
                .DataSource = dt
            End With
        Catch ex As Exception
            Debug.Print("Exception: ")
            Throw ex
        End Try

check this or put the code in ur button click

wahahahaha indo punya nih.. wkwkwkwkwkw

begini gan, ada baiknya data yang ente binding ke DGV di binding melalui DataTable, itu lebih baik untuk DGV, seperti contoh yang dikasih ama si pooja, terus kalo ente ingin ketika user mengetik ketika itu juga data terfilter, maka ente harus taro itu kodenya di event KeyPress untuk TextBox dan gak perlu pake function searchdatas(), cukup kodenya si pooja letakkan di KeyPress TextBox. ok Gan?

for all : sorry i have do this with my planet language.. this is my bro from my planet :D

i already write the code like this.

Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
        Dim conn As SqlClient.SqlConnection
        conn = GetConnect()
        Try
            conn.Open()
            Dim myCommand As New SqlClient.SqlCommand
            With myCommand
                .CommandText = "SELECT * FROM pelanggan where nama like '" & TextBox2.Text & "%" + "'"
                .CommandType = CommandType.Text
                .Connection = conn
            End With
            Dim dt As New DataTable
            dt.Load(myCommand.ExecuteReader)
            With PelangganDataGridView
                .AutoGenerateColumns = True
                .DataSource = dt
            End With
        Catch ex As Exception
            Debug.Print("Exception: ")
            Throw ex
            conn.Close()
        End Try
    End Sub
End Class

but, after i run and i try to type something, it nothing happen. this is my interface =Click Here
and I hope you can help me to make datagridview to be empty at the beginning of the program, only after i search something it will show the data. sorry for your inconvenience......im still newbie....please help me, im still study and this is my project, it already pending 2 month!!.....=(

to artemix22
sorry because i can't understand your keypress only.....=(

ok try this :

Private Sub GoButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles GoButton.Click
Try
            Using conn As SqlClient.SqlConnection("put your connection string here")
                conn.Open()
                Dim myCommand As New SqlClient.SqlCommand("SELECT * FROM pelanggan where nama like '%" & TextBox2.Text & "%'", conn)
                Dim adapter As New SqlClient.SqlDataAdapter
                Dim dt As New DataTable
                adapter.SelectCommand = myCommand
                adapter.Fill(dt)
                With PelangganDataGridView
                    .DataSource = dt
                    .Columns(0).Width = 100 'you can set column width here
                    .Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter 'you can set column alignment here
                    .Columns(1).Width = 150
                    .Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                    'if you had 10 columns to display, then add more column to set the width and alignment just like i do above.
                End With
                myCommand.Dispose()
                conn.Close()
            End Using
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

like you see, i add that code in Event Button Click, but if you want when user type a word and DGV immediately filtering the data without clicking a button, you must add that code in TextBox2 KeyPress Event.

in first form load, didn't do anything for your DGV, it will keep your DGV clear, and when you want to clear your DGV after searching something use this code in Clear Button :

Try
            Me.PelangganDataGridView.DataSource = Nothing
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

sorry for my language before, i think you came from indonesia, i see "pelanggan" as your table name and "nama" as your column name :D, "pelanggan" mean customer in here and "nama" is name.

If u want the datagridview to be empty at first set the datasource to nothing ....

i tried the same code but as soon as any value in text box changes it shows me the datagrid with values in database.....check ur connection...is it fine???

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.