public static bool cancelTicket(int ticketId) { if (ticketId == 0) { MessageBox.Show("Select a ticket first", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning); return(false); } var ticket = searchTicketById(ticketId); var reservation = new { coach = ticket.Coach, source = ticket.Source, destination = ticket.Destination, date = ticket.Date, time = ticket.Time, seats = ticket.Seat, //booked = ticket.Booked.Split(',').Length, //available = 40 - ticket.Booked.Split(',').Length }; var hasCoach = ReservationController.getSingleCoachReservation(reservation); char[] separator = { ',' }; string[] bookedSeats = hasCoach.Seats.Split(separator, StringSplitOptions.RemoveEmptyEntries); string[] cancelSeats = ticket.Seat.Split(separator, StringSplitOptions.RemoveEmptyEntries); List <string> newSeats = new List <string>(); foreach (var s in bookedSeats) { newSeats.Add(s.Trim()); } foreach (var bs in bookedSeats) { foreach (var cs in cancelSeats) { if (bs.Trim().Equals(cs.Trim())) { newSeats.Remove(cs.Trim()); } } } string seats = string.Join(", ", newSeats); var newReservation = new { coach = hasCoach.Coach.Trim(), source = hasCoach.Source.Trim(), destination = hasCoach.Destination.Trim(), date = hasCoach.Date.Trim(), time = hasCoach.Time.Trim(), seats = seats.Trim(), booked = newSeats.Count, available = 40 - newSeats.Count }; if (newReservation.booked == 0) { bool res = ReservationController.cancelReservation(reservation); } else { if (hasCoach == null) { bool reserve = ReservationController.addCoachReservation(newReservation); } else { bool reserve = ReservationController.updateCoachReservation(newReservation); } } return(db.Tickets.cancelTicket(ticketId)); }