.NET ObjectDataSource ignores culture information when inserting and updating

It’s been ages since I last updated my blog and I’m feeling super guilty.

So I thought I’d get stuck into it again and start with a whinge – a whinge about something that had me stumped for way too long this morning.

I have to say that I hardly ever use the out-of-the-box ASP.NET server controls (like asp:DropDownList, asp:TextBox, asp:DataGrid, etc etc) because I prefer to use standard HTML controls, stick runat=”server” tags on them and handle them manually. Call me old-fashioned, tell me I’m doing it the “hard” way… I don’t care. It works and I know how to do it quickly. I always find that when I use the more complete and complex server controls, I get 95% of the work done quickly and then spend hours trying to get the last 5% to work. I don’t like that very much.

Anyway, sometimes I end up having to use a variety of these server controls and today was one such day. Everything was going swimmingly until I tried to insert / update a DateTime property on the type that I had bound my ListView control to, using an ObjectDataSource. I’m in the UK and here, we do not use US-formatted dates. For some reason, no matter where I put localization code to set the locale to “en-GB”, when I hit my “Save” button, my code would throw an exception because it could not convert the string representation of the date into a DateTime object. That’s because it was trying to convert it to an American date. Grr!!!

After a lot of Googling, I came across this article. And there was the answer I was looking for.

Turns out that this is a known bug since ASP.NET 2.0 that Microsoft have decided they “won’t fix”. WTF. They’ve decided not to fix it because ”... the potential fix is relatively dangerous and the workaround is relatively simple…”.

The bug occurs because behind the scenes, .NET is calling ConvertFromInvariantString on all the new values – which is the “en-US” culture. This is clearly a massive assumption on their part but alas, it seems it is “too risky” to fix this.

The “simple workaround” they suggest is to handle the OnItemInserting and OnItemUpdating or OnRowUpdating events and in those handlers, extract the date values from the EventArgs, parse them from strings into DateTime objects and save them back. The following is an example of how one might do this:

?View Code CSHARP
1
2
3
4
5
6
7
8
9
10
11
private static void MyList_ItemInserting(object sender, ListViewInsertEventArgs e)
{
	var myDate = e.Values["MyDate"].ToString();
	e.Values["MyDate"] = DateTime.Parse(myDate);
}
 
private static void MyList_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
	var myDate = e.Values["MyDate"].ToString();
	e.Values["MyDate"] = DateTime.Parse(myDate);
}

I realize this is not a hard fix – but the annoying part was figuring out why it was happening in the first place!!

Anyway, I hope this whinge helps someone else solve this problem… :)

If you liked this post, share it!
Facebook Twitter Linkedin Delicious Digg Stumbleupon Email

One thought on “.NET ObjectDataSource ignores culture information when inserting and updating

  1. Thanks for this post, it helped me a lot.
    In .NET Framework 4.5 Objectdatasource got a property called ParsingCulture, that fix this problem.

    Regards!

Leave a Reply

Your email address will not be published. Required fields are marked *