C# – How to i fix this kind of exception? ArgumentOutOfRangeException

cmonthcalendarwinforms

The exception message returns:

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

The exception hits the monthCalendar1.SelectionStart

            DateTime check_date = monthCalendar1.SelectionStart;

            for (int index = 0; index <= 5; index++)
            {
                check_date = new DateTime(check_date.Year, check_date.Month, check_date.Day - index);

            }

I also tried this one. but another exception has occured TargetInvocationException

                DateTime check_date = monthCalendar1.SelectionStart;

                for (int index = 0; index <= 5; index++)
                {
                    check_date = check_date.AddDays(-index);

                }

Exception details:

System.ArgumentOutOfRangeException was unhandled
Message=Year, Month, and Day parameters describe an un-representable DateTime.
Source=mscorlib
StackTrace:
at System.DateTime.DateToTicks(Int32 year, Int32 month, Int32 day)
at System.DateTime..ctor(Int32 year, Int32 month, Int32 day)
at TestGetDates.Program.Main(String[] args) in C:\Documents and Settings\ngd11\My Documents\Visual Studio 2010\Projects\TestGetDates\TestGetDates\Program.cs:line 15
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:

Best Answer

  date_tax.Day - index

this might be producing a negative number that is causing this exception, debug and check its value.

Related Topic