Take the 2-minute tour ×

I have a MVC3 application in which i want to set a timespan to for example 2 days and 5 hours. when i enter 02:05:00:00 it gives me the following exception:

System.OverflowException: SqlDbType.Time overflow.  Value '2.05:00:00' is out of range.  Must be between 00:00:00.0000000 and 23:59:59.9999999.

When i enter 05:00:00 it correctly saves 5 hours into the database. according to MSDN timespan has a property for days. How do i correctly set the days?


public class ProductionTimeVM
    public TimeSpan DefaultTime { get; set; }

In my view i just use:

@Html.TextBoxFor(x => x.DefaultTime)

For my controller:

public ActionResult SaveProductionTime(ProductionTimeVM vm)
   ProductionTime productionTime = new ProductionTime();
   productionTime.Default = vm.DefaultTime;

   //some more code

Any idea's?

share|improve this question
The error message shows a . where you should have a :. –  Oded Oct 12 '12 at 9:09
yes, it does. It did suprise me too because i do enter a : –  middelpat Oct 12 '12 at 9:10
How are you passing the value to the DB? –  Oded Oct 12 '12 at 9:10
I entered some code from my controller as an edit. I find it odd that just a time saves correctly but a time with a day throws an error –  middelpat Oct 12 '12 at 9:14
"SqlDbType.Time overflow." clearly indicates it's SQL server that causes the issues, and not ASP.net MVC. –  CodesInChaos Oct 12 '12 at 9:29

1 Answer 1

up vote 5 down vote accepted

You are probably saving a TimeSpan value to column with Time datatype in database. Time can only express a time of day, so you can't save TimeSpans larger than a day.

I use columns with bigint data type to store and retrieve ticks of TimeSpan. I would also like to know if there is a better alternative. This behavior is like default for some popular ORMs and it's misleading a lot of people I think.

share|improve this answer
+1 Not 'probably' but definitely because it's a SqlDbType.Time overflow. Best thing to do will be to store the Ticks value in a bigint –  Andras Zoltan Oct 12 '12 at 9:28
hmm, you're right! how could i have not thought about that. I use EF and the column type does say "Time". What should i use instead? –  middelpat Oct 12 '12 at 9:28
I'm am testing your answer. But i'm almost sure it should work –  middelpat Oct 12 '12 at 9:36
Works great! thanks for your effort –  middelpat Oct 12 '12 at 9:50
@middelpat Happy to help :) –  Ufuk Hacıoğulları Oct 12 '12 at 9:51

Your Answer


