This was working fine before I moved the page to a new web server. I now get an error that the "String was not recognised as a valid DateTime".
DateTime.Now.ToShortDateString
is 5/30/2012.
?
There's no reason to get a DateTime, render it as a string, and then re-parse it again. You weren't passing your UK culture to the ToShortDateString, so that's probably why it wasn't working anymore. Either way: don't do that.
Menno
cultureinfoDateTimeFormat
Marked as answer by Vivani on May 30, 2012 03:40 PM
Thanks. Some of the other parameters I have are string values passed from a DetailsView control. I needed to use the UK DateTimeFormat for these and so just got a bit overenthusiastic and applied it to all my dates!
Vivani
Member
6 Points
25 Posts
Problem with datetime.parse
May 30, 2012 02:24 PM|LINK
I have the following code on my webpage:
Dim ukCulture As CultureInfo = New CultureInfo("en-GB")
cmd.Parameters.Add("@MODIFY_DATE", SqlDbType.DateTime).Value = DateTime.Parse(DateTime.Now.ToShortDateString, ukCulture.DateTimeFormat)
This was working fine before I moved the page to a new web server. I now get an error that the "String was not recognised as a valid DateTime". DateTime.Now.ToShortDateString is 5/30/2012.
Any help with this appreciated. Thanks
cultureinfo DateTimeFormat
urenjoy
Star
12347 Points
1857 Posts
Re: Problem with datetime.parse
May 30, 2012 02:28 PM|LINK
1. No need to convert in string and again convert in date.
cmd.Parameters.Add("@MODIFY_DATE", SqlDbType.DateTime).Value = DateTime.Now;
2. If you have to pass date from string variable Try to convert string in yyyy-MM-dd string format and then parse it.
Check following:
http://techbrij.com/227/string-to-datetime-formatexception-and-asp-net
Menno van de...
Contributor
2764 Points
391 Posts
Re: Problem with datetime.parse
May 30, 2012 02:29 PM|LINK
What is wrong with
cmd.Parameters.Add("@MODIFY_DATE", SqlDbType.DateTime).Value = DateTime.Nowor, if you really only need the date
cmd.Parameters.Add("@MODIFY_DATE", SqlDbType.DateTime).Value = DateTime.Now.Date?
There's no reason to get a DateTime, render it as a string, and then re-parse it again. You weren't passing your UK culture to the ToShortDateString, so that's probably why it wasn't working anymore. Either way: don't do that.
Menno
cultureinfo DateTimeFormat
Vivani
Member
6 Points
25 Posts
Re: Problem with datetime.parse
May 30, 2012 03:40 PM|LINK
Thanks. Some of the other parameters I have are string values passed from a DetailsView control. I needed to use the UK DateTimeFormat for these and so just got a bit overenthusiastic and applied it to all my dates!
cultureinfo DateTimeFormat