Need help on this logic... (.NET)

141 Views Asked by At

This is the code I have in my form to check if the date the user selected is more than 14 days in advance, or in the past.

If (dtpDate.Value > DateTime.Today.AddDays(14)) Then
    frmBookErr.SetError(dtpDate, "You cannot book more than two weeks in advance.")
Else
    frmBookErr.SetError(dtpDate, "")
End If
If (dtpDate.Value < DateTime.Today) Then
    frmBookErr.SetError(dtpDate, "You cannot book a room for the past.")
Else
    frmBookErr.SetError(dtpDate, "")
End If

It works, but if I select a date more than 14 days ahead it won't show the error message, because of the second IF checking if it is in the past and blanking it.

I really can't think of another way around this other than making another textbox to sit behind the one the user types into, and displaying the second error message to that one.

Anybody have any bright ideas? Thanks :)

2

There are 2 best solutions below

0
On

try this

If (dtpDate.Value > DateTime.Today.AddDays(14)) Then
    frmBookErr.SetError(dtpDate, "You cannot book more than two weeks in advance.")
Else If (dtpDate.Value < DateTime.Today) Then
    frmBookErr.SetError(dtpDate, "You cannot book a room for the past.")
Else
    frmBookErr.SetError(dtpDate, "")
End If
0
On

You are very close! Just place the check in a else if block.

If (dtpDate.Value > DateTime.Today.AddDays(14)) Then
    frmBookErr.SetError(dtpDate, "You cannot book more than two weeks in advance.")
Else If (dtpDate.Value < DateTime.Today) Then
    frmBookErr.SetError(dtpDate, "You cannot book a room for the past.")
Else
    frmBookErr.SetError(dtpDate, "")
End If