locked
Problem with datetime.parse RRS feed

  • Question

  • User623455698 posted

    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

    Wednesday, May 30, 2012 10:24 AM

Answers

  • User1648350404 posted

    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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 30, 2012 10:28 AM
  • User560403387 posted

    What is wrong with

    cmd.Parameters.Add("@MODIFY_DATE", SqlDbType.DateTime).Value = DateTime.Now

    or, 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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 30, 2012 10:29 AM

All replies

  • User1648350404 posted

    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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 30, 2012 10:28 AM
  • User560403387 posted

    What is wrong with

    cmd.Parameters.Add("@MODIFY_DATE", SqlDbType.DateTime).Value = DateTime.Now

    or, 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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 30, 2012 10:29 AM
  • User623455698 posted

    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! Smile

    Wednesday, May 30, 2012 11:40 AM