Results 1 to 10 of 10

Thread: Date in DatePicker

  1. #1

    Thread Starter
    Addicted Member Tengkorak's Avatar
    Join Date
    Nov 2006
    Posts
    240

    Date in DatePicker

    i have problem when display date data from database to display on DatePicker.
    the error is:
    "Conversion from string "30/08/2011" to type 'Date' is not valid."

    this vb code:
    vb.net Code:
    1. dtPO.Value = CDate(dtt.Rows(0).Item("po_date"))

    data in database is "30/08/2011", not error if data "07/04/2011"

    at other pc this can run well.

  2. #2
    Hyperactive Member marniel647's Avatar
    Join Date
    Aug 2010
    Location
    MSDN Library
    Posts
    259

    Re: Date in DatePicker

    Quote Originally Posted by Tengkorak View Post
    i have problem when display date data from database to display on DatePicker.
    the error is:
    "Conversion from string "30/08/2011" to type 'Date' is not valid."

    this vb code:
    vb.net Code:
    1. dtPO.Value = CDate(dtt.Rows(0).Item("po_date"))

    data in database is "30/08/2011", not error if data "07/04/2011"

    at other pc this can run well.
    maybe the format is mm/dd/yyyy try to input 12/08/2011

  3. #3

    Thread Starter
    Addicted Member Tengkorak's Avatar
    Join Date
    Nov 2006
    Posts
    240

    Re: Date in DatePicker

    i tried modify code :
    vb Code:
    1. tPO.Value = CDate(String.Format("{0:mm/dd/yyyy}", (dtt.Rows(0).Item("po_date"))))
    but same error

  4. #4
    Hyperactive Member marniel647's Avatar
    Join Date
    Aug 2010
    Location
    MSDN Library
    Posts
    259

    Re: Date in DatePicker

    Quote Originally Posted by Tengkorak View Post
    i tried modify code :
    vb Code:
    1. tPO.Value = CDate(String.Format("{0:mm/dd/yyyy}", (dtt.Rows(0).Item("po_date"))))
    but same error
    try this :
    vb Code:
    1. tPO.Value = CDate(dtt.Rows(0).Item((String.Format"{0:mm/dd/yyyy}")"po_date")))

    i dont think it's correct

  5. #5

    Thread Starter
    Addicted Member Tengkorak's Avatar
    Join Date
    Nov 2006
    Posts
    240

    Re: Date in DatePicker

    still can not

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Date in DatePicker

    The error message says that it's trying to convert a String to a Date. Why do you have a String in the first place? If the data is dates then why aren't you storing it as Dates?

  7. #7

    Thread Starter
    Addicted Member Tengkorak's Avatar
    Join Date
    Nov 2006
    Posts
    240

    Re: Date in DatePicker

    at this code
    vb Code:
    1. dtPO.Value = CDate(dtt.Rows(0).Item("po_date"))
    i tried covert string to date type

  8. #8

    Thread Starter
    Addicted Member Tengkorak's Avatar
    Join Date
    Nov 2006
    Posts
    240

    Re: Date in DatePicker

    the big question for me is, why an error occurs when I open my program it from another pc. whereas if I use the other pc no problem.
    whether this is related with the regional settings in windows or whether ?

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Date in DatePicker

    It is related to regional settings. CDate assumes that a String is in the appropriate format based on regional settings. On a system where a format like dd/MM/yyyy is used, "30/08/2011" will be converted to August 30, 2011. On a system where a format like MM/dd/yyyy is used, that String doesn't represent a valid date. If you want to convert a String into a Date using a specific format regardless of regional settings, you should use Date.ParseExact or, if you're not 100% sure the String is valid, Date.TryParseExact.

    Of course, if you stored the data as Dates instead of Strings, there'd be no conversions performed so format would never be an issue. If you have no choice but to use Strings because this is an existing database or some other reason, use ParseExact or TryParseExact. If it's within your power to change the database schema, do so. If you do it right in the first place, you don't have to worry about fixes later.

  10. #10

    Thread Starter
    Addicted Member Tengkorak's Avatar
    Join Date
    Nov 2006
    Posts
    240

    Re: Date in DatePicker

    thank you, very helpful

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