Search code examples
asp.net-mvc-3c#-4.0

Year, Month, and Day parameters describe an un-representable DateTime Exception


I'm adding an object to a list within an ASP.NET MVC 3 application using the following code but one of the properties of the object is giving me difficulties.

ls.Add(new UserRoleModel { UserRoleId = 1, UserName = "Paul", InsertDate = new DateTime(05, 24, 2012),InsertProgram="sqlplus",InsertUser="sp22",Role="Implementation Co-corindator"});

This builds but when I go to the relevant page i get an Exception with the following type:

Exception Details: System.ArgumentOutOfRangeException: Year, Month, and Day parameters describe an un-representable DateTime.

I've tried removing the 0 in the month but I get the same exception.


Solution

  • The DateTime constructor you are calling accepts parameters in the order year, month, day.

    You are providing them in the order month, day, year -- which ends up trying to assign meaningless values. The documentation is pretty clear on what the allowed values are and what happens if you pass 2012 for the "day" value.