Last post May 05, 2011 08:21 PM by jalparushi
Feb 19, 2008 08:47 PM|ewallig|LINK
In ASP.net, is there any elegant way to handle a set of time inserts from a form when the 2nd time is past midnight?
Specifically, I have a form with 2 textboxes on it (startTime and endTime) that are set up to accept time values (using AJAX MaskedEditExtender for formatting/validation - pretty cool). This data is posted to a sub that enters the data into a table (T_Details).
However, I've noticed that the data inserted as part of the record (SQL field is smalldatetime) doesn't take into account the fact that a time value past 23:59:59 in the "endTime" textbox is a time on the next day - it simply rolls to an A.M. date for the
same day as the date for the pre-midnight value from the "startTime" textbox.
I'm sure that I can simply do some conditional coding and modify the date if necessary but is there a better way to do it? Thanks as always...this forum is a great resource [:D]
Feb 20, 2008 01:03 AM|Matt-dot-net|LINK
You have perfectly described the
smalldatetime type. You need to switch to a
Hope this helps!
Feb 20, 2008 12:10 PM|ewallig|LINK
Thanks for the reply but no joy - Time2 is still showing the same date as Time1 and in looking at it, I may be asking SQL to do something that it doesn't want to do. In submitting the times from text boxes, the date is simply being added onto the data by
virtue of the "Convert.ToDateTime" declaration in my parameters - see below:
objCommand.Parameters.AddWithValue("@Time2", SqlDbType.DateTime).Value = Convert.ToDateTime(Time2.Text)
No date is being entered by the end user, just the times so the code is appending the current date to it. This is a problem for me as the data may be entered retroactively - the user picks a date from a calendar object. I'll need to address
that issue with code anyway by getting the picked date so I'm guessing that I will need to do some code work for the incremented date...unless I'm missing something - can you advise?
Feb 20, 2008 02:03 PM|ewallig|LINK
OK, so building on my last post, I need a way of comparing the values from Textbox "Time1" and Textbox "Time2" and if the value in "Time2" goes past 23:59 (00:30 for example), I need to increment the date for the SQL field "Time2" by one day - best approach
Feb 20, 2008 04:35 PM|Matt-dot-net|LINK
Ok, you have some unclear business logic taking place. You have to write the rules because only you know them. For what time values do you want the date to be today's date? For what values do you want the date to be tomorrow's date? You will need to
write code to do what you want.
Here is an example of some code to this. This code omits the second textbox and assumes that you want the earliest entry for "today" to be 3:00 AM.
DateTime todayCutOff = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Date, 3, 0, 0);
DateTime t1 = DateTime.Parse(time1.Text);
if (t1 < todayCutOff)
//12:00AM <= Time1 < 3:00AM
t1 = t1.AddDays(1.0);
Feb 20, 2008 07:35 PM|ewallig|LINK
I think that will help some - will advise in the morning. As far as the business logic, in the grand scheme all i'm trying to do at this point is to look at the data in the second text box and make sure that it:
Example: User enters 23:00 into textbox "Time1" and enters 00:30 into textbox "Time2". If Time2 is past 00:00 then upon the postback that calls the INSERT sub (in the code-behind) - I'm already doing some other data conversions in this sub so it seems logical
to do this one as well.
I may actually need to re-think this a bit. I can see some potential problems with users "fumble-fingering" the data in Time2 and the page thinking that it is a valid time frame, incrementing the date and inserting an inaccurate record into the table. This
little project tracks flight times and aircraft utilization so the data has got to be accurate. In this example, if a user incorrectly enters 03:30 instead of 23:30, a 1/2 hour flight is turned into a 4 hour flight through an inaccurate end time value that
the validation wouldn't catch because there was code that simply assumed the bad value in Time2 was actually a reference to time on the next day.
Oh well, back to the drawing board - thanks anyway for your help...[:^)]
Feb 20, 2008 11:35 PM|Matt-dot-net|LINK
The only solution is to ask the user to input the date as well.
Problems with your requirements:
Feb 21, 2008 10:55 AM|ewallig|LINK
OK, well, it's not overly elegant but I added a checkbox to the page that users click if the time data traverses 00:00 and then built in code that looks for that checkbox data. If the box is checked, the date is incremented by one day (your code) and INSERTED
into the table along with the time. A custom validator prevents the INSERT if the value in Time2 appears to be <= Time1 and the "overnight" checkbox is not checked.
Thanks again for your help...[Yes]
May 05, 2011 08:21 PM|jalparushi|LINK
This is realy not a good solution
i also want to do same like this but i dont like your check box idea but could you just tell me after on wards which logic you applied if checkbox = true then