title: How can I compare a text box entry against a list of database values in the Text_Changed event
body: <p>as the title states I am trying to compare or validate a text box entry against a list of acceptable values stored in my database.  As of now I have taken the values from my database and store them in a List(of String) and I have a for loop that loops through that list and returns true if the values match, if the values do not match it will return false. Below I have attached the code I am currently working with.</p>
  <pre><code>Protected Sub txtSearchOC_TextChanged(sender As Object, e As EventArgs) Handles txtSearchOC.TextChanged
      Dim listEType As List(Of String) = New List(Of String)
      Dim eType As String = txtSearchOC.Text
      Dim strResult As String = &quot;&quot;
  
      lblPrefix.Text = &quot;&quot;
      lblList.Text = &quot;&quot;
  
      Dim TypeIDQuery As String = &quot;
          SELECT a.OrderCode
          FROM SKU AS a
          INNER JOIN EnrollmentType AS e ON a.EnrollmentTypeID = e.TypeID
          INNER JOIN Enrollment AS f ON e.RecID = f.EnrollmentTypeID
          WHERE f.AccountNumber = '12345';
          &quot;
  
      Using connEType As New SqlConnection(ConfigurationManager.ConnectionStrings(&quot;WarrantyConnectionString&quot;).ToString)
          Using cmdEType As New SqlCommand(TypeIDQuery, connEType)
              cmdEType.Parameters.Add(&quot;@AccountNumber&quot;, SqlDbType.VarChar, 15).Value = &quot;12345&quot;
              connEType.Open()
              Using sdrEType As SqlDataReader = cmdEType.ExecuteReader
                  While sdrEType.Read
                      listEType.Add(sdrEType(&quot;OrderCode&quot;).ToString)
                  End While
              End Using
          End Using
      End Using
  
      For Each Item As String In listEType
          strResult &amp;= Item &amp; &quot;, &quot;
      Next
  
      For i = 0 To listEType.Count - 1
          If eType = listEType(i) Then
              lblPrefix.Text = &quot;True&quot;
          End If
          If eType &lt;&gt; listEType(i) Then
              lblList.Text = &quot;Error&quot;
          End If
      Next
  
      'lblList.Text = strResult
  End Sub
  </code></pre>
  <p>In the code I declare my list and a variable to store the text value of the text box. To verify that it pulled the appropriate values from the database I have the strResult variable and can confirm that the appropriate values are being stored.</p>
  <p>The problem I am having has to do with the For loop I have at the bottom, when I enter in a valid value that is contained in the listEType, I get the confirmation message of &quot;True&quot; indicating it has matched with one of the values, but I also get the &quot;Error&quot; message indicating that it does not match.  If I enter in a value that is not contained in the list I only get the &quot;Error&quot; message which is supposed to happen.</p>
  <p>My question is, based on the code I have supplied, why would that For loop be returning both &quot;True&quot; and &quot;Error&quot; at the same time for a valid entry? Also, if there is a better way to accomplish what I am trying to do, I am all ears so to speak as I am relatively new to programming.</p>