Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
15 views4 pages

Bookings

The document is a code snippet for a booking system in Visual Basic that allows users to select dates and book rooms while checking for conflicts in bookings. It includes functionality to display a calendar, manage bookings, calculate total bills, and reset input fields. The code also connects to a SQL database to store and retrieve booking information.

Uploaded by

rb0374
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views4 pages

Bookings

The document is a code snippet for a booking system in Visual Basic that allows users to select dates and book rooms while checking for conflicts in bookings. It includes functionality to display a calendar, manage bookings, calculate total bills, and reset input fields. The code also connects to a SQL database to store and retrieve booking information.

Uploaded by

rb0374
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

BOOKINGS FORM

Imports System.Data.SqlClient
Public Class bookings
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

Dim startDate = New DateTime(MonthCalendar1.SelectionStart.Year,


MonthCalendar1.SelectionStart.Month, 1)
Dim EndDate = startDate.AddMonths(1).AddDays(-1)

MessageBox.Show(startDate)
MessageBox.Show(EndDate)

End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click


Dim ConString As String = "Server=RB-REALME\SQLEXPRESS03; Database=starlight;
Trusted_Connection=True; TrustServerCertificate=True;"
Using Con As New SqlConnection(ConString)
Con.Open()

' Convert the DateTime values from the DateTimePickers


Dim startDate As DateTime = dtp1.Value
Dim endDate As DateTime = dtp2.Value
Dim roomNumber As Integer = Convert.ToInt32(TextBox1.Text)

' 1. Check for overlapping bookings


Dim checkQuery As String = "SELECT COUNT(*) FROM Booking
WHERE room_Number = @RoomNumber
AND (@StartDate <= End_Book_Date AND @EndDate >=
Start_Book_Date)"

Using checkCmd As New SqlCommand(checkQuery, Con)


checkCmd.Parameters.AddWithValue("@RoomNumber", roomNumber)
checkCmd.Parameters.AddWithValue("@StartDate", startDate)
checkCmd.Parameters.AddWithValue("@EndDate", endDate)

Dim overlapCount As Integer = Convert.ToInt32(checkCmd.ExecuteScalar())

If overlapCount > 0 Then


MessageBox.Show("Room is already booked for the selected date range. Please
choose another room or adjust your dates.", "Booking Conflict", MessageBoxButtons.OK,
MessageBoxIcon.Warning)
Exit Sub
End If
End Using

Dim stayDuration As Integer = (endDate - startDate).Days + 1


Dim ratePerDay As Decimal = 2000

1
Dim totalBill As Decimal = stayDuration * ratePerDay

' 3. Insert booking


Dim insertQuery As String = "INSERT INTO Booking (Name, Contact, room_Number,
Start_Book_Date, End_Book_Date, Total_Bill)
VALUES(@Name, @Contact, @RoomNumber, @StartDate, @EndDate,
@TotalBill)"

Using cmd As New SqlCommand(insertQuery, Con)


cmd.Parameters.AddWithValue("@Name", TextBox2.Text)
cmd.Parameters.AddWithValue("@Contact", TextBox3.Text)
cmd.Parameters.AddWithValue("@RoomNumber", roomNumber)
cmd.Parameters.AddWithValue("@StartDate", startDate)
cmd.Parameters.AddWithValue("@EndDate", endDate)
cmd.Parameters.AddWithValue("@TotalBill", totalBill)
cmd.ExecuteNonQuery()
End Using

MessageBox.Show("Booking Successful! Bill Saved in Database.")

' Show Bill Form


Dim billForm As New BillForm()
billForm.CustomerName = TextBox2.Text
billForm.RoomNumber = roomNumber.ToString()
billForm.StayDuration = stayDuration
billForm.TotalBill = totalBill
billForm.ShowDialog()
End Using
End Sub

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click


Add_Columns()
End Sub

Private Sub Add_Columns()


DGV.Columns.Add("RoomNum", "Room Number")
Dim DaysInMonth As Integer =
DateTime.DaysInMonth(MonthCalendar1.SelectionStart.Year,
MonthCalendar1.SelectionStart.Month)

For i = 1 To DaysInMonth
DGV.Columns.Add(i, i)
Next
End Sub
Private Sub Add_Rows()

2
For i = 1 To 20
DGV.Rows.Add(i.ToString)
Next
End Sub

Private Sub MonthCalendar1_DateChanged(sender As Object, e As DateRangeEventArgs)


Handles MonthCalendar1.DateChanged
DGV.Columns.Clear()
DGV.Rows.Clear()

Add_Columns()
Add_Rows()

End Sub

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click


Dim ConString As String = "Server=RB-REALME\SQLEXPRESS03; Database=starlight;
Trusted_Connection=True; TrustServerCertificate=True;"
Dim Con As New SqlConnection(ConString)
Con.Open()

Dim query As String = " Select * From Booking Where Start_Book_Date >= @ST AND
End_Book_Date <= @ET"

Dim startDate = New DateTime(MonthCalendar1.SelectionStart.Year,


MonthCalendar1.SelectionStart.Month, 1)
Dim EndDate = startDate.AddMonths(1)

Dim cmd As New SqlCommand(query, Con)

cmd.Parameters.AddWithValue("@ST", startDate)
cmd.Parameters.AddWithValue("@ET", EndDate)

Dim dr As SqlDataReader = cmd.ExecuteReader()

While dr.Read()
Dim RoomNum_R As Integer = dr.GetInt32(3) ' Room Number
Dim Start_Date As DateTime = dr.GetDateTime(4)
Dim End_Date As DateTime = dr.GetDateTime(5)

Dim Start_Date_C As Integer = Start_Date.Day


Dim End_Date_C As Integer = End_Date.Day

Dim TotalDays As Integer = End_Date_C - Start_Date_C

' Ensure RoomNum_R - 1 is within the row range


If RoomNum_R - 1 >= 0 AndAlso RoomNum_R - 1 < DGV.RowCount Then

3
' Ensure Start_Date_C is within the column range
If Start_Date_C >= 0 AndAlso Start_Date_C < DGV.ColumnCount Then
DGV(Start_Date_C, RoomNum_R - 1).Style.BackColor = Color.Red
End If

' Loop to fill all days in the range


For i = 1 To TotalDays
Dim columnIndex As Integer = Start_Date_C + i
If columnIndex >= 0 AndAlso columnIndex < DGV.ColumnCount Then
DGV(columnIndex, RoomNum_R - 1).Style.BackColor = Color.Red
End If
Next
End If

End While
End Sub

Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click


TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
dtp1.Value = DateTime.Now
dtp2.Value = DateTime.Now

For Each ctrl As Control In Me.Controls


If TypeOf ctrl Is TextBox Then
DirectCast(ctrl, TextBox).Clear() ' Clear all textboxes
ElseIf TypeOf ctrl Is ComboBox Then
DirectCast(ctrl, ComboBox).SelectedIndex = -1 ' Reset combo boxes
ElseIf TypeOf ctrl Is CheckBox Then
DirectCast(ctrl, CheckBox).Checked = False ' Uncheck checkboxes
ElseIf TypeOf ctrl Is RadioButton Then
DirectCast(ctrl, RadioButton).Checked = False ' Uncheck radio buttons
ElseIf TypeOf ctrl Is DateTimePicker Then
DirectCast(ctrl, DateTimePicker).Value = DateTime.Today ' Reset date pickers
ElseIf TypeOf ctrl Is MonthCalendar Then
DirectCast(ctrl, MonthCalendar).SetDate(DateTime.Today) ' Reset calendar
End If
Next

MessageBox.Show("Reset successfully!")
End Sub

Private Sub Label6_Click(sender As Object, e As EventArgs) Handles Label6.Click

End Sub
End Class

You might also like