The problem arises when the user clicks the update button and we have to turn that string (dropdown.SelectedValue) back into a DateTime. Here the ObjectDataSource messes up and converts the string back using the InvariantCulture, which throws an exception since "30/10/2006" isn't a valid date.
This Microsoft Connect link summarizes the issue a bit more.
I was unhappy with the workaround posted there so I figured out a better one. Basically we just do the DateTime conversion ourselves, instead of letting the ObjectDataSource handle it.
1. Handle the ObjectDataSource Updating event.
2. In the handler, replace the string input parameter with its culturally aware DateTime equivalent, using something like:
TypeConverter converter =
TypeDescriptor.GetConverter(typeof(DateTime));
e.InputParameters["DateField"] =
converter.ConvertFromString(
e.InputParameters["DateField"] as string);
TypeDescriptor.GetConverter(typeof(DateTime));
e.InputParameters["DateField"] =
converter.ConvertFromString(
e.InputParameters["DateField"] as string);
And that's it.
1 comment:
Hi,
The workaround presented in the MS Connect is using business object. When you are using business object, you cannot do the conversion by handling the ObjectDataSource Updating event, because converting the TextBox value into DateTime value is done BEFORE the Updating event is raised. This is difference when you are using business object and when you are using parameters to update the value.
see Using Parameters with the ObjectDataSource Control
Post a Comment