/** * * This function is used to add the course into the database and then refresh the datagrid view on form frmCourse * * @return Boolean: true - addedd successfully | false - if course not addedd successfully * **/ private Boolean addCourse() { // Check if the user added all items in the form if (txtCourseCode.Text.Trim().Equals("") || txtCourseName.Text.Trim().Equals("") || txtLocation.Text.Trim().Equals("") || dtpTime.Text.Trim().Equals("") || nudCapacity.Value == 0 || nudCredits.Value == 0) { // Display error message MessageBox.Show("Oops... Not all fields were filled in. Please fill in all fields and try again.", "Invalid Entry", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { // Add the user into the database using (SqlConnection connection = new SqlConnection(Properties.Settings.Default.StudentManagerDBConnectionString)) { // Open the DB connection connection.Open(); // Check if the course code is unique srvDatabase.DatabaseClient client = new srvDatabase.DatabaseClient(); bool isUnique = client.checkUniqueCourse(txtCourseCode.Text.Trim()); // Check if the courseCode is in use if (!isUnique) { // Display Error Message MessageBox.Show("Oops... Look like the Course Code that you entered is already in use. Please enter another Course Code and try again!", "Course Code Taken", MessageBoxButtons.OK, MessageBoxIcon.Error); // Focus on the courseCode field txtCourseCode.Focus(); } else { // Add the course using (SqlCommand command = new SqlCommand("INSERT INTO courses (courseCode, name, location, time, capacity, credits) VALUES (@courseCode, @name, @location, @time, @capacity, @credits)", connection)) { command.Parameters.AddWithValue("@courseCode", txtCourseCode.Text); command.Parameters.AddWithValue("@name", txtCourseName.Text); command.Parameters.AddWithValue("@location", txtLocation.Text); command.Parameters.AddWithValue("@time", dtpTime.Value.ToString()); command.Parameters.AddWithValue("@capacity", nudCapacity.Value.ToString()); command.Parameters.AddWithValue("@credits", nudCredits.Value.ToString()); command.ExecuteNonQuery(); // Refresh the DataGrid on the Courses form coursesForm.coursesTableAdapter.Fill(coursesForm.dtsAllData.Courses); coursesForm.dgvCourses.Refresh(); coursesForm.dgvCourses.Parent.Refresh(); // Close the DB connection connection.Close(); // Return successful return(true); } } } } // Perform blank return to not process any more of this function return(false); }
/** * * This function is used to add the program into the database and then refresh the datagrid view on form frmPrograms * * @return Boolean: true - addedd successfully | false - if program not addedd successfully * **/ private Boolean addProgram() { // Check if the user added all items in the form if (txtProgramName.Text.Trim().Equals("") || txtProgramOutcome.Text.Trim().Equals("") || nudDuration.Value == 0) { // Display error message MessageBox.Show("Oops... Not all fields were filled in. Please fill in all fields and try again.", "Invalid Entry", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { // Add the user into the database using (SqlConnection connection = new SqlConnection(Properties.Settings.Default.StudentManagerDBConnectionString)) { // Open the DB connection connection.Open(); // Check if the program name is unique srvDatabase.DatabaseClient client = new srvDatabase.DatabaseClient(); bool isUnique = client.checkUniqueProgram(txtProgramName.Text.Trim()); // If the program name is in use display a message if (!isUnique) { // Display Error Message MessageBox.Show("Oops... Look like the Program Name that you entered is already in use. Please enter another Program Name and try again!", "Program Name Taken", MessageBoxButtons.OK, MessageBoxIcon.Error); // Close the DB connection connection.Close(); // Focus on the program name field txtProgramName.Focus(); } else { // Add the program using (SqlCommand command = new SqlCommand("INSERT INTO program (name, duration, coop, outcome) VALUES (@progName, @dur, @coop, @outcome)", connection)) { command.Parameters.AddWithValue("@progName", txtProgramName.Text); command.Parameters.AddWithValue("@dur", nudDuration.Value.ToString()); //determines if the program has the coop option if (chkbxCoop.Checked) { command.Parameters.AddWithValue("@coop", '1'); } else { command.Parameters.AddWithValue("@coop", '0'); } command.Parameters.AddWithValue("@outcome", txtProgramOutcome.Text); command.ExecuteNonQuery(); // Refresh the DataGrid on the Programs form programsForm.programTableAdapter.Fill(programsForm.dtsAllData.Program); programsForm.dgvPrograms.Refresh(); programsForm.dgvPrograms.Parent.Refresh(); // Close the DB connection connection.Close(); // Return successful return(true); } } } } // Perform blank return to not process any more of this function return(false); }