Combo box "specified cast is not valid" error

  • Thread starter Thread starter Jayyde
  • Start date Start date
J

Jayyde

Not sure why all of a sudden this line of code:

cmbProductCategoryTreeDisplayName.SelectedIndex = 0;

is producing a "Specified cast is not valid error". In the watch it has 1
item in it at index 0. This code hasn't been changed in months and it has
never had this problem before...

Thanks for any help you guys can give me!

-Jayyde
 
Hmmm, very strange!

Sure it's a zero and not an Oh?
The Combo is definatly instantiated at that point?
Definatly has at least one item in it.
Don't think you'd get that error even if it wasn't
Otherwise, can't see anything wrong.
Try this code as an experiment:

int indexValue = 0;
if(cmbProductCategoryTreeDisplayName.Items.Count > 0)
cmbProductCategoryTreeDisplayName.SelectedIndex = indexValue;
else
MessageBox.Show("Wait a minute... there's nothing here to set!!");

Cheers,

Chris.
 
One other thing...

Is it possible that the error is in fact coming from somewhere else, that is
being triggered by that assignment? (say the SelectedIndexChanged event)?
 
Jayyde said:
Not sure why all of a sudden this line of code:

cmbProductCategoryTreeDisplayName.SelectedIndex = 0;

is producing a "Specified cast is not valid error". In the watch it has 1
item in it at index 0. This code hasn't been changed in months and it has
never had this problem before...

Thanks for any help you guys can give me!

-Jayyde

Hi Jayyde,

Check your stack trace for the exception, and see where it's coming from.
If you are adding objects straight into the Items collection, i.e. not just
strings/etc then perhaps one of those objects are playing up.
 
Back
Top