Date and Time

M

Michael Turner

Hi Guys

I have two DateTime pickers one shows the Date and the other the time, this
is a requirement of the solution. The problem I have is that when the time
is saved to the sql database into a datetime field it uses the default date
which is a problem when I try to sort, what I wanted to know is there a way
for me to set the date in the time picker to the date in the date picker?

Any help greatly appreciated.

Thanks
Mike
 
N

Not Aaron

I am going to assume that you HAVE to store the time into a datetime
field within SQL.

Before you save the values you can store the datetime value by taking
the date value of the date picker and putting the time value behind it.
Then you can through the time value into your db with the correct date
in front of it. This seems a tad askew though

hth
 
M

Michael Turner

OK, if you can think of a better way the onyl requirement is that the date
is in one picker and the time is in another, please let me know I would be
more than interested, I am taking over someone elses project so I am just
trying to fix the problems so a different way would suit me fine.

Mike.
 
J

Jay B. Harlow [MVP - Outlook]

MIchael,
You should be able to set the Value of your Time picker to the Date of the
Date picker & the Time of the Time picker, when the Date picker's value
changes.

Something like:

Private Sub pickerDate_ValueChanged(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles pickerDate.ValueChanged
pickerTime.Value =
pickerDate.Value.Date.Add(pickerTime.Value.TimeOfDay)
End Sub

Private Sub pickerTime_ValueChanged(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles pickerTime.ValueChanged
Me.labelDateTime.Text = pickerTime.Value.ToString()
End Sub

Where pickerDate is a DateTimePicker for the Date value & pickerTime is a
DateTimePicker for the Time value.

I would consider encapsulating both controls into a new User Control to
allow easier reuse & better encapsulation...

Hope this helps
Jay
 
J

Jay B. Harlow [MVP - Outlook]

Michael,
Do you have a single datetime field on the database or 2 datetime fields?

I would expect a single datetime field.

Using the code I posted earlier you can use 2 DateTimePickers, but store
both values in a single database field.

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top