Results 1 to 12 of 12

Thread: String was not recognized as a valid DateTime

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2005
    Posts
    17

    String was not recognized as a valid DateTime

    Dear All,

    I have this piece of code:-

    CType(r.FindControl("calDate"), Calendar).SelectedDate = DateTime.Parse(CType(r.FindControl("txtEvDate"), TextBox).Text())

    And its giving me an error:-

    String was not recognized as a valid DateTime

    How can I solve this?

    Thanks for your help and time

    Johann

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: String was not recognized as a valid DateTime

    You can make sure that the txtEvDate control contains a valid date. DateTime.Parse only recognises certain date formats and will throw an exception if it doesn't find one. If you're using a specific date format then you can use ParseExact. If you're using .NET 2.0 you can use TryParse. Otherwise you need to validate the value in the TextBox. Don't just assume that the user will enter the correct value. They could enter "purple monkey dishwasher" if they wanted. I'm sure you won't be surpridsed to learn that that wouldn't parse to a date.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2005
    Posts
    17

    Re: String was not recognized as a valid DateTime

    I tried to go around it like this:-

    If Request.QueryString("Cmd") = "Add" Then
    CType(r.FindControl("calDate"), Calendar).SelectedDate = DateTime.Today.ToShortDateString
    Else
    CType(r.FindControl("calDate"), Calendar).SelectedDate = CType(r.FindControl("calDate"), Calendar).SelectedDate.ToShortDateString
    End If

    And the "Add" is working fine, displaying today's date. However when I come to edit, the following error is cropping up

    Ticks must be between DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks. Parameter name: ticks

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jan 2005
    Posts
    17

    Re: String was not recognized as a valid DateTime

    I was inspecting a bit with the debugger.

    The problem seems to arise when there is a value in the date bigger than 12.

    For example, 11/06/2005 works fine

    However 14/11/2005 does not.

    Have no idea why it is not accepting that value

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: String was not recognized as a valid DateTime

    Presumably Date.Parse is assuming that the first number is the month. I'm not sure but I'd expect that its behaviour is determined by the current culture setting on the local machine. If that's not the case then you'll have to use ParseExact and tell it exactly what format to use.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jan 2005
    Posts
    17

    Re: String was not recognized as a valid DateTime

    Can you tell me how to do that since I have never done it before

    Thanks for your help

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: String was not recognized as a valid DateTime

    There are a number of ways. I suggest you take a look at the appropriate MSDN topics for more info, as you always should when dealing with unfamiliar types or members. MSDN won't tell you everything but it should always be your first port of call. In this case, if you know you want dd/mm/yyyy format then the easiest way is to use:
    VB Code:
    1. Dim myDate As Date = Date.ParseExact(myString, "dd/MM/yyyy", Nothing)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jan 2005
    Posts
    17

    Re: String was not recognized as a valid DateTime

    I tried this code:-

    Dim myDate As DateTime = DateTime.ParseExact(CType(r.FindControl("txtEvDate"), TextBox).Text(), "dd/MM/yyyy HH:mm", New DateTimeFormatInfo)
    CType(r.FindControl("calDate"), System.Web.UI.WebControls.Calendar).SelectedDate = myDate.ToShortDateString

  9. #9
    Frenzied Member Phill64's Avatar
    Join Date
    Jul 2005
    Location
    Queensland, Australia
    Posts
    1,201

    Re: String was not recognized as a valid DateTime

    hey about the culture thing Jm,

    know of any way to detect the culture setting? so that i can make my app issue a warning to the user?

    you see parseExact is all good, but the date picker boxes i have on some forms would still look funny, hehe

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Jan 2005
    Posts
    17

    Re: String was not recognized as a valid DateTime

    I am trying to figure out this whole globalization thing cause I have never used it!

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: String was not recognized as a valid DateTime

    Quote Originally Posted by Phill64
    hey about the culture thing Jm,

    know of any way to detect the culture setting? so that i can make my app issue a warning to the user?

    you see parseExact is all good, but the date picker boxes i have on some forms would still look funny, hehe
    Look at the CultureInfo class and its CurrentCulture and CurrentUICulture properties.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Jan 2005
    Posts
    17

    Re: String was not recognized as a valid DateTime

    ok i solved it

    I had to do the format like this:-

    Dim myDate As DateTime = DateTime.ParseExact(CType(r.FindControl("txtEvDate"), TextBox).Text(), "MM/dd/yy", New DateTimeFormatInfo)

    and now its workin!

    I know this is not the best format but I am doing some amendments on an old system, and I wish to keep the same format it originally uses.

    Thanks for your help!

    Johann

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width