title: How do I solve an "InvalidCastException"?
body: <p>I'm getting an "<code>InvalidCastException</code>" during run time on the following code:</p>
  
  <p>My C# WinForm code contains a comboBox which is populated from a database with the following code:</p>
  
  <pre><code>public void PopulateCompetitionFormatDd()
  {
       var _competitionFormat            = new CompetitionFormatBL();
       cbCompetitionFormat.DataSource    = _competitionFormat.GetByAllCompetitionFormats();
       cbCompetitionFormat.ValueMember   = "CompetitionFormatId";
       cbCompetitionFormat.DisplayMember = "CompetitionFormatType";
  }
  </code></pre>
  
  <p>The <code>ValueMember</code> (<code>CompetitionFormatId</code>) is a list of numbers and the <code>DisplayMember</code> (<code>CompetitionFormatType</code>) is a <code>string</code> of text. When I change the item in this comboBox during run time I get the error "<code>InvalidCastException</code>".</p>
  
  <pre><code>private void cbCompetitionFormat_SelectedIndexChanged(object sender, EventArgs e)
  {
       int competitionFormat = 1;
       competitionFormat = (int)cbCompetitionFormat.SelectedValue;
  }
  </code></pre>
  
  <p>Any ideas what i'm doing wrong and how I can get around it?</p>