Hello Friends,

I need help to get the time value from MSSQL database to VB.Net Datetimepicker

I have a datetimepicker but the custom format is set to HH:mm tt so that I can get eg 11:20 AM

this value I have stored in database with the help of Label4.Text = DateTimePicker3.Value.ToShortTimeString

but now if I want to get the same value from database to datetimepicker what shud be done???

please do help me on this

Thanks in advance

Fetch the DateTime column value from the database, Cast it to DateTime type and assign this value to the DateTimePicker1.Value property.

Something like this:

IF reader.Read() Then
   DateTimePicker1.Value=reader.GetDateTime(0) 
End IF

I am getting an error 'Specified cast not valid'

If the database value is retrieved in a label or variable then how can I get the label or variable value to datetimepicker

Edited 4 Years Ago by poojavb

One thing I almost always have to do when working with dates from SQL server, is do a string split on the data coming in, seperating the date and the time.

Dim tmp As String = dt.Rows(0).Item(0)
Dim tmpAr() As String = tmp.Split(" ")
'tmpAr(0) will be the Date'
'tmpAr(1) will be the Time'

Edited 4 Years Ago by Begginnerdev: edit

Date is not stored in the database only the time value is stored because while storing the value in database I am using

DateTimePicker3.Value.ToShortTimeString

so only time value like 1:23 AM is getting stored in the database....

and I want to retrive the same value in datetimepicker....is it possible???

else is there any other way....I have set the datetimepicker custom format to HH:mm tt

i think first you have to set then format of your datetime picker so that it can only show the time then just simple bind it with your datasource.

Regards

Yes I have set the datatimepicker format to custom and the custom format as HH:mm tt....the value is getting correctly stored in database...but difficult to retrieve....can u please explain in detail....

If your data column type is a Date/Time, both the DATE and the time will be stored.....

Or are you just storing the value in a varchar?

Since I dont want date first i tried with the Time datatype in mssql and then later with varchar...but both did not work out....

No I just want time...

from datetimepicker1 i have time and in ms sql also time....but I am not able to get the data from database to picker...

This article has been dead for over six months. Start a new discussion instead.