// Create a new student with input from the user private void NewStudent_Click(object sender, RoutedEventArgs e) { try { // Use the StudentDialog to get the details of the student from the user StudentDialog sd = new StudentDialog(); // Display the form and get the details of the new student if (sd.ShowDialog().Value) { // When the user closes the form, retrieve the details of the student from the form // and use them to create a new Student object Student newStudent = new Student(); newStudent.FirstName = sd.firstName.Text; newStudent.LastName = sd.lastName.Text; newStudent.Password = sd.password.Text; // Generate the UserName property - lastname with the initial letter of the first name all converted to lowercase newStudent.UserName = (newStudent.LastName + newStudent.FirstName.Substring(0, 1)).ToLower(); // Generate a unique ID for the user: Use the maximum StudentID in the Students collection and add 1 newStudent.StudentID = (from s in DataSource.Students select s.StudentID).Max() + 1; // Add the student to the Students collection DataSource.Students.Add(newStudent); } } catch (Exception ex) { MessageBox.Show(ex.Message, "Error creating new student", MessageBoxButton.OK, MessageBoxImage.Error); } }
// Create a new student with input from the user private void NewStudent_Click(object sender, RoutedEventArgs e) { try { // Use the StudentDialog to get the details of the student from the user StudentDialog sd = new StudentDialog(); // Display the form and get the details of the new student if (sd.ShowDialog().Value) { // When the user closes the form, retrieve the details of the student from the form // and use them to create a new Student object Grades.DataModel.Student newStudent = new Grades.DataModel.Student(); newStudent.FirstName = sd.firstName.Text; newStudent.LastName = sd.lastName.Text; newStudent.User = new User(); if (!newStudent.User.SetPassword(Role.Student, sd.password.Text)) { throw new Exception("Password must be at least 6 characters long. Student not created"); } // Generate the UserName property - lastname with the initial letter of the first name all converted to lowercase newStudent.User.UserName = (newStudent.LastName + newStudent.FirstName.Substring(0, 1)).ToLower(); // Generate a unique ID for the user newStudent.UserId = Guid.NewGuid(); // Assign a value for the ImageName field newStudent.ImageName = "No photo"; // Generate default values for remaining properties of user object newStudent.User.ApplicationId = (from Grades.DataModel.User u in SessionContext.DBContext.Users select u.ApplicationId).FirstOrDefault(); newStudent.User.IsAnonymous = false; newStudent.User.LastActivityDate = DateTime.Now; newStudent.User.UserId = newStudent.UserId; // Add the student to the Students collection // TODO: Exercise 2: Task 2i: Use the AddToStudents method to add a new student SessionContext.DBContext.AddToStudents(newStudent); SessionContext.Save(); } } catch (Exception ex) { MessageBox.Show(ex.Message, "Error creating new student", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void ButtonEdit_Click(object sender, RoutedEventArgs e) { Student studentEdit = studentGrd.SelectedItem as Student; if (studentEdit == null) { return; } StudentDialog studentDialog = new StudentDialog(new Student(studentEdit)) { Title = "Editar Funcionário" }; if (studentDialog.ShowDialog() == true) { DbContextHelper.EditStudent(_db, studentDialog.Student); } }