ObjectDataSource fails to parse string to DateTime

霸气de小男生 提交于 2019-12-02 00:45:16

问题


I have a textbox with value that stores ValidFrom form value:

31.01.2012

and cultures set to:

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

in web.config.

And now, ObjectDataSource update method:

    public static void UpdateLac(int id, DateTime ValidFrom)
    {
        /// ...
    }

fails as I get exception that string cannot be parsed. However date in format dd.mm.yyyy (31.01.2012) is valid en-GB format and can be parsed (as far as I know). I have tested it with following code:

            DateTimeFormatInfo dtfi = CultureInfo.CreateSpecificCulture("en-GB").DateTimeFormat;
            var date = DateTime.Parse("31.01.2012", dtfi);
            Console.Write(date.ToLongDateString());

So how come that ObjectDataSource internal conversion fails to convert string (31.01.2012) to DateTime in this example?


回答1:


As far as I know the culture info is loaded directly from the OS ( in this case windows), you can check on your regional setting for the format specified. This is a screenshot from my pc:

http://imageshack.us/photo/my-images/96/engbg.png/

As you can see the format for short date is: dd/MM/aa, so maybe there is something going on with your server regional settings or the input should be: 31/01/12 instead of 31.01.2012

Hope this helps.



来源:https://stackoverflow.com/questions/11294069/objectdatasource-fails-to-parse-string-to-datetime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!