Combo box "specified cast is not valid" error

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
 
C

ChrisM

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.
 
C

ChrisM

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)?
 
T

Tom Spink

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top