Invalid cast exception when assign combobox returned value to the variable

G

Guest

Hi! I'm from Poland and sorry for my english ;-)
I'm also beginner with ADO Net programing.
My problem is, when I want to assign returned value from combobox, which is
connected to an existing datatable in dataset.
The combobox's propertises like datasource, displaymember and valuemember
are sets properly.
I achieve returned value through the combobox.selectedvalue propertie

I guess that The Invalid cast exception occures because I don't exactly
know what type of value combobox1.selectedvalue is...
I want to assign this returned value to the column of another table (foreign
key). I know that is typical operation, and it seems to be simple TO DO,
but i can't find any clear tutorial (Even on the csharpcorner website).

I will be very thankful for any samples codes (I suppose that it will be a
few lines of code).... or explanation where my approach and understanding
this trouble starts to be incorrectly..

Please don't call me LAMER for this, because I know it's often using
operation. But I really tried to find satisfactory solution by google and
another ways and I didn't think that it will be so difficult .....
 
D

David Browne

Hi! I'm from Poland and sorry for my english ;-)
I'm also beginner with ADO Net programing.
My problem is, when I want to assign returned value from combobox, which
is connected to an existing datatable in dataset.
The combobox's propertises like datasource, displaymember and valuemember
are sets properly.
I achieve returned value through the combobox.selectedvalue propertie

I guess that The Invalid cast exception occures because I don't exactly
know what type of value combobox1.selectedvalue is...

This happens all the time, and not just when you're new. Whenever you are
having problems converting types, unpack all the madness onto seperate
lines, and walk it through one cast or conversion at a time.

SomeType v = combobox.selectedvalue;

becomes

object o = combobox.selectedvalue;
SomeType v = (SomeType)o;

Put a breakpoint there and examine o. Then figure out the type of the
destination, and convert it appropriately.


David
 

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