Click here to Skip to main content
15,912,082 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have encountered an error like the title says and can't really understand what this error means. Could someone explain it?

Exception: "The added or subtracted value results in an un-representable DateTime.\r\nParameter name: months"

Thanks.

What I have tried:

My dates takes only the date, i.e dateTimePicker.Value.Date, so I thought this is the problem that I did not added the .Date everywhere I use the dates, but it didn't helped.

C#
DateTime twoYearsAgo = policy.startDateOfThePolicy.AddYears(-2);
DateTime fiveYearsAgo = policy.startDateOfThePolicy.AddYears(-5);

foreach (var driver in policy.Drivers)
 {
 if (driver.Claims.Any(claim => claim.ClaimDate.Date >=             policy.startDateOfThePolicy.AddYears(-1).Date))
 {
     policy.Premium = policy.Premium + (policy.Premium + 10);
 }
 else if(driver.Claims.Any(claim => claim.ClaimDate.Date <= twoYearsAgo.Date && claim.ClaimDate.Date >= fiveYearsAgo.Date))
 {
     policy.Premium = policy.Premium + (policy.Premium + 10);
 }


Policy is a class that holds the premium variable as well as a list of Drivers, policy.Drivers

Driver Class:
C#
<public string Name { get; set; }
        public string Surname { get; set; }
        public int Age { get; set; }
        public string Occupation { get; set; }
        public List<Claim> Claims { get; set; } = new List<Claim>();


Claim class:
C#
public class Claim
    {
        public DateTime ClaimDate { get; set; }
    }


Policy Class:

C#
public class Policy
   {
       private decimal premium = 100;

       public List<Driver> Drivers { get; set; } = new List<Driver>();

       public DateTime startDateOfThePolicy { get; set; }

       public decimal Premium
       {
           get { return premium; }
           set { premium = value; }
       }
   }
Posted
Updated 26-Mar-17 4:10am
v3
Comments
CHill60 26-Mar-17 9:47am    
We can't help you if you don't post your code
Thank you for the code. When you use the debugger and put a break point on the line that is giving the exception, what is the value in the startDateOfThePolicy at that time?
If you are not sure about debugging then read this: Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Member 13081540 26-Mar-17 10:11am    
the date of the startDateOfThePolicy is 01/01/0001 00:00:00

1 solution

The error message means that when you add or subtract a value from your date, the result is outside the limits for a .NET DateTime value: From 00:00:00.0000000 UTC, January 1, 0001 to 23:59:59.9999999 UTC, December 31, 9999 (both in the Gregorian calendar).
At a guess, the value you are starting with is zero - 00:00:00.0000000 UTC, January 1, 0001 - and you subtracted any non-zero value from that.

Use the debugger, and look at exactly what is in policy.startDateOfThePolicy when your code is running. If it's what I suspect, then you need to find out why - and that is not in the code you show since you do not at any point in that code change the value!
 
Share this answer
 
Comments
Member 13081540 26-Mar-17 10:32am    
policy.startDateOfThePolicy value looks like that: 01/01/0001 00:00:00.

I think that I am changing the value, in my main form like that:

Policy dateOfThePolicy = new Policy();
dateOfThePolicy.startDateOfThePolicy = dtpPolicy.Value;
OriginalGriff 26-Mar-17 10:57am    
Well...clearly the value in your DTP is not set!
Is that the right DTP? Did you give it a value from somewhere, a database or suchlike?
Member 13081540 26-Mar-17 11:00am    
Yes thats the right dateTimePicker and no, I didn't give a vlue from a database. Do I understand correctly that I need to set the value of a DTP before I click a button to add a new value to it?
OriginalGriff 26-Mar-17 11:19am    
No - when you create a DTP it is initialized to the current date and time, even if you don't display it. And a DTP minimum value is 1753 (The start of the Gregorian calendar).
So presumably, your code setting it to the DTP value isn't being used. What does the debugger say if you put a breakpoint on that line?
Member 13081540 26-Mar-17 11:21am    
Ah, found the problem. I needed to do it like that,
policy.StartDateOfThePolicy = dtpPolicy.Value;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900