May 12, 2006 at 10:13 am
I would like to save only the time in a date/time field in SQL Server 2000 database.
I have a datagrid where I am displaying the date in one column and the time arrived in another column. I want to save this to the database in the same format. Also, the user will be able to edit the date and time fields in the datagrid.
Does anyone have some asp.net code to do this? Thanks,
May 12, 2006 at 12:08 pm
You can get just the Time part of a DateTime with a ToLongTimeString or ToShortTimeString. e.g.:
Dim MyTime as DateTime
MyTime = MyDateTime.ToLongTimeString()
Then the SQL is easy...
"INSERT INTO MyTable (MyTime) VALUES(@MyTime)"
Where @MyTime is a param
SQLCommand.Parameters.Add("@MyTime",MyTime)
ToLongTimeString
May 12, 2006 at 1:35 pm
The following code isn't working, meaning no record is saved to the database:
dr.rec_checkin_time = Now().ToShortTimeString
This doesn't work either:
dr.rec_checkin_time = CType(Now().ToShortTimeString, DateTime)
And neither does this
Dim
MyTime As DateTime
MyTime = Now().ToLongTimeString()
dr.rec_checkin_time = MyTime
This works, however, it stores the current date and time.
dr.rec_checkin_time = Now()
I'm using a dataset and the insert statement runs after the user adds a row via the data grid. What am I missing here? Does anyone know? I would even be happy if I could store 1/1/1900 and the current time.
Thanks,
May 12, 2006 at 2:41 pm
I don't know what dr.rec_checkin_time is, but I would guess it is a datetime field of some sort?
Try:
dr.rec_checkin_time = CDate(Now().ToShortTimeString )
May 15, 2006 at 6:46 am
Yes, it is a datetime field, a dataset datarow.
Your code above works, however, the row is not being saved to the database.
I'm getting the following error: SQLDateTimeOverflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.
Do I need to include a default date like 1/1/1900 to the time so that it will save to the database. Or is there a way to make SQL do that part of it for me?
Thanks,
May 15, 2006 at 6:58 am
When you look at:
dr.rec_checkin_time
What value is in there before the Insert? Put a break point and a watch on it after you set it.
May 15, 2006 at 8:53 am
Dim MyTime As DateTime
Before MyTime is populated it is #12:00:00 AM#
After the line of code:
MyTime = CDate(Now().ToShortTimeString)
it is #10:44:00 AM#
May 15, 2006 at 12:29 pm
Huh, you shouldn't have any trouble storing that in a SQL DataTime. Try appending 1/1/1900 to it before you convert it back to a Date and see what happens.
May 15, 2006 at 1:05 pm
Yep, that works fine. I can now save the time, but not without 1/1/1900 in front of it. Thanks for your help!
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply