Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace RoomBookingSystem
- {
- public partial class RoomBookingForm : Form
- {
- public RoomBookingForm()
- {
- InitializeComponent();
- }
- private void RoomBookingForm_Load(object sender, EventArgs e)
- {
- }
- private void RoomBookingForm_FormClosing(object sender, FormClosingEventArgs e)
- {
- Application.Exit();
- }
- private void BookRoomButton_Click(object sender, EventArgs e)
- {
- string Date = DateToBeBooked.Text;
- string Period = PeriodBooked.Text;
- string Email = EmailBookingRoom.Text;
- string Room = RoomBeingBooked.Text;
- string ExtraEquipment = ExtraEquipmentTextBox.Text;
- List<string> DatabaseDate = new List<string>();
- List<string> DatabasePeriod = new List<string>();
- //Selects all date records from the bookings table so that they can be compared to eliminate double bookings.
- using (SqlConnection CompareDate = new SqlConnection("RoomBookingSystem"))
- {
- string SelectDate = "SELECT DateBooked FROM Bookings";
- using (SqlCommand GetDate = new SqlCommand(SelectDate, CompareDate))
- {
- using (SqlDataReader DateReader = GetDate.ExecuteReader())
- {
- while (DateReader.Read())
- {
- CompareDate.Open();
- DatabaseDate.Add(DateReader.GetString(0));
- }
- }
- }
- }
- //Takes the date records from the bookings table in the database and puts them in a list
- if (DatabaseDate.Contains(Date))
- {
- using (SqlConnection ComparePeriod = new SqlConnection("RoomBookingSystem"))
- {
- string SelectPeriod = "SELECT PeriodBookedFor FROM Bookings";
- using (SqlCommand GetPeriod = new SqlCommand(SelectPeriod, ComparePeriod))
- {
- using (SqlDataReader DateReader = GetPeriod.ExecuteReader())
- {
- while (DateReader.Read())
- {
- ComparePeriod.Open();
- DatabasePeriod.Add(DateReader.GetString(0));
- }
- }
- }
- }
- //If the list contains the period that the user is booking for on the same date, emails the user telling them the booking was a failure
- if (DatabasePeriod.Contains(Period))
- {
- MailMessage emailFailure = new MailMessage();
- SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
- emailFailure.From = new MailAddress("RoomBookingSystem@gmail.com");
- emailFailure.To.Add(Email);
- emailFailure.Subject = "Booking Failure";
- emailFailure.Body = "Your recent booking failed due to the room already being booked on the same date and the same time.";
- SmtpServer.Send(emailFailure);
- }
- else
- {
- //Because the period booking for was different but on the same date, the booking can go through and so the details are added
- using (SqlConnection connection = new SqlConnection("RoomBookingConnection"))
- {
- string SaveBooking = "INSERT INTO User(DateBooked, PeriodBooked) " + "Values('" + Date + "' , '" + Period + "')";
- using (SqlCommand command = new SqlCommand(SaveBooking, connection))
- {
- connection.Open();
- command.ExecuteNonQuery();
- }
- }
- //Emails the user telling them that the booking was successful
- MailMessage emailSuccess = new MailMessage();
- SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
- emailSuccess.From = new MailAddress("RoomBookingSystem@gmail.com");
- emailSuccess.To.Add(Email);
- emailSuccess.Subject = "Booking Success";
- emailSuccess.Body = "Your recent booking was successful and has been added to the database.";
- SmtpServer.Send(emailSuccess);
- //Emails the side team for extra equipment
- MailMessage emailSideTeam = new MailMessage();
- SmtpClient SmtpServer2 = new SmtpClient("smtp.gmail.com");
- emailSideTeam.From = new MailAddress("RoomBookingSystem@gmail.com");
- emailSideTeam.To.Add("sideteam@gmail.com");
- emailSideTeam.Subject = "Extra Equipment";
- emailSideTeam.Body = "The user has entered this as what they would like for extra equipment " + ExtraEquipment + " in the room " + Room + " by period " + Period + ", thank you.";
- SmtpServer2.Send(emailSideTeam);
- }
- }
- else
- {
- //Adds booking details in to the database
- using (SqlConnection connection = new SqlConnection("RoomBookingConnection"))
- {
- string SaveBooking = "INSERT INTO User(DateBooked, PeriodBooked) " + "Values('" + Date + "' , '" + Period + "')";
- using (SqlCommand command = new SqlCommand(SaveBooking, connection))
- {
- connection.Open();
- command.ExecuteNonQuery();
- }
- }
- //Emails the user telling them that the booking was successful
- MailMessage emailSuccess = new MailMessage();
- SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
- emailSuccess.From = new MailAddress("RoomBookingSystem@gmail.com");
- emailSuccess.To.Add(Email);
- emailSuccess.Subject = "Booking Success";
- emailSuccess.Body = "Your recent booking was successful and has been added to the database.";
- SmtpServer.Send(emailSuccess);
- //Emails the side team for extra equipment
- MailMessage emailSideTeam = new MailMessage();
- SmtpClient SmtpServer2 = new SmtpClient("smtp.gmail.com");
- emailSideTeam.From = new MailAddress("RoomBookingSystem@gmail.com");
- emailSideTeam.To.Add("sideteam@gmail.com");
- emailSideTeam.Subject = "Extra Equipment";
- emailSideTeam.Body = "The user has entered this as what they would like for extra equipment " + ExtraEquipment + " in the room " + Room + " by period " + Period + ", thank you.";
- SmtpServer2.Send(emailSideTeam);
- }
- }
- //Adds each line of the text file "Rooms" into the drop down box
- private void RoomBeingBooked_SelectedIndexChanged(object sender, EventArgs e)
- {
- string[] Rooms = File.ReadAllLines("Rooms.txt");
- foreach (var line in Rooms)
- {
- RoomBeingBooked.Items.Add(line);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement