0

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:

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. 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. Any help would be great!

seven-phases-max
  • 11,765
  • 1
  • 45
  • 57
ceskin
  • 13
  • 5

3 Answers3

0

The end result of all your decisions is that you are always setting

dtpManuelFirst.MinDate = minDate;
dtpManuelFirst.MaxDate = maxDate;

Why not simply do that immediately? You don't need all those if-else branches when you do the same thing anyway.

If you are still getting problems, set the dtpManuelFirst.MaxDate first to something very large first.

dtpManuelFirst.MaxDate = DateTime.MaxValue;
dtpManuelFirst.MinDate = minDate;
dtpManuelFirst.MaxDate = maxDate;
David S.
  • 5,965
  • 2
  • 40
  • 77
  • it gives error 19.04.2014 is not valid for MinDate. MinDate must be less than MaxDate. However i assign 18.04.2014 for MinDate and 19.04.2014 for MaxDate. It is absolutely ridiculous error :( – ceskin Jun 27 '14 at 08:34
  • @ceskin But you assign `MinDate` before `MaxDate`. That seems to be the reason. `MaxDate` is not yet set to 19.04.2014 when you set `MinDate` and may be something else. – David S. Jun 27 '14 at 10:55
0

I know this thread is quite old, but maybe this might help the next person... Perhaps they should consider adding the 'complete' DateTime range: Including Hours, Minutes and Seconds.

DateTime firstDayInMonth = new DateTime(today.Year, today.Month, 1, 0, 0, 0);

DateTime lastDayInMonth = new DateTime(today.Year, today.Month, DateTime.DaysInMonth(today.Year, today.Month), 23, 59, 59);
Channard
  • 1
  • 1
  • 1
0

set the MinDate to DateTimePicker.MinDateTime and Max Date to DateTimePicker.MaximumDateTime, before setting the actual values. Like below:

dtpManuelFirst.MinDate = DateTimePicker.MinDateTime;
dtpManuelFirst.MaxDate = DateTimePicker.MaximumDateTime;

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;
  }
}