/// <summary> /// Opens a dialog window to add a guest /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void addGuestBtn_Click(object sender, EventArgs e) { List <string> availableRooms = GetAvailableRooms(); GuestDialog dialog = new GuestDialog(); dialog.AvailableRooms = availableRooms; if (dialog.ShowDialog(this) == DialogResult.Yes) { Person guest = dialog.NewGuest; dialog.Dispose(); // Update the room status in hotelDataSet hotelDataSet.Tables[roomDataTableString].Rows.Cast <DataRow>().ToList().ForEach(r => { if (r.Field <int>(0) == guest.RoomNumber) { r.SetField <string>(3, "Booked"); } }); // Insert new guest into hotelDataSet hotelDataSet.Tables[guestDataTableString].Rows.Add(guest.FirstName, guest.LastName, guest.RoomNumber, guest.NumberOfNights, guest.ReservationDate); // Update the DataSet roomsAdapter.Update(hotelDataSet, roomDataTableString); guestAdapter.Update(hotelDataSet, guestDataTableString); } }
/// <summary> /// Opens a dialog window to edit a guest's information /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void editGuestBtn_Click(object sender, EventArgs e) { if (guestTable.SelectedRows.Count == 0) { MessageBox.Show("Please select a row from the guest table"); return; } List <string> availableRooms = GetAvailableRooms(); DataRow row = hotelDataSet.Tables[guestDataTableString].Rows[guestTable.SelectedRows[0].Index]; Person currentGuest = new Person(row.Field <string>(0), row.Field <string>(1), row.Field <int>(2), row.Field <int>(3), row.Field <string>(4)); GuestDialog dialog = new GuestDialog(currentGuest); dialog.AvailableRooms = availableRooms; if (dialog.ShowDialog(this) == DialogResult.Yes) { Person editedGuest = dialog.NewGuest; dialog.Dispose(); if (currentGuest.Equals(editedGuest)) { return; } MySqlConnection conn = new MySqlConnection(connectionString); /***** CHECKS FOR EDITED GUEST *****/ // Check if the guest's first name was changed if (!currentGuest.FirstName.Equals(editedGuest.FirstName)) { hotelDataSet.Tables[guestDataTableString].Rows.Cast <DataRow>().ToList().ForEach(r => { if (r.Field <int>(2) == currentGuest.RoomNumber) { r.SetField <string>(0, editedGuest.FirstName); } }); } // Check if the guest's last name was changed if (!currentGuest.LastName.Equals(editedGuest.LastName)) { hotelDataSet.Tables[guestDataTableString].Rows.Cast <DataRow>().ToList().ForEach(r => { if (r.Field <int>(2) == currentGuest.RoomNumber) { r.SetField <string>(1, editedGuest.LastName); } }); } // Check if the guest's room number was changed if (currentGuest.RoomNumber != editedGuest.RoomNumber) { // Change the guest's room number hotelDataSet.Tables[guestDataTableString].Rows.Cast <DataRow>().ToList().ForEach(r => { if (r.Field <int>(2) == currentGuest.RoomNumber) { r.SetField <int>(2, editedGuest.RoomNumber); } }); // Update the room status in hotelDataSet // Make old room available hotelDataSet.Tables[roomDataTableString].Rows.Cast <DataRow>().ToList().ForEach(r => { if (r.Field <int>(0) == currentGuest.RoomNumber) { r.SetField <string>(3, "Available"); } }); // Make new room unavailable hotelDataSet.Tables[roomDataTableString].Rows.Cast <DataRow>().ToList().ForEach(r => { if (r.Field <int>(0) == editedGuest.RoomNumber) { r.SetField <string>(3, "Booked"); } }); } // Check if the guest's number of nights was changed if (currentGuest.NumberOfNights != editedGuest.NumberOfNights) { hotelDataSet.Tables[guestDataTableString].Rows.Cast <DataRow>().ToList().ForEach(r => { if (r.Field <int>(2) == currentGuest.RoomNumber) { r.SetField <int>(3, editedGuest.NumberOfNights); } }); } // Check if the guest's reservation date was changed if (!currentGuest.ReservationDate.Equals(editedGuest.ReservationDate)) { hotelDataSet.Tables[guestDataTableString].Rows.Cast <DataRow>().ToList().ForEach(r => { if (r.Field <int>(2) == currentGuest.RoomNumber) { r.SetField <string>(4, editedGuest.ReservationDate); } }); } roomsAdapter.Update(hotelDataSet, roomDataTableString); guestAdapter.Update(hotelDataSet, guestDataTableString); } }