Search code examples
c#asp.netdrop-down-menudetailsview

Binding DropDownList's SelectedIndex to 0 if value does not exist


I have a DropDownList in a DetailsView, which binds a user's 'AllocatedUser' to a user in the Users table. Occasionally, we'll have some mishaps in the database where the user allocated isn't in the users table, which the DropDownList is filled with. In this case the site crashes and we get a 'SelectedValue does not exist in the list of items error'.

What I want to do is catch any binding errors like that, and just set the DropDownList index to 0. I've tried something like this:

OnDataBinding='DoesExist("AllocatedUser",<%#Bind("AllocatedUser")%>)'

but it doesn't like the syntax of that. Any other ideas?


Solution

  • your question is usually asked again and again, couldn't find an old one really nice with some SELECT 0 IF NULL from the database, or similar, but found this one which should help anyway...

    How to bind to a dropdown list with no value