Date comparison in C#.NET

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • doggy1982
    New Member
    • Mar 2008
    • 4

    Date comparison in C#.NET

    Hi All,

    I am facing a critical issue when I compare my dates and display error is the start date is greater than the end date. Here is my code... I am making use of Datetime picker.

    private void datetimepicker1 _ValueChanged(o bject sender, EventArgs e)
    {

    Datetime dt1 = datetimepicker1 .value;
    Datetime dt2 = datetimepicker2 .value;

    if(dt1 > dt2)
    {
    Messagebox.show ("Start date cannot be greater than end date");
    datetimepicker1 .value = datetimepicker2 .value;
    return;
    }
    }


    This code executes well if I am selecting dates in the same calendar month. If I select the start date to be somewhere in April and the end date is march, the control goes into loop and displays the above message box infinite times.....

    Please help....

    Thanks,
    Last edited by doggy1982; Mar 18 '08, 12:53 PM. Reason: Added details
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    I think it has something to do with that that function gets called for EVERY change(postback ) to the datetimepicker object's value.
    And you are also setting the value inside it?

    Comment

    • doggy1982
      New Member
      • Mar 2008
      • 4

      #3
      Originally posted by Plater
      I think it has something to do with that that function gets called for EVERY change(postback ) to the datetimepicker object's value.
      And you are also setting the value inside it?
      thanks for replying plater.... its a windows form so no issue with postback. I am setting the value inside just to make the start date equals end date.

      This issue still persists.... no clues.. :-(

      Comment

      • balabaster
        Recognized Expert Contributor
        • Mar 2007
        • 798

        #4
        Originally posted by doggy1982
        thanks for replying plater.... its a windows form so no issue with postback. I am setting the value inside just to make the start date equals end date.

        This issue still persists.... no clues.. :-(
        In a windows form, you're correct that it's not a postback issue - however, you're still setting the value of the startdatepicker = enddatepicker.. .which triggers a change...which triggers the startdatapicker = enddatepicker.. .which triggers a change ad infinitum.

        Try changing the line:
        DateTimePicker1 .Text = DateTimePicker2 .Text;
        To
        DateTimePicker2 .Text = DateTimePicker1 .Text;

        This way, you're not triggering the event in infinite loops. Although, it may not be what you want.

        An alternative to this would be to find some method of determining if it's a user update or a system update. I usually use some method similar to the following mechanism:
        Code:
        bool _SystemUpdate = false;  //class level variable
         
        private void datetimepicker1_valueChanged(object Sender, EventArgs e){
          DateTime dt1 = datetimepicker1.value;
          DateTime dt2 = datetimepicker2.value;
         
          if((dt1 > dt2) && (!_SystemUpdate)){
        	MessageBox.show("Start date...");
        	_SystemUpdate = true;
        	datetimepicker1.value = datetimepicker2.value;
        	_SystemUpdate = false;
        	return;
          }
        }
        This will allow the code to determine if the update was triggered by the user or by the system and if it was a system update it won't error, but a user update won't set the _SystemUpdate variable to true and consequently the code block will be triggered.

        Comment

        • doggy1982
          New Member
          • Mar 2008
          • 4

          #5
          Originally posted by balabaster
          In a windows form, you're correct that it's not a postback issue - however, you're still setting the value of the startdatepicker = enddatepicker.. .which triggers a change...which triggers the startdatapicker = enddatepicker.. .which triggers a change ad infinitum.

          Try changing the line:
          DateTimePicker1 .Text = DateTimePicker2 .Text;
          To
          DateTimePicker2 .Text = DateTimePicker1 .Text;

          This way, you're not triggering the event in infinite loops. Although, it may not be what you want.

          An alternative to this would be to find some method of determining if it's a user update or a system update. I usually use some method similar to the following mechanism:
          Code:
          bool _SystemUpdate = false;  //class level variable
           
          private void datetimepicker1_valueChanged(object Sender, EventArgs e){
            DateTime dt1 = datetimepicker1.value;
            DateTime dt2 = datetimepicker2.value;
           
            if((dt1 > dt2) && (!_SystemUpdate)){
          	MessageBox.show("Start date...");
          	_SystemUpdate = true;
          	datetimepicker1.value = datetimepicker2.value;
          	_SystemUpdate = false;
          	return;
            }
          }
          This will allow the code to determine if the update was triggered by the user or by the system and if it was a system update it won't error, but a user update won't set the _SystemUpdate variable to true and consequently the code block will be triggered.
          Thanks balabaster.... i tried your piece of code but the result is the same...it goes infinite.... my GOSH!!.... I am really crazy now coz I tried working many alternative approaches but all are resulting in the same when I change the calendar month to some other month... infinite..infin ite...
          Is there any other clean way of handling this validation?.... . Any code sample please...greatf ul to u....

          Comment

          • doggy1982
            New Member
            • Mar 2008
            • 4

            #6
            Originally posted by doggy1982
            Thanks balabaster.... i tried your piece of code but the result is the same...it goes infinite.... my GOSH!!.... I am really crazy now coz I tried working many alternative approaches but all are resulting in the same when I change the calendar month to some other month... infinite..infin ite...
            Is there any other clean way of handling this validation?.... . Any code sample please...greatf ul to u....

            wowoow...I finally did it... I made use of the closeup event of my datetimepicker and now I can see that the control is not going to infinite loop....
            Its a great learning anyways... :-)

            Comment

            • int08h
              New Member
              • Apr 2007
              • 28

              #7
              try to trim time (hour, minute, second...), just compare the date may make it work correctly

              Comment

              Working...