i'm currently working on my thesis e-voting system. and i need to modify my search candidate and search voter form. the data from my database is already loaded in my listview when the system. what i want to do is while i am typing in the search text box, at the same time the items in my list view is being filtered without clicking the search button. i hope you guys can help me.

this is my code

Private Sub View_Candidate_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 

    SqlString = "SELECT * FROM candidate order by candidateLname ASC"
        'Function for searching by candidate ID number
        Using conn As New MySqlConnection(ConnString)

            Using cmd As New MySqlCommand(SqlString, conn)

                cmd.CommandType = CommandType.Text

                conn.Open()

                Using reader As MySqlDataReader = cmd.ExecuteReader()

                    If reader.HasRows = True Then
                        While reader.Read()

                            Dim li As New ListViewItem

                            li.Text = reader("candidateIDno").ToString
                            li.SubItems.Add(reader("candidateLname").ToString)
                            li.SubItems.Add(reader("candidateFname").ToString)
                            li.SubItems.Add(reader("candidateMname").ToString)
                            li.SubItems.Add(reader("position").ToString)
                            li.SubItems.Add(reader("partyList").ToString)

                            lstviewvoter.Items.Add(li)

                        End While

                    End If

                End Using

            End Using

        End Using

    End Sub

Recommended Answers

All 4 Replies

Hi there,
What could you do is to load all Candidate's data into a list, and then on the TextChanged() event of the textbox you could use linq to list to retrieve the data. :)

Hi there,
What could you do is to load all Candidate's data into a list, and then on the TextChanged() event of the textbox you could use linq to list to retrieve the data. :)

thanks for the reply... can you please show me a sample code. i'm still a newbie in vb.net so i don't have any idea about linq. i'm using mysql as a database. thanks

i already solve it... thanks alexpap for the idea of TextChanged()... i tried this code

Private Sub txtsearchby_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtsearchby.TextChanged
        lstviewvoter.Items.Clear()
        SqlString = "SELECT * FROM candidate where (candidateLname LIKE '%" & txtsearchby.Text & "%')"
        'Function for searching by candidate ID number
        Using conn As New MySqlConnection(ConnString)

            Using cmd As New MySqlCommand(SqlString, conn)

                cmd.CommandType = CommandType.Text

                conn.Open()

                Using reader As MySqlDataReader = cmd.ExecuteReader()

                    If reader.HasRows = True Then
                        While reader.Read()

                            Dim li As New ListViewItem

                            li.Text = reader("candidateIDno").ToString
                            li.SubItems.Add(reader("candidateLname").ToString)
                            li.SubItems.Add(reader("candidateFname").ToString)
                            li.SubItems.Add(reader("candidateMname").ToString)
                            li.SubItems.Add(reader("position").ToString)
                            li.SubItems.Add(reader("partyList").ToString)

                            lstviewvoter.Items.Add(li)

                        End While

                    End If

                End Using

            End Using

        End Using

    End Sub

You're welcome. :)

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.