Click here to Skip to main content
15,879,239 members
Please Sign up or sign in to vote.
5.00/5 (3 votes)
See more:
C#
private void dateTimePickerHolidayToDate_ValueChanged(object sender, EventArgs e)
        {
DateTime fromdate = Convert.ToDateTime(dateTimePickerFromDate.Text);
DateTime todate1 = Convert.ToDateTime(dateTimePickerToDate.Text);               
                if (fromdate1 <= todate1)
                {
                    TimeSpan daycount = todate1.Subtract(fromdate1);
                    int dacount1 = Convert.ToInt32(daycount.Days) + 1;
                    txtDay.Text = Convert.ToString(dacount1);                    
                }
                else
                {                    
                   MessageBox.Show("From Date Must be Less Than To Date");
                }
        }



HI frnds Good Afternoon to every one,In windows application i am using two datetimepicker for "from date" and "to date" ...After choosing "to date" i need to count number of days in between "from date" and "to date" days count ,for that i used above code...but when move my "to datetimepicker" less than "from datetimepicker" it executing infinite times of showing message box.How to show only one time that message box...
Posted
Updated 9-Jul-12 23:25pm
v2
Comments
Member 9377809 4-Sep-13 3:30am    
How to read the the dates one by one from DateTimePicker i.e StartDate 2012-01-01 and EndDate 2012-01-07 . I want to read one by one between two dates.
Please help me in resolving this issue

Thanks in Advance
TJM
itsureshuk 4-Sep-13 5:30am    
use order by datecolumn-- to get records in datewise

Instead of dateTimePicker2_ValueChanged
Use dateTimePicker2_CloseUpevent , so your final code will be

private void dateTimePicker2_CloseUp(object sender, EventArgs e)
{
    DateTime fromdate = Convert.ToDateTime(dateTimePicker1.Text);
    DateTime todate1 = Convert.ToDateTime(dateTimePicker2.Text);
    if (fromdate <= todate1)
    {
        TimeSpan daycount = todate1.Subtract(fromdate);
        int dacount1 = Convert.ToInt32(daycount.Days) + 1;
        MessageBox.Show(Convert.ToString(dacount1));
    }
    else
    {
        MessageBox.Show("From Date Must be Less Than To Date");
    }
}


CloseUp event is triggered only when the user finally selects a value. ValueChanged event will fire when you change the month also that was your issue.

I have corrected your variable names in code :) to run in my local box

mark it as solution if this resolved your issue

Thanks...
 
Share this answer
 
Comments
itsureshuk 10-Jul-12 7:40am    
Thanks...its working
Shemeer NS 10-Jul-12 7:43am    
You're welcome
Member 13153537 27-Apr-17 1:58am    
why are you added 1.
please explain me.
I think that in ClosUp event from "fromDate" you could configure something like:

dateTimePicker2.MinDate = dateTimePicker1.Value


This way, user can only choose a data equal or after the first date.
 
Share this answer
 
C#
DateTime selectedDate = Convert.ToDateTime(dtpExpireDate.Value);
DateTime todayDate = Convert.ToDateTime(DateTime.Now);
if (selectedDate < todayDate)
{
    MessageBox.Show("Selected date Must be greater then Today's date");
}
 
Share this answer
 
v2

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