Click here to Skip to main content
15,906,455 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
DateTimePicker2.Value.ToShortDateString(drr.GetValue(5))

This is my coding but it shows error only... i want to retrive the date from database sql 2005 to show it into combobox using vb.net.
Posted
Updated 1-Aug-11 2:21am
v2
Comments
Herman<T>.Instance 1-Aug-11 8:23am    
a) what is the error?
b) why you want to set the value of a datetimepicker if you want to show it in a combobox?
perhaps DateTimePicker2.Value = Convert.ToDateTime(drr.GetValue(5))

1) Read the value from the database.
2) Convert it to a DateTime.
2.1) If you stored this in an SQL Date type, then just cast it:
VB
Dim d As DateTime = drr("myDate")

2.2) If you stored it as a string, then convert it:
VB
Dim d As DateTime = DateTime.ParseExact(ddr("myDate"), "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture)

3) You can now convert it to the appropriate string for the ComboBox.

If you don't convert the string to DateTime, then you are stuck with the culture that you entered the info in: so if it is entered by a Japanese it will display as 2011-08-01, where as an American input would show as 08/01/2011. Neither is very helpful if your user is English...

Always store dates in Date fields: it gets around this problem!
 
Share this answer
 
VB
DateTimePicker2.Value = Format(CDate(rdr(5)), "dd/MM/yy")
 
Share this answer
 
v3
Sometimes, date format on ms sql server differs from system date format on local computer. Use:
SQL
SET DATEFORMAT ymd;
SELECT ...
FROM ...
WHERE (DateField=@aDate)

to temporary change date format.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900