Results 1 to 7 of 7

Thread: date time error

  1. #1

    Thread Starter
    Fanatic Member karthikeyan's Avatar
    Join Date
    Oct 2005
    Location
    inside .net
    Posts
    919

    date time error

    hi,
    i am retreving date from database and dispalying in datetimepicker.
    while doing this i am getting error as: "String was not recognized as a valid DateTime"

    how to rectify this error:
    this is my code:
    msgbox(ds.Tables(0).Rows(0)("expiry_date"))
    i am getting 17/01/2008 as result

    HRUser_dtp_expdate.Text = ds.Tables(0).Rows(0)("expiry_date")

    so please help me to rectify this error

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

    Re: date time error

    Don't store dates in a database as text. Store them as dates. That way there's no possible way that an issue can occur. Would you store numbers in text fields? No you wouldn't, so what reason is there to store dates in text fields?
    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
    Fanatic Member karthikeyan's Avatar
    Join Date
    Oct 2005
    Location
    inside .net
    Posts
    919

    Re: date time error

    hi jmcilhinney,
    i need to convet the following string as datetime

    dim s as string="20/01/2007"

    how to convert this string as datetime,please show me some example code

    show me some sample code please

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

    Re: date time error

    You convert a string to a Date using Date.Parse, .ParseExact, .TryParse or .TryParseExact.

    Having said that, you should avoid having to perfrom any conversions if possible. Where is that string coming from? If you're having the user enter it in a TextBox then may I suggest using a DateTimePicker instead?
    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

  5. #5

    Thread Starter
    Fanatic Member karthikeyan's Avatar
    Join Date
    Oct 2005
    Location
    inside .net
    Posts
    919

    Re: date time error

    hi jmcilhinney, actually i have my date in my database in the of sqlserver date format.but i am converting and retreiving and showing in datetime picker in "dd/mm/yyyy" format.

    my date picker has the custom formate as "dd/mm/yyyy"

    while retreiving i am getting date as string.so that only i want ot convert string as datetime.so show me some example code please

  6. #6

    Thread Starter
    Fanatic Member karthikeyan's Avatar
    Join Date
    Oct 2005
    Location
    inside .net
    Posts
    919

    Re: date time error

    hi jmcilhinney
    i used like this:
    Dim d As DateTime = Date.Parse("20/01/2007")

    but it also shows error not valid datetime.so help me to do

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

    Re: date time error

    I'm obviously not making myself clear. Do NOT, at ANY point, convert a Date to a String or a String to a Date because there is no need. You say your database contains datetime values, and a DateTimePicker contains a DateTime value too. Get the value from the database and assign it to the Value property of the DateTimePicker to display it. When you're done you get the Value property of the DateTimePicker and assign it back to the database. No strings, no converting, no problem.
    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

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