//Student private void Add_Click(object sender, RoutedEventArgs e) { int group; if (int.TryParse(GroupBox.Text, out group)) { } else { MessageBox.Show("You have entered a wrong data"); } if (GroupBox.Text == "" || StudentNameBox.Text == "" || HometownBox.Text == "") { MessageBox.Show("Fill in all data and continue"); } else { string ConnectionString = @"Data Source=DESKTOP-R0R983R;Initial Catalog=ADO.NET;Integrated Security=True"; using (SqlConnection cn = new SqlConnection(ConnectionString)) { cn.Open(); SqlTransaction transaction = cn.BeginTransaction(); SqlCommand command = cn.CreateCommand(); command.Transaction = transaction; try { command.CommandText = $"INSERT INTO STUDENT (IDGROUP, NAME, HOMETOWN) VALUES ({group}, '{name}', '{hometown}');"; command.ExecuteNonQuery(); transaction.Commit(); MessageBox.Show("Object has been added"); } catch (Exception ex) { MessageBox.Show(ex.Message); transaction.Rollback(); } } GroupBox.Clear(); StudentNameBox.Clear(); HometownBox.Clear(); } }
} //END OF MAIN WINDOW CONSTRUCTOR //An event handler that handles all of the button clicks on the main application page. private void EnrollButton_ClearButton_SetStaffButton_ClearStaffButton_AddToModuleButton_StudentModRemoveButton_Click(object sender, RoutedEventArgs e) { // STAFF // //Sets the details entered into the text boxes as the current staff members details. if (sender == SetStaffButton) //If the set staff button is clicked. { //A data Range check to make sure correct values hve been entered into each textbox. if (StaffNameBox.Text == "" || StaffEmailBox.Text == "" || StaffAddressBox.Text == "" || DepartmentBox.Text == "" || RoleBox.Text == "" || PayrollNoBox.Text == "") { //Display an error message box. MessageBox.Show("You have left one or more fields blank. Please enter values in all of the fields."); } else { //Range checks payroll number. if (int.Parse(PayrollNoBox.Text) < 9000 || int.Parse(PayrollNoBox.Text) > 9999) { MessageBox.Show("You have entered a payroll number that is not within the range (9000 - 9999). Re-enter."); } else { //Add this newly created staff to the staff list to then be added to the staff data grid. StaffList.Add(new Staff(StaffNameBox.Text, StaffEmailBox.Text, StaffAddressBox.Text, int.Parse(PayrollNoBox.Text), DepartmentBox.Text, RoleBox.Text)); } } } //END OF ADD STAFF BUTTON //Clears the currently entered details in the staaff details boxes. if (sender == ClearStaffButton) { //Clear the contents of the staff detail boxes. StaffNameBox.Clear(); StaffAddressBox.Clear(); StaffEmailBox.Clear(); PayrollNoBox.Clear(); DepartmentBox.Clear(); RoleBox.Clear(); } //END OF CLEAR STAFF BUTTON //STUDENT// //If the Enroll Button is clicked this method is carried out. //This will create a new student object and add it to the student list. //Any details entered into the textboxes will be saved to the current student object. //The students in the student list will be saved to a Student.txt file in the debug folder. if (sender == EnrollButton) { //A range check to make sure non of the fields are blank. if (StudentNameBox.Text == "" || StudentEmailBox.Text == "" || StudentAddressBox.Text == "" || MatricnoBox.Text == "") { MessageBox.Show("You have left one or more of the fields blank. Please enter values."); } else { if (int.Parse(MatricnoBox.Text) >= 1000 && int.Parse(MatricnoBox.Text) <= 9000) { //Add this newly created student to the student list. StudentList.Add(new Student(StudentNameBox.Text, StudentEmailBox.Text, StudentAddressBox.Text, int.Parse(MatricnoBox.Text))); } else { MessageBox.Show("The matriculation number box contains incorrect values. Enter within 1000 and 9000."); } } } //END OF ENROLL STUDENT BUTTON //A clear button to clear the contents in the student detail boxes. if (sender == ClearButton) { //Clear all student details in the text boxes. StudentNameBox.Clear(); StudentEmailBox.Clear(); StudentAddressBox.Clear(); MatricnoBox.Clear(); } //END OF CLEAR STUDENT DETAILS BUTTON //MODULES// //This function is carried out when the add to module button is clicked. //This takes the student selected from the combo box and adds that student to the selected module in the second combo box. if (sender == AddToModuleButton) { //This searches through the student list for a students name that is equal to the student combo box selection. Student search = StudentList.FirstOrDefault(z => z.Name == StudentCNameBox.Text); //If the name typed into the text box (studentCNameBox) is found then this is carried out. if (search != null) //If search is not equal to null. { //If the user selects Database Systems in the combo box when clicking the add button. if (ModuleComboBox.SelectedItem == "Database Systems") { //Add the student from the student list whos name matches the name in the textbox to the selected module. DatabaseList.Add(new Student(search.Name, search.MatricNumber, search.Mark, search.Status)); } //If the user selects Systems and Services in the combo box when clicking the add button. if (ModuleComboBox.SelectedItem == "Systems and Services") { //Add the student from the student list whos name matches the name in the textbox to the selected module. SystemsAndServicesList.Add(new Student(search.Name, search.MatricNumber, search.Mark, search.Status)); } //If the user selects Software Development in the combo box when clicking the add button. if (ModuleComboBox.SelectedItem == "Software Development") { //Add the student from the student list whos name matches the name in the textbox to the selected module. SoftwareDevelopmentList.Add(new Student(search.Name, search.MatricNumber, search.Mark, search.Status)); } } } //END OF ADD STUDENT TO MODULE BUTTON //A button that allows for the removal of a student from a specific module. if (sender == StudentModRemoveButton) //If the Remove Student button is clicked. { //This searches through the student list for a students name that is equal to the string entered into the StudentCNamebox. //Sets this object to look. Student look = StudentList.FirstOrDefault(z => z.Name == StudentCNameBox.Text); //If the name is found then this is carried out. if (look != null) { //If the Database Systems option is selected in the combo box. if (ModuleComboBox.SelectedItem == "Database Systems") { //Remove the student from the database list. //Creates a variable called itemToRemove and sets its values to equal the name entered in the textbox. //For each instance of that item in the list, it is removed from the list. var itemToRemove = DatabaseList.Where(z => z.Name == look.Name).ToList(); foreach (var item in itemToRemove) { DatabaseList.Remove(item); // Removes each instance of that student within the given module list. } } //If the Systems and Services option is selected in the module combo box and the remove student is clicked... if (ModuleComboBox.SelectedItem == "Systems and Services") { //Remove the student from the database list. //Creates a variable called itemToRemove and sets its values to equal the name entered in the textbox. var itemToRemove = SystemsAndServicesList.Where(z => z.Name == look.Name).ToList(); foreach (var item in itemToRemove) { SystemsAndServicesList.Remove(item); // Removes each instance of that student within the given module list. } } //If the Software Development option is selected in the combo box and the remove student button is clicked... if (ModuleComboBox.SelectedItem == "Software Development") { //Remove the student from the software development list. //Creates a variable called itemToRemove and sets its values to equal the name entered in the textbox. var itemToRemove = SoftwareDevelopmentList.Where(z => z.Name == look.Name).ToList(); foreach (var item in itemToRemove) { SoftwareDevelopmentList.Remove(item); // Removes each instance of that student within the given module list. } } } } //END OF STUDENT REMOVE BUTTON } //END OF BUTTON EVENT HANDLER