Overflow Exception when calling DiffDate

12-07-2004, 04:01 PM

I'm trying to fix a bug in a program I wrote for work that we use to make basic hotel reservations. I've got two date time pickers to select the arrival and departure dates, and a numeric up/down selector to select the number of nights. The problem is that when the user changes the departure date in the date/time picker, the number of nights doesn't update in the numeric up/down control.

What I've been trying to do it call DiffDate from the sub that's called then whe departure date is changed. This results in a stack overflow error. For the interim, I've put a "Update Nights" button next to the dtp which calls diffdate when pressed. I'd like this to be automatic....is there a way around this?

Here's some of the code...

Called when user changes the departure date. I would like functionality of the second code segment to be included in this segment.

Private Sub dtpDeparture_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtpDeparture.ValueChanged

If dtpDeparture.Value.Date.Date < dtpArrival.Value.Date.Date Then
dtpDeparture.Value = dtpArrival.Value.AddDays(numNights.Value)
txterror1.Enabled = True
txterror1.Text = "Error - departure date cannot be before arrival date."
errcheck = True
errcheck = False
txterror1.Enabled = False
End If

End Sub

Private Sub btnNights_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNights.Click
numnights.value = DateDiff(DateInterval.Day, dtpArrival.Value.Date, dtpDeparture.Value.Date)
End Sub

12-07-2004, 05:59 PM
Set a breakpoint in there and you'll see what's happening... it's being called over and over and over......

Also, DateDiff is legacy VB.... Look at TimeSpan.

12-07-2004, 09:00 PM
Set a breakpoint in there and you'll see what's happening... it's being called over and over and over......

Also, DateDiff is legacy VB.... Look at TimeSpan.

Which sub is being called over and over? The one that handles the button click or the date change? Sorry.....I'm not very experienced with this.

12-08-2004, 08:51 AM
To set a breakpoint, click on the left margin of the code window (the part on the left edge between the code and the end of the window). You should see a big red circle appear. That is a breakpoint.
Set this breakpoint on a line in your dtpDeparture_ValueChanged subroutine. When the line that has the breakpoint on it is about to run, your program will be thrown into break mode, so that you can use F11 to step and see how your code is executing line by line.

For a Timespan, all you do is subtract two date values:
Dim TS As Timespan = Date2 - Date1
The number of days difference can be found in the timespan at
TS.Days :)

12-08-2004, 09:12 AM
I'm working on it but there's an oddity with the ValueChanged event that I'm trying to work out.

There's no doubt the ValueChanged event will be called twice since you alter the value of the control from within the event. But when I use this handler:
Private Sub dtpDepart_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dtpDepart.ValueChanged


'Indicate the controls are busy
_controlsUpdating = True

'Use Date to avoid time issues
If Not DatesAreValid(dtpArrive.Value.Date, dtpDepart.Value.Date) Then

'Set it to the closest possible date
dtpDepart.Value = dtpArrive.Value.AddDays(udNumNights.Value)

'Report the error
MessageBox.Show("departure date must be the proper number " & _
"of nights after arrival date.")
End If

The event is fired four times, not the twice I expect. Here's the order I see happening in the debugger when I set the departure date too early:

User changes value. Handler alters value and fires the event again. The second call to the handler terminates since the dates are now valid The first call to the handler reports the error and exits. The event is fired AGAIN and the control's value is reset to the original value. The dates are invalid so the control is altered and the event is fired again. The second handler terminates. The handler reports the error and terminates.

I'm unclear on why the handler is called twice, and VERY unclear on why the value is reset for the second call. This only seems to happen when you alter the value of the control from within the handler.

HOWEVER, I did find a workaround. The CloseUp event of the DateTimePicker is called when the drop-down calendar portion is dismissed, and it does not seem to have the same problem. I've attached an example form that does what I think you're asking for.

12-08-2004, 09:28 AM
Thanks everyone for helping! I'm going to try the workaround tonight when I get in to work. Hopefully this will fix it.

Thanks again!

12-08-2004, 06:30 PM
I got it working.....I didn't end up using the whole workaround that was posted....I just used TimeSpan instead of DiffDate, and had the event trigger only when the date/time picker was closed, not just when the value was changed.

Thanks for all your help.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum