Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am writing a desktop application in c# using VS2013. I am getting an absurd error that has not to be produced, in my opinion. I am setting a DateTimePicker's MinDate and MaxDate properties somewhere in the code in such way:

C#
DateTime minDate = DateTime.Parse(...);
DateTime maxDate = DateTime.Parse(...);

...

if (maxDate < dtpManuelFirst.MinDate)
{
    dtpManuelFirst.MinDate = minDate;
    dtpManuelFirst.MaxDate = maxDate;
}
else
{
    if (minDate > dtpManuelFirst.MaxDate)
    {
        dtpManuelFirst.MaxDate = maxDate;
        dtpManuelFirst.MinDate = minDate;
    }
    else
    {
        dtpManuelFirst.MinDate = minDate;
        dtpManuelFirst.MaxDate = maxDate;
    }
}

Very initially i know that minDate value is always less than maxDate value. This is absolute. When minDate is bigger than dtpManuelFirst.MaxDate like the second if condition, it updates the MaxDate property with no problem whereas i get the error "value is not valid for MinDate. MinDate must be less than MaxDate." on updating the MinDate property. It is ridiculous since i am already checking these conditions. In addition, the values are not supporting the error when i check in debug mode. Let say dtpManuelFirst.Mindate is 25.01.2014 and dtpManuelFirst.MaxDate is 27.01.2014. Let say minDate is 18.04.2014 and maxDate is 19.04.2014. then it will be going to the second if statement and enter in it. Updating firstly MaxDate property will not produce an error. Now we have dtpManuelFirst.MaxDate is 19.04.2014 and dtpManuelFirst.MinDate is 25.01.2014. Under these conditions it generates the error while assignign minDate value to MinDate property. Any help would be great!
Posted
Comments
George Jonsson 27-Jun-14 3:43am    
What are the initial values of dtpManuelFirst.MaxDate and dtpManuelFirst.MinDate?
Sprint89 27-Jun-14 6:05am    
What is the error message?
Member 8580595 27-Jun-14 8:08am    
Lets accept the initial values as .MinDate is 25.01.2014 and .MaxDate is 27.01.2014 and due to my algorithm assign .MinDate 18.04.2014 and .MaxDate 19.04.2014. Then it will enter the second if block and inside the block it will throw ArgumentOutOfRange exception when assigning the value to .MinDate

1 solution

Try changing the order:
C#
else
{
    dtpManuelFirst.MinDate = minDate;
    dtpManuelFirst.MaxDate = maxDate;
}

Becomes:
C#
else
{
    dtpManuelFirst.MaxDate = maxDate;
    dtpManuelFirst.MinDate = minDate;
}
In your version, the MaxDate is currently 27.01.2014 when you try to set the Mindate to 18.04.2014 - so the mindate is outside the range. Setting the max first should solve teh problem.
 
Share this answer
 
Comments
Member 8580595 27-Jun-14 3:44am    
Actually i am checking it in if (minDate > dtpManuelFirst.MaxDate) condition. When minDate is 18.04.2014 and dtpManuelFirst is 27.01.2014, the condiion is true and inside the if block, i am already assignign MaxDate first.

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