title: C# Selecting a CheckBoxList item(s) by matching against an Array of strings
body: <p>I'm struggling to find a solution to my problem, I have spent a considerable amount of time trying alternative solutions to no avail. Any help or explanations would be greatly appreciated. </p>
  
  <p>I have a SQL database which has a string field called "categories", this containing a list of categories separated by ',' e.g. Referrals, Outpatients. </p>
  
  <p>So I want this list to be compared to a number of CheckListBoxes items (ID=CategoryCBL), anytime the category matches a CheckListBox item it needs to be selected.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>string categories = result.GetString(12).ToString();
  string[] categorie = categories.Split(',');
  
   //loops through all seperated categories (cat) in categorie.
   foreach(string cat in categorie)
   {
       //loops through all list checkboxes 
       for(int index = 0; index &lt;CategoryCBL.Items.Count; index++)
       {
           //gets the listcheck box string 
           string item = CategoryCBL.Items[index].ToString();
           //compare the list box string against the current Category looking for matches
           if (item == cat)
           {
               //if a match occures the list checkbox at that index is selected 
               CategoryCBL.SelectedIndex = index;
  
               TextBox1.Text += item + "-" + cat + "-" + index;
           }
       }
   }
  </code></pre>
  
  <p>Here's my Check list box code:</p>
  
  <pre><code>&lt;asp:CheckBoxList ID="CategoryCBL" class="listItem" RepeatLayout="Table" RepeatColumns="2" RepeatDirection="Vertical" runat="server" Width="100%"&gt;
      &lt;asp:ListItem&gt;Referrals&lt;/asp:ListItem&gt;
      &lt;asp:ListItem&gt;Outpatients&lt;/asp:ListItem&gt;
      &lt;asp:ListItem&gt;Admissions/Discharges&lt;/asp:ListItem&gt;
      &lt;asp:ListItem&gt;A&amp;E&lt;/asp:ListItem&gt;
      &lt;asp:ListItem&gt;Medical Records&lt;/asp:ListItem&gt;
      &lt;asp:ListItem&gt;Outcome Form&lt;/asp:ListItem&gt;
      &lt;asp:ListItem&gt;Data Quality&lt;/asp:ListItem&gt;
      &lt;asp:ListItem&gt;Executive Reporting&lt;/asp:ListItem&gt;
      &lt;asp:ListItem&gt;Infection Control&lt;/asp:ListItem&gt;
      &lt;asp:ListItem&gt;Planning and Performance&lt;/asp:ListItem&gt;
      &lt;asp:ListItem&gt;QlikView&lt;/asp:ListItem&gt;
      &lt;asp:ListItem&gt;Theatres&lt;/asp:ListItem&gt;
      &lt;asp:ListItem&gt;Waiting Times&lt;/asp:ListItem&gt;
  &lt;/asp:CheckBoxList&gt;
  </code></pre>
  
  <p>So I take my categories as <strong>result.getString(12).ToString();</strong> in this example it equals Infection Control,QlikView,Theatres</p>
  
  <p>You can also see I've printed the result to TextBox1. </p>
  
  <p><strong>Here's the outcome of the above code</strong></p>
  
  <p><a href="https://imgur.com/a/IwUqt" rel="nofollow noreferrer">https://imgur.com/a/IwUqt</a></p>
  
  <p>as you can see ONLY Theatres is selected. </p>
  
  <p><a href="https://imgur.com/y9sYNfW" rel="nofollow noreferrer">https://imgur.com/y9sYNfW</a></p>
  
  <p>The output in TextBox1 shows that there are 3 matches occurring at X index's, these index's which line up with the indexes of my individual checklist boxes.</p>
  
  <p>I'd really like to know why only the last matches checklistbox is selected and not the previos 2 matches.</p>
  
  <p>any ideas?</p>
  
  <p>Thank you.</p>