Thursday, 26 July 2012

ObjectDataSource Ignores Culture Information When Updating

"When using a business object with the ObjectDataSource control, culture information seems to be ignored when property values are extracted from the TextBox control generated by a BoundField. The same is true when a TemplateField is used." from here.
And, yes it is true. I encountered this problem when I was working on a page using FormView and ObjectDataSource to grab some values including a date value into the database. I used jQuery Datepicker to tie it to my date text box and because the website will be used in Australia, then I changed the date format of Datepicker to Australian format which is dd/MM/yyyy (in .NET) like this:


<script type="text/javascript">
    $(document).ready(function () {
        $('#DateTextBoxID').datepicker({ dateFormat: "dd/mm/yy" });
    });
</script>

Then, happily added below setting to my web.config file to change the culture to en-AU.


    <globalization culture="en-AU" uiCulture="en-AU"/>

When, I clicked the update button of my FormView I got error.

The problem was that my entered value for the date field was 27/07/2012 and if you pars it in US date formatting which is (MM/dd/yyyy) of course we don't have 27th month! If I had entered 12/07/2012 then everything seems working except that the date was wrong into the database.
Ok, this was the problem and you can find some workarounds for this "bug" at above link under Workarounds tab. I chose to add update and insert parameters for that date field to my ObjectDataSource as below:


    <InsertParameters>
        <asp:Parameter Name="DateField" Type="DateTime" />
    </InsertParameters>
    <UpdateParameters>
        <asp:Parameter Name="DateField" Type="DateTime" />
    </UpdateParameters>


By adding these parameters, ObjectDataSource will use your globalization settings and it did the job for me.

Happy Globalization!

2 comments:

  1. I have spend few hours fighting with this error until i saw your solution.
    So a big THANKS!!!! for you :)

    ReplyDelete
  2. You're welcome. good to see this post helped someone.

    ReplyDelete