views:

507

answers:

5

Here's the scenario:

The selections in DropDownList are bound from a DB Table listing the acceptable values. At one point in time the values were:

One Two Three Four

At some point later, the list of acceptable values change to:

One Two Four Five

However, the field in the DB that stores the dropdown's value still contains a value of "Three" on some rows. When loading one of those row and setting SelectedValue as such:

dd.SelectedValue = data.Field; // where data.Field == "Three"

...an error is thrown stating: 'dd' has a SelectedValue which is invalid because it does not exist in the list of items.

Data clean up isn't an option here. It would cause problems for customers as the stored values aren't invalid choices for data that's already created, but are invalid choices for newly created data.

How have others handled this situation?

+1  A: 

You could add an extra column in the database table for the dropdown values called 'Active' which can be either true or false. Then instead of deleting an old value, you just mark it as inactive. You should have a foregin key contraint from the customer to the list of acceptable values to ensure that you cannot delete a value from the table if there are still some customers using it.

In the client, you can show customers who are using inactive types in a different color, and have a validation method that doesn't allow you to change a customer from an active type to an inactive type, but allows customers with inactive types to remain on that setting.

Mark Byers
+1  A: 

Assuming data.Field is actually a string here, I would do:

ListItem itemToSelect = dd.Items.FindByText(data.Field);
if(itemToSelect != null)
{
     dd.SelectedItem = itemToSelect;
}
Justin Swartsel
best solution i've found thus far
alex
A: 

so the old value, in this case "Three", is still present in the list table, but just deactivated or is it removed from the table altogether?

if the former, then setup two separate views, one which only includes active items and is used for new data entry, and one which includes all items and is used for viewing historical transactions.

A: 

So you'll have to add the "historically accurate but now defunct" value into your drop-down list, but you can use a RequiredFieldValidator to prevent the user from saving this value back to the database. The RequiredFieldValidator's ControlToValidate is set to the DropDownList you wish to validate, and then you can set the InitialValue property to the invalid value. Now in your save method you can check the bool Page.IsValid before you save the page to the database. The message on your RequiredFieldValidator could be something like "This value is no longer acceptable due to ..."

Hope this helps!

Scott Anderson
+4  A: 

We do have this kind of situation here.

When that happens, I manually add the missing item to the dropdownlist, but in a red font.

If the user tries to re-save the item, the red items are considered inactives and invalid. A valid choice (non-red) must then be picked from the drop-down list.

Danny T.
Thanks. I like this solution the best so far. A lot of the other answers suggest moving this business rules to the DB (which I don't like) or cause the current value to not be displayed and that is definitely not acceptable in this situation. Though in this particular situation, I'd allow re-saves since the value is still valid for previously created data. Only new data should not have these values. The fact that invalid values are added dynamically only on the old data sufficiently solves the problem.
Craig M