VB.NET•Use LIKE in the DataSet.Search String to Return a DataRow Array

Listing 4. The btnSearchDS_Click event handler passes "ColumnName LIKE '[%]Match%'" as the argument to the dsCusts.Select() method to perform partial-match searches on e-mail addresses, phone numbers, and ZIP codes. Multiple matches populate a dropdown list from which users select the row to edit (see Figure 3).

Private Sub btnSearchDS_Click(ByVal sender As _
   System.Object, ByVal e As System.EventArgs) _
   Handles btnSearchDS.Click
   'Search based on option button selection
   Dim strSearch As String = txtSearchDS.Text
   Dim strType As String
   Call ShowHideCombo(False)
   If optCustIDDS.Checked Then
      'Exact match
      strSearch = "CustID = " + strSearch
   ElseIf optPhoneDS.Checked Then
      'starts-with match
      strSearch = "Phone LIKE '" + strSearch + _
         "%'"
      strType = "Select a Phone Number"
   ElseIf optEmailDS.Checked Then
      'contains match
      strSearch = "Email LIKE '%" + strSearch + _
         "%'"
      strType = "Select an Email Address"
   ElseIf optZipCodeDS.Checked Then
      'starts-with match
      strSearch = "ZIPCode LIKE '" + strSearch _
         + "%'"
      strType = "Select a ZIP Code"
   End If
   Dim timSearch As Long = Now.Ticks
   aRows = tblCusts.Select(strSearch)
   If aRows.Length > 0 Then
      If aRows.Length = 1 Then
         Call LoadTextBoxes(aRows(0), False)
         Call EditButtonState(True, False)
      Else
         If aRows.Length > 50 Then
            'Too many records for a list 
            'box
            Dim strMsg As String = _
               "Please narrow " & _
               "your search to " + _
               "produce 50 or " & _
               "fewer customer " & _
               "records."
            Dim strTitle As String = _
               "Query Returned " + _
               aRows.Length.ToString _
               + " Records"
            MsgBox(strMsg, _
               MsgBoxStyle. _
               Exclamation, _
               strTitle)
         Else
            'Select from a drop-down list
            txtSearchDS.Visible = False
            btnSearchDS.Visible = False
            Dim intCtr As Integer
            Dim strItem As String
            With cboResultsDS
               'Fill the list with 
               'search results
               .Items.Clear()
               .Items.Add(strType)
               For intCtr = 0 To _
                  aRows.Length - 1
                  If _
                     optEmailDS. _
                     Checked Then
                     strItem = _
                       aRows(intCtr).Item(8).ToString
                  ElseIf _
                     optPhoneDS. _
                     Checked Then
                     strItem = _
                        aRows(intCtr).Item(7).ToString
                  ElseIf _
                     optZipCodeDS. _
                     Checked Then
                     strItem = _
                        aRows(intCtr).Item(6).ToString
                  End If
                  strItem += " - " + _
                    aRows(intCtr).Item(2).ToString + _
                   " (" + aRows(intCtr).Item(0).ToString _
                    + ")"
                  .Items.Add(strItem)
               Next
               .Visible = True
               .SelectedIndex = 0
            End With
         End If
      End If
   End If
End Sub