views:

96

answers:

1

Hi there,

I have 3 dropdown boxes (combo box) in asp.net environment. They are all optional, so if a user has selected anything, i am updating database, if nothing has been selected at all, i am still updating database with null values.

I tried to do this:

 int? CountryId = Convert.ToInt32(ddCountries.SelectedItem.Value);

I was hoping that if nothing is selected null will be inserted in CountryId, but, instead its throwing an exception.

I tried to search for ddCountries.isSelected (or something like that) but it obviously doesnt exist..

so how do I find out if a selection has been made on a dropdown box? - through c# code.

Many Thanks

ps: I have a thought - i put each dropdown box in a try... catch block and if exception arises, set variables to null manually.. but I am not sure thats the best way to do it!

+2  A: 

You're looking for

if(ddCountries.SelectedIndex > -1)

You should never be using exceptions to control program flow.

Jamiec
Hi, I used a mix of your answer and the one below (from LukasW with ternary operator) and its working now. Plus thanks much for info on using exception to control program flow.
iamserious