ObjectDataSource – Cannot convert value of parameter value from System.String to System.DateTime

One bug I’ve come across a few times and again just recently is when using an ObjectDataSource with a GridView to update dates.  When doing the update the ObjectDataSource always uses the en-US culture and not the culture defined for the application.

The bug has been acknowledged by Microsoft but never fixed.

If you are in the US then you’ll have no problems, but if you are using a culture that uses the dd/MM/yyyy date format such as the UK or Australia you data will display in the correct format on your page, but you will get the following error when you try to save:

Cannot convert value of parameter ‘value’ from ‘System.String’ to ‘System.DateTime’

If your date is valid for the US format then you won’t get the error, but you day and month will be swapped over which is probably even more annoying.

This can be fixed by hooking into the RowUpdating event of the GridView.  The RowUpdating event is fired before the update takes place and it is simple to find any date values and change the format of them:

using System.Collections;
using System.Globalization;

protected void gvStudents_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    foreach (DictionaryEntry de in e.NewValues)
    {
        DateTime currentDate = DateTime.MinValue;
        if (de.Value != null && DateTime.TryParse(de.Value.ToString(), out currentDate))
        {
            e.NewValues[de.Key] = currentDate.ToString(new CultureInfo("en-US"));
        }
    }
}
Posted on by Joe in ASP.NET, C#

9 Responses to ObjectDataSource – Cannot convert value of parameter value from System.String to System.DateTime

  1. flip.be

    Exactly what I was looking for. Tnx!

  2. Robert Kafarski

    Hi,

    I had exactly same issue, but found out it was not a framework bug.
    The error occurred only when I was using the built-in web server.
    Try switching your project to IIS instead and see if it helps.
    There is no need for any workarounds…

    Regards,

    Robert

  3. Joe

    Hi Robert

    Thanks for the insight.

    When I first came across the issue it definately was a framework bug that was verified by Microsoft here.

  4. Luis Guimarães

    What if you put this?
    – EnableScriptGlobalization=”true”

    I think it works

  5. Luis Guimarães

    ASP.NET 3.5-> What’s new:

    Support for script globalization and localization by using client script. Globalization enables you to display dates and numbers based on a culture value (locale). Localization enables you to specify localized content (text, images, and so on) in client components for UI elements or exception messages.

    Source:
    http://msdn.microsoft.com/en-us/library/s57a598e.aspx

    So, I think it works
    EnableScriptGlobalization=”true”

  6. Joe

    Luis

    EnableScriptGlobalization is something completely different to the subject of this post.

    EnableScriptGloablization and EnableScriptLocalization are used when displaying information with JavaScript.

    The problem outlined in my post is a bug with the ObjectDataSource when performing an update, not when displaying data. The ObjectDataSource uses ConvertFromInvariantString which is what causes the issue and therefore the workaround is required.

    Robert

    I tried using IIS as you suggested but this still doesn’t solve the issue as the ObjectDataSource is still using ConvertFromInvariantString.

  7. Arturo HG

    Muchas gracias!!!

  8. slow worm

    Perfect. No nonsense fix. Thank you.

  9. Yeldos

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
    try
    {
    object[] keys = (object[])Array.CreateInstance(typeof(object), e.NewValues.Count);
    e.NewValues.Keys.CopyTo(keys, 0);
    object[] Values = (object[])Array.CreateInstance(typeof(object), e.NewValues.Count);
    e.NewValues.Values.CopyTo(Values, 0);
    for (int i = 0; i < keys.Length; i++)
    {
    DateTime currentDate = DateTime.MinValue;
    if (Values[i] != null && DateTime.TryParse(Values[i].ToString(), out currentDate))
    {

    e.NewValues[keys[i]] = currentDate.ToString(new CultureInfo("en-US"));

    }
    }
    }
    catch (Exception)
    {

    throw;
    }
    }

Add a Comment