public StudentModal(object owner, Modes mode = Modes.CREATE, Student student = null) { Student = mode == Modes.CREATE ? new Student() : student; Mode = mode; Parant = (MainWindow)owner; DataContext = this; InitializeComponent(); if (mode == Modes.EDIT) { if (student is null) { throw new ArgumentNullException("Student can not be null when modal opened in edit mode"); } SetUpEditMode(student); } SurnameTB.Focus(); }
void Add_Click(object sender, EventArgs e) { if (NameTB.Text.Trim().Length < 1 || SurnameTB.Text.Trim().Length < 1 || PatronymicTB.Text.Trim().Length < 1 || FacultyCB.SelectedIndex == -1 || SpecialtyCB.SelectedIndex == -1 || MarkTB.Text.Trim().Length < 1) { MessageBox.Show("Fill all data"); return; } if (!Single.TryParse(MarkTB.Text.Trim().Replace(',', '.'), out var mark) || mark < 50 || mark > 100) { MessageBox.Show("Enter mark between 50 and 100"); return; } var b = BirthdayDT.Value; bool result = new Student { Surname = SurnameTB.Text.Trim(), Name = NameTB.Text.Trim(), Patronymic = PatronymicTB.Text.Trim(), Gender = Male.Checked, Birthday = new DateTime(b.Year, b.Month, b.Day), Mark = mark }.Insert(); if (!result) { MessageBox.Show("An error occured"); } DB.Connection.Open(); string query = "SELECT MAX(id) " + "FROM mycursa4_Db.Student;"; var reader = new MySqlCommand(query, DB.Connection).ExecuteReader(); reader.Read(); int studentId = (int)reader[0]; DB.Connection.Close(); result = new Course { CourseNum = (int)CourseUD.Value }.Insert(); if (!result) { MessageBox.Show("An error occured"); } DB.Connection.Open(); var cmd = DB.Connection.CreateCommand(); cmd.CommandText = "INSERT INTO mycursa4_Db.showStudent (student, faculty, specialty, course) " + $"VALUES ('{studentId}','{FacultyId}','{SpecialtyId}', '{(int)CourseUD.Value}');"; cmd.ExecuteNonQuery(); DB.Connection.Close(); NameTB.Text = SurnameTB.Text = PatronymicTB.Text = MarkTB.Text = String.Empty; FacultyCB.SelectedItem = SpecialtyCB.SelectedItem = null; CourseUD.Value = 1; Male.Checked = true; SurnameTB.Focus(); }