In my application I have a datagrid that populates with info from a SQLserver DB. Amongst other things the data which populates the grid includes times. Now I struggled for some time getting the data from the DB to look the way I wanted. In the end my sql comman included the following : –
LEFT(CONVERT(VARCHAR(8), te.StartTime, 8), 5) AS [Start Time],
LEFT(CONVERT(VARCHAR(8), te.FinishTime, 8), 5) AS [Finish Time]
Which nicely shows times in the format of 8:00 & 10:00 etc.
However, this causes a problem later on in my application when I need to use the info in the datagrid to calculate the difference between two times. This is because (I believe) my SQL command (above) has coverted the times to strings. So when I try the following for example:
DateTime startTime = (DateTime)varValue;
DateTime endTime = (DateTime)varFinish;
I get the error message ‘invalid cast’. So I’m faced with converting these strings back into DateTime in order to perform any calculations with them. I can’t simply add the original time fields to the SQL command, because sometimes the times in the grid will be new times input by the user, so those will be strings anyway.
How should I best convert the user input e.g. “8:00” into a datetime – so that I can perform functions like calucating the difference between two times. Or alternatively, should I be going about the whole timesheet issue a different way.
Any advice will be greatly appreciated. I’m rather new to C# as you may have guessed.
Have you tried this?
Think this could solve your Problem?
You got now the current Date with your given times in the two parameters