// Attempt to assign the selected seat to the customer private void btnBookSeat(object sender, EventArgs e) { lblMessages.Text = ""; // resets all messages before start string customer = txtName.Text; bool hasErrors = false; // cannot add customer on a full flight if (AirplaneService.IsFull()) { // attempts to add to wait list. btnAddToWaitlist(sender, e); lblMessages.Text += "Flight is full.\r\n"; return; } // book customer... try { // null or empty name if (String.IsNullOrWhiteSpace(customer)) { txtName.Focus(); throw new NullReferenceException("Name must not be empty or white spaced\r\n"); } } catch (NullReferenceException ex) { // no name was typed lblMessages.Text += ex.Message; hasErrors = true; } // checks seat selection try { if (String.IsNullOrWhiteSpace(lstBoxRow.SelectedItem.ToString())) { lstBoxRow.Focus(); throw new NullReferenceException(); } } catch (NullReferenceException) { // invalid seat selection lblMessages.Text += "Select a row" + "\r\n"; hasErrors = true; } try { if (String.IsNullOrWhiteSpace(lstBoxSeat.SelectedItem.ToString())) { lstBoxSeat.Focus(); throw new NullReferenceException(); } } catch (NullReferenceException) { // invalid seat selection lblMessages.Text += "Select a seat" + "\r\n"; hasErrors = true; } // if there are errors, stop here! if (hasErrors) { return; } string seat = lstBoxRow.SelectedItem.ToString(); seat += lstBoxSeat.SelectedItem.ToString(); if (!AirplaneService.AssignCustomerToSeat(seat, customer)) { lblMessages.Text += "Unable to assign customer to the seat\r\n"; } btnShowAllSeats(sender, e); // update customer list ResetSelection(); // clear info after successfully insertion }