Instead of
dateTimePicker2_ValueChanged
Use
dateTimePicker2_CloseUp
event , 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...