can anyone can help how to create a live search in vb.net 2008 using combo box.

example when i try to search a name then a press a then there should be a drop-down with a list of suggestions of name.

here is my code

Private Sub ComboBox4_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox4.SelectedIndexChanged


        Dim commandString As String

        ComboBox4.Items.Clear()

        commandString = "SELECT * FROM dealer WHERE (fname LIKE '%" & ComboBox4.Text & "%')"
        Try

            Using connectionSearch As New MySqlConnection(globalConnectionString)

                Using commanSearch As New MySqlCommand(commandString, connectionSearch)

                    commanSearch.CommandType = CommandType.Text

                    connectionSearch.Open()


                    Using reader As MySqlDataReader = commanSearch.ExecuteReader

                        If reader.HasRows = True Then

                            While reader.Read

                                ComboBox4.Items.Add(reader("fname").ToString)

                            End While

                        End If

                    End Using

                End Using

            End Using
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "ERROR")
        End Try

    End Sub

Recommended Answers

All 7 Replies

Check out this thread.

yeah this is what i want but i got a problem how i can connect that into the database.

Dim arTemp() As String = {"ComboBox1", "AutoCompleteSource", "AddRange", "Items", "With", "End With"}

i what to load the string from the database.

can you help about that

I think we can do it by looping but how i can do that, any guide for that

ok.. i have solved it..

i have use the northwind database of microsoft here.. i hope you will understand from it.

Please try the Below:

1. Put a combobox on form.
2. now use following code accordingly:

Imports System.Data.SqlClient
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim strconn As String = "Data Source=.\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True"
        Dim conn As New SqlConnection(strconn)
        Dim cmd As New SqlCommand("select ContactName from Customers", conn)
        Dim da As New SqlDataAdapter(cmd)

        Dim ds As New DataSet

        da.Fill(ds, "List")

        Dim col As New AutoCompleteStringCollection ' From which our names will come

        Dim i As Integer
        For i = 0 To ds.Tables(0).Rows.Count - 1
            col.Add(ds.Tables(0).Rows(i)(0).ToString()) 
        Next

'this is the main part 
        ComboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource
        ComboBox1.AutoCompleteCustomSource = col
        ComboBox1.AutoCompleteMode = AutoCompleteMode.Suggest





    End Sub

see the attachment for more info..

commented: :) +10

by the way what this code does

da.Fill(ds, "List")

the world "List", is it a column from the database or not?

it will fill the Dataset ds with the Table Named "List" with the data returned by the query..

check the values of ds in debugging mode you will understand.

its ok thanks a lot @codeoreder and @sandeepparekh9 for the help

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.