title: Selecting items in multi-select listbox from delimited database string field
body: <p>I'm trying to build an "edit" page for a database record that can be edited and saved back to the database. One of the fields will be a multi-select listbox that will need to highlight the appropriate list items in a hard-coded list when loaded. </p>
  
  <p>Using C#, how do I populate a multi-select listbox -- with the appropriate items selected -- based on the comma-delimited string from a database field? I've researched a few solutions that involve loops, but I have been unable to get them to work with my limited C# skillset. </p>
  
  <p>This is all I have now, before I got stuck. You'll see that it doesn't account for multiple values in the string. Is there a function like "contains" that I can check to see if the value matches? I'm still missing some (probably basic) C# logic and coding here.</p>
  
  <pre><code>int i;
  for (i = 0; i &lt;= CATEGORYListBox.Items.Count - 1; i++)
  {
      if (reader["CATEGORY"].ToString() == CATEGORYListBox.Items(i).Value)
      {
          CATEGORYListBox.Items(i).Selected = True;                   
      }
  }
  </code></pre>
  
  <p>...</p>
  
  <pre><code>&lt;asp:ListBox ID="CATEGORYListBox" runat="server"&gt;
      &lt;asp:ListItem Value="Circulation"&gt;Circulation&lt;/asp:ListItem&gt;
      &lt;asp:ListItem Value="Interactive Media"&gt;Interactive Media&lt;/asp:ListItem&gt;
      &lt;asp:ListItem Value="Classified"&gt;Classified&lt;/asp:ListItem&gt;
      &lt;asp:ListItem Value="Publishing"&gt;Publishing&lt;/asp:ListItem&gt;
      &lt;asp:ListItem Value="Editorial"&gt;Editorial&lt;/asp:ListItem&gt;
      &lt;asp:ListItem Value="Retail"&gt;Retail&lt;/asp:ListItem&gt;
   &lt;/asp:ListBox&gt;
  </code></pre>
  
  <p>Thanks everyone.</p>