SelectedValue which is invalid

B

Bart

What is the best way to handle null values in dropdownlists?
Why is the dropdownlist generating the error
"System.ArgumentOutOfRangeException: 'DropDownList1' has a SelectedValue
which is invalid because it does not exist in the list of items." on null
values?
Should it not be possible to have an empty dropdownlist field?

Thanks
Bart
 
P

Phillip Williams

I would always check before assigning a value to the
DropDownList.SelectedValue like this:

ListItem li = ddl.Items.FindByValue(myValue);
ddl.ClearSelection();
if (li != null) //it means the value is found in the list
{
li.Selected = true; //select the item that was found
}
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top