AddDays exception when subtracting value

P

patrick

I have the following code in ASP.NET 2.0 (C#)

DateTime datStartDate;
datStartDate = calMonth.SelectedDate; //calStart is a calendar
control
datStartDate = datStartDate.AddDays(-6);

and it gives me the following error:

System.ArgumentOutOfRangeException was unhandled by user code
Message="The added or subtracted value results in an un-representable
DateTime.\r\nParameter name: value"
Source="mscorlib"
ParamName="value"
StackTrace:
at System.DateTime.AddTicks(Int64 value)
at System.DateTime.Add(Double value, Int32 scale)
at System.DateTime.AddDays(Double value)
at Controls_diaryweek.Page_Load(Object sender, EventArgs e) in
c:\Documents and Settings\Padraig\My Documents\Visual Studio
2005\WebSites\TheFitnessDiary\Controls\diaryweek.ascx.cs:line 18
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp,
Object o, Object t, EventArgs e)
at
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender,
EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)


BTW - adding a positive number of days works fine.

Any ideas would be appreciated!!

Thanks,
Patrick.
 
K

Karl Seguin

What's the initial value of SelectedDate? My guess it isn't what you
expect.

Karl
 
P

patrick

Whoops, its always the obvious!! I had not set the initial date of the
calendar control, I had assumed it had defaulted to today! What threw
me was that I could add a day, but not subtract.

Thanks for the smack in the head!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top