|
-
Apr 7th, 2011, 10:32 AM
#1
Thread Starter
Addicted Member
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:
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.
-
Apr 7th, 2011, 10:37 AM
#2
Hyperactive Member
Re: Date in DatePicker
 Originally Posted by Tengkorak
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:
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
-
Apr 7th, 2011, 10:44 AM
#3
Thread Starter
Addicted Member
Re: Date in DatePicker
i tried modify code :
vb Code:
tPO.Value = CDate(String.Format("{0:mm/dd/yyyy}", (dtt.Rows(0).Item("po_date"))))
but same error
-
Apr 7th, 2011, 10:51 AM
#4
Hyperactive Member
Re: Date in DatePicker
 Originally Posted by Tengkorak
i tried modify code :
vb Code:
tPO.Value = CDate(String.Format("{0:mm/dd/yyyy}", (dtt.Rows(0).Item("po_date"))))
but same error
try this :
vb Code:
tPO.Value = CDate(dtt.Rows(0).Item((String.Format"{0:mm/dd/yyyy}")"po_date")))
i dont think it's correct
-
Apr 7th, 2011, 11:13 AM
#5
Thread Starter
Addicted Member
-
Apr 7th, 2011, 07:43 PM
#6
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?
-
Apr 8th, 2011, 12:32 AM
#7
Thread Starter
Addicted Member
Re: Date in DatePicker
at this code
vb Code:
dtPO.Value = CDate(dtt.Rows(0).Item("po_date"))
i tried covert string to date type
-
Apr 8th, 2011, 12:35 AM
#8
Thread Starter
Addicted Member
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 ?
-
Apr 8th, 2011, 12:55 AM
#9
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.
-
Apr 8th, 2011, 01:06 AM
#10
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|