private void BtnAddStudent_Click(object sender, EventArgs e) { Student stu = new Student() { FirstName = txtFirstName.Text, LastName = txtLastName.Text, ProgramOfChoice = txtProgram.Text, DateOfBirth = dtpDateOfBirth.Value }; try { if (existingStudent != null) { stu.StudentId = existingStudent.StudentId; StudentDB.Update(stu); MessageBox.Show("Student updated!"); } else { StudentDB.Add(stu); MessageBox.Show("Student added!"); } Close(); } catch (SqlException ex) { MessageBox.Show("There was a problem with the database, try again later"); #if DEBUG //preprocessor directive MessageBox.Show(ex.Message); #endif } }
private void PopulateStudentListBox() { List <Student> students = StudentDB.GetAllStudents(); //Ordering the students in alphabetical order students = students.OrderBy(stu => stu.FirstName).ToList(); //Databinding - internally loops through and adds each student lstStudents.DataSource = students; lstStudents.DisplayMember = nameof(Student.FullName); }
private void BtnDeleteStudent_Click(object sender, EventArgs e) { //If no student selected, tell user and break out of method if (lstStudents.SelectedIndex < 0) { MessageBox.Show("Please choose a student!"); return; } Student stu = lstStudents.SelectedItem as Student; string msg = $"Are you sure you want to delete {stu.StudentId} : {stu.FullName}. \n {stu.DateOfBirth.ToShortDateString()}\n{stu.ProgramOfChoice}."; DialogResult answer = MessageBox.Show(msg, "Delete Student?", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (answer == DialogResult.Yes) { if (StudentDB.Delete(stu.StudentId)) { PopulateStudentListBox(); MessageBox.Show("Student deleted successfully"); } } }