Start by fixing the
SQL Injection[
^] vulnerability.
Then, to find any bookings that overlap the new booking, check whether they start before the new booking ends, and end after the new booking starts.
You only need to select the first matching record, and you don't need all of the fields.
Using con As New SqlConnection("...")
Using cmd As New SqlCommand("SELECT TOP 1 BookingId FROM BookingRecordTransaction WHERE FacilityCode = @FacilityCode AND BookedFrom <= @EndDate And BookedTo >= @StartDate", con)
cmd.Parameters.AddWithValue("@FacilityCode", ComboBox1.Text)
cmd.Parameters.AddWithValue("@StartDate", DatetimePicker1.Text)
cmd.Parameters.AddWithValue("@EndDate", DatetimePicker2.Text)
con.Open()
Using dr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.SingleResult Or CommandBehavior.SingleRow Or CommandBehavior.CloseConnection)
If dr.HasRows Then
End If
End Using
End Using
End Using
Now, do yourself a favour and give your controls meaningful names. You might remember what
ComboBox1
means
now, but when you come back to your code in a couple of months, you'll have forgotten. If you give it a name like
FacilityCodeList
, it will be much more obvious. :)