DropDownList.SelectedValue

C

CK

Hi All,
I have a class and one property is an Enum. I am trying to persist the
DropDownList.SelectedValue to the class property.
I am using
job.ExpRelated =
Enum.ToObject(typeof(ExperienceRelated),ddlExpRelated.SelectedValue);

but it doesn't build. Any ideas anyone?

Thanks All,

~CK
 
J

Jeff Dillon

Do you get an error?

Break it into pieces...hard-code SelectedValue etc until you find why it
isn't working
 
J

Jeff Dillon

Instead of ddlExpRelated.SelectedValue, hard-code a specific value.

Did you copy this code? Beginners generally don't know about Enum's and
typeof...

start with Job.ExpRelated = value

then add pieces in one at a time to see where it's failing

And post your error message

Jeff
 
R

Rajib

Try this:
job.ExpRelated = (ExpRelated)
Enum.ToObject(typeof(ExperienceRelated),ddlExpRelated.SelectedValue);

Hope that works.

- Rajib
 
C

CK

Well this works.
job.ExpRelated = ExperienceRelated.Directly;

I basically wanted to grab the value from the dropdown and cast it as an
Enum.

Any ideas? I can't give you an error as it doesn't compile. The compiler
gives me a typecasting error.

E:\Web Projects\EmploymentSolution\Employment\AppEmploymentCtl.ascx.cs(390):
Cannot implicitly convert type 'object' to
'Employment.Library.ExperienceRelated'
 
R

Rajib

Your initial statement is basically converting it to an object. Assuming the
assignment works correctly all you needed to do a type cast to
ExperienceRelated.

Updated statement:

job.ExpRelated = (ExperienceRelated)
Enum.ToObject(typeof(ExperienceRelated),ddlExpRelated.SelectedValue);
 
C

CK

I get:
Object reference not set to an instance of an object

when I use:
(ExperienceRelated)Enum.ToObject(typeof(ExperienceRelated),ddlExpRelated.SelectedValue);

and I get:
E:\Web Projects\EmploymentSolution\Employment\AppEmploymentCtl.ascx.cs(390):
The type or namespace name 'ExpRelated' could not be found (are you missing
a using directive or an assembly reference?)

when I use:
(ExpRelated)Enum.ToObject(typeof(ExperienceRelated),ddlExpRelated.SelectedValue);



Any other ideas anyone? I know this should be doable.

Thanks,

~CK
 

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

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top