I have a column (datatype is time(7)) in my SQL Server database to store the time. When I try to retrieve that value from the database and display it in a DateTimePicker (format set to display time only), it throws an InvalidCastException error.
This is the line where the casting happens.
r = com.ExecuteReader();
dtpOutTime.Value = (DateTime)r["OutTime"];
I can understand why the error is thrown but I can’t figure out a way to correct it.
How can I display only the time from database in a DateTimePicker?
Thank you.
For
System.Data.DbType.Timeyou use the .NET Framework typeSystem.TimeSpan.for example:
(if you want to add the TimeSpan to
Todaymidnight and the timespan would be 6 hours, the result would be10/22/2012 6:00:00 AM)