//When the save button is pressed private void save_button_Click(object sender, EventArgs e) { Regex checkName = new Regex("^[A-Za-z0-9 ]+$"); //check if the course name is invaild or empty if (!checkName.IsMatch(course_textbox.Text) || course_textbox.ForeColor == Color.Gray) { MessageBox.Show("Course name invaild!\nMake sure that the name contains only letters and numbers", "Error" , MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //Check if lecturer was chosen if (LecturerID_combo.SelectedItem == null) { MessageBox.Show("You must chose lecturer", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //Check if practitioner was chosen if (practitionerID_combo.SelectedItem == null) { MessageBox.Show("You must chose practitioner", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //Check if lecture duration was chosen if (Lectur_duration_combobox.SelectedItem == null) { MessageBox.Show("You must choose lecture duration for a course!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //Check if some item was selected in the practice/lab duration list if (Practice_checkbox.Checked && Practice_duration_combobox.SelectedItem == null) { MessageBox.Show("Please chose practice duration", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (Lab_Checkbox.Checked && lab_duration_combobox.SelectedItem == null) { MessageBox.Show("Please chose lab duration", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //Check if at least 1 check box is checked if (!Lab_Checkbox.Checked && !Practice_checkbox.Checked) { MessageBox.Show("For a course there must be at least lab and/or practice", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //Create new connection to write to database DataRowView oDataRowView = LecturerID_combo.SelectedItem as DataRowView; int lecturerID = (int)oDataRowView.Row["ID"]; oDataRowView = practitionerID_combo.SelectedItem as DataRowView; int practitionerID = (int)oDataRowView.Row["ID"]; //Check if the course exists if (SqlWorker.checkCourseExistence(course_textbox.Text)) { MessageBox.Show("The course " + course_textbox.Text + " already exists!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } string practiceDur = null, labDur = null; //Show confirm message string messageString = "Are you sure you want to add the following course to the department?" + "\nName: " + course_textbox.Text + "\nLecturer ID: " + lecturerID.ToString() + "\nPractitioner ID: " + practitionerID.ToString() + "\nLecutre duration: " + Lectur_duration_combobox.SelectedItem; if (Practice_checkbox.Checked) { messageString += "\nPractice duration: " + Practice_duration_combobox.SelectedItem; practiceDur = Practice_duration_combobox.SelectedItem.ToString(); } if (Lab_Checkbox.Checked) { messageString += "\nLab duration: " + lab_duration_combobox.SelectedItem; labDur = lab_duration_combobox.SelectedItem.ToString(); } DialogResult resault = MessageBox.Show(messageString, "Before you continue", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk); //If the user click ok, add the course to database if (resault == DialogResult.OK) { SqlWorker.addCourse(course_textbox.Text, lecturerID.ToString(), practitionerID.ToString(), Lectur_duration_combobox.SelectedItem.ToString(), practiceDur, labDur, checkBox1.Checked); MessageBox.Show("Course successfully added!", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); this.Close(); } }
public void Test_check_course_existence() { //Add new course SqlWorker.addCourse(courseName, lecturerID, practitionerID, LectureDuration, PracticeDuration, LabDuration, true); Assert.IsTrue(SqlWorker.GetDataSet("select * from Courses where CourseName='" + courseName + "' and Lecturer=" + lecID + " and practitioner=" + PracID + " and MaxStudent=20 and " + "LectureDuration=" + LecDuration + " and PracticeDuration=" + PracDuration + " and LabDuration=" + LabDur + " and Must=" + must).Tables[0].Rows.Count == 1 && SqlWorker.checkCourseExistence(courseName)); SqlWorker.ExecuteQueries("Delete from Courses where CourseName='" + courseName + "'"); }