ASP.NET: Programmatically selecting a DDL item

DJFuji

Diamond Member
Oct 18, 1999
3,643
1
76
When a user accesses a page that is populating a form for edit using existing data, i have to programmatically select a DDL with the data from the DB. But I dont want to put try catch blocks around every flamin' line that does this just in case the database is holding a value that doesn't exist in the DDL items collection.

Say, for example, that i have a DDL with items "apple", "orange", and "tangerine". The database holds the value "apple" but i later take that value out of the dropdownlist entries. When the code tries to select the item for "apple" and can't find it, it crashes. Isn't there a way to do something like the attached code?

Note: I know about ddl.items.contains(), but it makes you pass in an object of type ListItem and frankly, i don't want to be concerned with both text and value pairs. I just want to check if the value exists before I run the selection code.

Any ideas?
 

DJFuji

Diamond Member
Oct 18, 1999
3,643
1
76
omg. i'm a moron. I've used findbytext and findbyvalue a lot in the past but i didn't realize it returns a null if it doesn't find anything. The way i was using it, it would throw an exception if nothing was found. Probably because i didn't know what to check for if it couldn't find anything. Thanks for the head's up.