Search code examples
c#sql-serverwpftimetextbox

WPF: Using a "time" textbox to search a database using seconds


I have a datatable with a column Total which displays total time in seconds. (Since this can be over 24 hours i used DateDiff to calculate the time in seconds, this returns a INT) as below:

SET tmTotals = DATEDIFF(Second, tmStartTime, tmEndTime)

the datatable looks like this:

StartTime                 EndTime                   Total
-----------------------------------------------------------        
2018-12-03 00:00:19.257   2018-12-04 00:00:23.288   86404
2018-12-02 10:13:32.586   2018-12-04 10:14:57.298   172885
2018-12-04 12:07:50.636   2018-12-04 18:04:25.526   21395
2018-12-04 03:15:25.061   2018-12-04 13:15:34.665   36009
2018-12-03 20:56:12.947   2018-12-04 03:11:07.992   22495
2018-12-02 00:11:46.020   2018-12-04 00:29:55.051   173889

In my WPF application i want to use two textboxes to search between two Total times in the table. I want these textboxes formatted as hhh:mm:ss (time can be over 100 hours). How can I get this to an Intvalue so it's usable in the StoredProcedure?

WPF app image: DataTable searching on active time

With times under 24 hours I used, but this can't be used now:

CONVERT(varchar, DATEADD(second,  DATEDIFF(second, tmStartTime, tmEndTime), 0), 108)

to calculate the total time and in my WPF app I did:

<TextBox Name="Activeto" Text="{Binding Path=TotalEnd,  StringFormat= \\hh\\:mm\\:ss }" Width="60"/>

and code-behind:

TimeSpan timeSpanStart = TimeSpan.Parse(Activefrom.Text);

to convert the string to a timespan which could be used to search between to times.


Solution

  • I have found a solution. In the end i simply seperated the time input textboxes to three seperate textboxes so: hh : mm : ss. In th code-behind i calculated te input to seconds and added them:

    int TotalBegin = (int.Parse(TotalStarthh.Text) * 3600) + (int.Parse(TotalStartmm.Text) * 60) + (int.Parse(TotalStartss.Text));
    int TotalEnd = (int.Parse(TotalEndhh.Text) * 3600) + (int.Parse(TotalEndmm.Text) * 60) + (int.Parse(TotalEndss.Text));
    

    Maybe not the best solution, but it works for me!