private void buttonDelete_Click(object sender, EventArgs e) { DialogResult dialogResult = MessageBox.Show("Are you sure you want to delete " + section.SectionName + "? Student list, classes and attendance data of this section will be permanently deleted", "Confirmation", MessageBoxButtons.YesNo); if (dialogResult == DialogResult.Yes) { SectionController scontroller = new SectionController(); SectionTimeController stcontroller = new SectionTimeController(); SectionStudentController sscontroller = new SectionStudentController(); try { ClassController ccontroller = new ClassController(); List <ClassModel> classList = ccontroller.GetBySectionId(section.Id); AttendanceController acontroller = new AttendanceController(); foreach (ClassModel Class in classList) { acontroller.DeleteAllByClass(Class.Id); } ccontroller.DeleteAllBySection(section.Id); sscontroller.RemoveAllBySection(section.Id); stcontroller.RemoveAllBySection(section.Id); scontroller.Delete(section); MessageBox.Show("Deleted!"); buttonBack.PerformClick(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } }