title: VB.NET - Search function using Textbox and Combo Box
body: <p>Good day everyone! I would like to ask for help regarding my code here. The main concern is to search from the MySQL database the keyword provided by a textbox. Here's my GUI for reference.</p>
  
  <p><img src="https://i.stack.imgur.com/BU9th.png" alt="The GUI of my program"></p>
  
  <p>When I type my search key on the text box and the selected a column on the combo box, the search query will have its output on the listview. I've tried numerous combinations to gain an output, but to no avail. </p>
  
  <p>Here's my code for you to help me. </p>
  
  <pre><code>Private Sub Search()
  
      lviClientList.Items.Clear()
      Dim strSqlSearch As String = "SELECT code, Company, StAdd, City, ContactPerson, Phone, Mobile, Email, Remarks FROM tblclients WHERE '@Column' LIKE '%" &amp; txtSearchCriteria.Text &amp; "%'"
  
      Dim item As New ListViewItem()
  
      If cboColumns.SelectedIndex = 0 Then
          column = "code"
      ElseIf cboColumns.SelectedIndex = 1 Then
          column = "Company"
      ElseIf cboColumns.SelectedIndex = 2 Then
          column = "StAdd"
      ElseIf cboColumns.SelectedIndex = 3 Then
          column = "City"
      ElseIf cboColumns.SelectedIndex = 4 Then
          column = "ContactPerson"
      ElseIf cboColumns.SelectedIndex = 5 Then
          column = "Phone"
      ElseIf cboColumns.SelectedIndex = 6 Then
          column = "Mobile"
      ElseIf cboColumns.SelectedIndex = 7 Then
          column = "Email"
      ElseIf cboColumns.SelectedIndex = 8 Then
          column = "Remarks"
      End If
  
      Dim mysqlCommand As New MySqlCommand(strSqlSearch, mysqlConnection)
      mysqlCommand.Parameters.AddWithValue("@Column", column)
  
      Try
          mysqlConnection.Open()
          mysqlReader = mysqlCommand.ExecuteReader()
  
          Do While mysqlReader.Read()
  
              item = lviClientList.Items.Add(mysqlReader("code").ToString)
              item.SubItems.Add(mysqlReader("Company").ToString)
              item.SubItems.Add(mysqlReader("StAdd").ToString)
              item.SubItems.Add(mysqlReader("City").ToString)
              item.SubItems.Add(mysqlReader("ContactPerson").ToString)
              item.SubItems.Add(mysqlReader("Phone").ToString)
              item.SubItems.Add(mysqlReader("Mobile").ToString)
              item.SubItems.Add(mysqlReader("Email").ToString)
              item.SubItems.Add(mysqlReader("Remarks").ToString)
  
          Loop
  
      Catch ex As Exception
  
          MsgBox("No results found.", MsgBoxStyle.OkOnly, "Project Analysis System")
  
      Finally
  
          mysqlReader.Close()
          mysqlConnection.Close()
  
      End Try
  
  End Sub
  </code></pre>