Monday 12 October 2015

The added or subtracted value results in an un-representable DateTime.

Hello firend i was wirking with HttpCookie in asp.net and i need to flush cookie in order to perform some other task. I tried my code was working fine at initial stage but suddenly i got following error.

Error: The added or subtracted value results in an un-representable DateTime.
Parameter name: months

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: The added or subtracted value results in an un-representable DateTime.
Parameter name: months


My old code:
       
Response.Cookies.Add(new HttpCookie("appts", ""));
Response.Cookies["appts"].Expires.AddYears(-1);
  
 

Solution: I changed my old code and now its worked for me. :)
[Solved The added or subtracted value results in an un-representable DateTime.]

My new code:
       
HttpCookie cookie = new HttpCookie("appts");
cookie.Expires = DateTime.Now.AddYears(-1);
Response.Cookies.Add(cookie);