public void insertSolutionOnSystem(string path) { //clean timetable_temp string currentSemester = getCurrentSemester(); DBConnection.Delete("timetable_" + currentSemester); // Open document List <string> myValues = new List <string>(); string line; // Read the file and display it line by line. StreamReader file = new StreamReader("Output/" + path); while ((line = file.ReadLine()) != null) { myValues.Add(line); } foreach (string item in myValues) { string row = string.Join(" ", item.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)); string[] items = row.Split(' '); if ((items.Count() != 4) || !items[0].StartsWith("c") || !items[1].StartsWith("r")) { throw new Exception("Selected file is not on the correct format!"); } string values = "'" + items[0] + "', '" + items[1] + "', '" + items[2] + "', '" + items[3] + "'"; int res = DBConnection.Create("timetable_" + currentSemester, "course_code, room_code, day, start_period", values); } }
private void saveTeacher_MouseUp(object sender, EventArgs e) { if (txtName.Text.Length == 0) { MessageBox.Show("Teacher name is required!", "Information", MessageBoxButton.OK, MessageBoxImage.Information); return; } if (txtSurname.Text.Length == 0) { MessageBox.Show("Teacher surname is required!", "Information", MessageBoxButton.OK, MessageBoxImage.Information); return; } if(txtEmail.Text.Length > 0 && !Regex.IsMatch(txtEmail.Text, @"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$")) { MessageBox.Show("Please enter a valid email!", "Information", MessageBoxButton.OK, MessageBoxImage.Information); return; } if (txtId.Text.Length == 0) { string code = null; for (int i = 1; i < 1000; i++) { code = "t" + i.ToString("000"); DataView data = DBConnection.Select("teachers", "'*'", " code = '" + code + "'").DefaultView; if (data.Count == 0) break; } string values = "'" + txtName.Text + "', '" + txtSurname.Text + "', '" + txtTitle.Text + "', '" + txtEmail.Text + "', '" + txtPhone.Text + "', '" + code.ToString() + "', '" + txtNote.Text + "'"; int result = DBConnection.Create("teachers", "name, surname, title, email, phone, code, note", values); if (result > 0) { MessageBox.Show("Successfully created new Teacher!", "Information", MessageBoxButton.OK); txtId.Text = result.ToString(); } } else if (Convert.ToInt32(txtId.Text) > 0) { string values = "name='" + txtName.Text + "', surname='" + txtSurname.Text + "', title='" + txtTitle.Text + "', email='" + txtEmail.Text + "', phone='" + txtPhone.Text + "', code='" + txtCode.Text + "', note='" + txtNote.Text + "'"; int result = DBConnection.Update("teachers", values, Convert.ToInt32(txtId.Text)); if (result > 0) MessageBox.Show("Successfully updated Teacher!", "Information", MessageBoxButton.OK); } }
public void main() { try { Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog(); // Set filter for file extension and default file extension dlg.DefaultExt = ".txt"; dlg.Filter = "TXT Files (*.txt)|*.txt"; // Display OpenFileDialog by calling ShowDialog method Nullable <bool> result = dlg.ShowDialog(); // Get the selected file name and display in a TextBox if (result == true) { //clean timetable_temp DBConnection.Delete("timetable_temp"); // Open document string filename = dlg.FileName; List <string> myValues = new List <string>(); string line; // Read the file and display it line by line. StreamReader file = new StreamReader(filename); while ((line = file.ReadLine()) != null) { myValues.Add(line); } foreach (string item in myValues) { string row = string.Join(" ", item.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)); string[] items = row.Split(' '); if ((items.Count() != 4) || !items[0].StartsWith("c") || !items[1].StartsWith("r")) { throw new Exception("Selected file is not on the correct format!"); } string values = "'" + items[0] + "', '" + items[1] + "', '" + items[2] + "', '" + items[3] + "'"; int res = DBConnection.Create("timetable_temp", "course_code, room_code, day, start_period", values); } TimetablingResultDisplay display = new TimetablingResultDisplay(); display.Show(); } } catch (Exception e) { MessageBox.Show(e.Message); } }
private void insertCoursePreferences(int courseId, int teacherId = 0) { if (teacherId > 0) { DataTable preferences = DBConnection.Select("teacher_periods_preferences", "day, period", "teacher_id=" + teacherId); if (preferences.Rows.Count != 0) { foreach (DataRow preference in preferences.Rows) { string values = "'" + courseId + "', '" + preference.ItemArray[0] + "', '" + preference.ItemArray[1] + "'"; int result = DBConnection.Create("course_periods_preferences", "course_id, day, period", values); } } } }
private void saveDepartment_MouseUp(object sender, EventArgs e) { try { if (txtName.Text == null || txtName.Text == "") { MessageBox.Show("Study program name is required!", "Information", MessageBoxButton.OK, MessageBoxImage.Information); return; } if (int.Parse(comboBox.SelectedIndex.ToString()) < 0) { MessageBox.Show("Please select study program's faculty!", "Information", MessageBoxButton.OK, MessageBoxImage.Information); return; } if (txtId.Text == "") { string values = "'" + txtName.Text + "', '" + comboBox.SelectedValue + "'"; int result = DBConnection.Create("departments", "name, faculty_id", values); if (result > 0) { MessageBox.Show("Successfully created new study program!", "Information", MessageBoxButton.OK, MessageBoxImage.Information); } } else if (Convert.ToInt32(txtId.Text) > 0) { string values = " name='" + txtName.Text + "', faculty_id='" + comboBox.SelectedValue + "'"; int result = DBConnection.Update("departments", values, Convert.ToInt32(txtId.Text)); if (result > 0) { MessageBox.Show("Successfully updated study program!", "Information", MessageBoxButton.OK, MessageBoxImage.Information); } } Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void saveCoursePeriodPreferences_MouseUp(object sender, MouseButtonEventArgs e) { var course_id = comboBoxCourse.SelectedValue; var day = comboBoxDay.SelectedValue; var period = comboBoxPeriod.SelectedValue; string values = "'" + course_id + "', '" + day + "', '" + period + "'"; int result = DBConnection.Create("course_periods_preferences", "course_id, day, period", values); if (result > 0) { MessageBox.Show("Successfully created new Course Period Preferences!", "Information", MessageBoxButton.OK); } Close(); }
private void saveCourseDepartmentRel_MouseUp(object sender, EventArgs e) { try { if (int.Parse(comboBoxCourse.SelectedIndex.ToString()) < 0) { MessageBox.Show("Please select course!", "Information", MessageBoxButton.OK, MessageBoxImage.Information); return; } if (int.Parse(comboBoxDepartment.SelectedIndex.ToString()) < 0) { MessageBox.Show("Please select department!", "Information", MessageBoxButton.OK, MessageBoxImage.Information); return; } var course_id = comboBoxCourse.SelectedValue; var department_id = comboBoxDepartment.SelectedValue; DataView data = DBConnection.Select("course_department_rel", "'*'", " course_id = '" + course_id + "' AND department_id='" + department_id + "'").DefaultView; if (data.Count != 0) { MessageBox.Show("This course is already assigned to the selected department!", "Information", MessageBoxButton.OK, MessageBoxImage.Information); return; } string values = "'" + course_id + "', '" + department_id + "'"; int result = DBConnection.Create("course_department_rel", "course_id, department_id", values); if (result > 0) { MessageBox.Show("Successfully created new course department assignment!", "Information", MessageBoxButton.OK); } Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK); } }
private void OnDoubleClick(object sender, MouseButtonEventArgs e) { if (e.LeftButton == MouseButtonState.Pressed && e.ClickCount == 2) { foreach (UControl child in childs) { if (child.Clicked == "true") { if (comboBoxRooms.SelectedIndex != -1) { if (child.ControlAssigned == "false" && child.BlockedAssigned != "true") { //check if clicked period is a red position string cCode = txtCode.Text; int day = int.Parse(child.Column) - 1; int startPeriod = int.Parse(child.Row) - 1; string rCode = ""; string query = " SELECT code FROM rooms WHERE id=" + comboBoxRooms.SelectedValue; DataTable res = DBConnection.RAW_Select(query); ArrayList result = new ArrayList(); foreach (DataRow r in res.Rows) { rCode = r.ItemArray[0].ToString(); } string values = "'" + cCode + "', '" + rCode + "', '" + day + "', '" + startPeriod + "'"; string table = "timetable_" + getCurrentSemester(); DataView data = DBConnection.Select(table, "'*'", " course_code = '" + cCode + "'").DefaultView; if (data.Count == 0) { int createdId = DBConnection.Create(table, "course_code, room_code, day, start_period", values); } else { string vals = "room_code='" + rCode + "', day='" + day + "', start_period='" + startPeriod + "'"; int createdId = DBConnection.Update(table, vals, 0, " course_code='" + cCode + "'"); } MessageBox.Show("Class was placed successfully!", "Information", MessageBoxButton.OK, MessageBoxImage.Information); Close(); break; } else { MessageBox.Show("Class can not be placed at this position!", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning); child.Clicked = "false"; break; } } else { MessageBox.Show("Please select a room first!", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning); child.Clicked = "false"; break; } } } } }
/* private void preferences() * { * DataTable data = DBConnection.Select(" courses c " + * " LEFT JOIN course_department_rel cdr ON c.id = cdr.course_id ", " c.id ", "c.detail='laboratory'");//AND c.detail='lecture' * ArrayList courseIds = new ArrayList(); * * foreach (DataRow courseRow in data.Rows) * courseIds.Add(courseRow.ItemArray[0]); * * ArrayList days = new ArrayList() {1,2,3,4,5,6}; * ArrayList periods = new ArrayList() {1,2,3 }; * int res; * foreach (int id in courseIds) * { * foreach(int day in days) * { * foreach(int period in periods) * { * DataView dt = DBConnection.Select("course_periods_preferences", "'*'", " course_id = " + id + " AND day=" + day + " AND period=" + period ).DefaultView; * if (dt.Count == 0) * res = DBConnection.Create("course_periods_preferences", " course_id, day, period", "'" + id + "','" + day + "','" + period + "'"); * } * } * } * }*/ private void roomPreferences(string code, ArrayList lecRooms, ArrayList numRooms, ArrayList labRooms) { //Lecture DataTable lecData = DBConnection.Select(" courses ", " id ", " base_parent_code='" + code + "' AND detail='lecture'");//AND c.detail='lecture' ArrayList lecCourseIds = new ArrayList(); foreach (DataRow courseRow in lecData.Rows) { lecCourseIds.Add(courseRow.ItemArray[0]); } int res; foreach (int id in lecCourseIds) { foreach (int lecRoom in lecRooms) { DataView dt = DBConnection.Select("courses_rooms_rel", "'*'", " course_id = " + id + " AND room_id=" + lecRoom).DefaultView; if (dt.Count == 0) { res = DBConnection.Create("courses_rooms_rel", " course_id, room_id", "'" + id + "','" + lecRoom + "'"); } } } //Numeric if (numRooms.Count > 0) { DataTable numData = DBConnection.Select(" courses ", " id ", " base_parent_code='" + code + "' AND detail='numeric'");//AND c.detail='lecture' ArrayList numCourseIds = new ArrayList(); foreach (DataRow courseRow in numData.Rows) { numCourseIds.Add(courseRow.ItemArray[0]); } int res1; foreach (int id in numCourseIds) { foreach (int numRoom in numRooms) { DataView dt = DBConnection.Select("courses_rooms_rel", "'*'", " course_id = " + id + " AND room_id=" + numRoom).DefaultView; if (dt.Count == 0) { res1 = DBConnection.Create("courses_rooms_rel", " course_id, room_id", "'" + id + "','" + numRoom + "'"); } } } } //Laboratory if (labRooms.Count > 0) { DataTable labData = DBConnection.Select(" courses ", " id ", " base_parent_code='" + code + "' AND detail='laboratory'");//AND c.detail='lecture' ArrayList labCourseIds = new ArrayList(); foreach (DataRow courseRow in labData.Rows) { labCourseIds.Add(courseRow.ItemArray[0]); } int res2; foreach (int id in labCourseIds) { foreach (int labRoom in labRooms) { DataView dt = DBConnection.Select("courses_rooms_rel", "'*'", " course_id = " + id + " AND room_id=" + labRoom).DefaultView; if (dt.Count == 0) { res2 = DBConnection.Create("courses_rooms_rel", " course_id, room_id", "'" + id + "','" + labRoom + "'"); } } } } }
private void saveRoom_MouseUp(object sender, EventArgs e) { try { if (txtName.Text == null || txtName.Text == "") { MessageBox.Show("Room name is required!", "Information", MessageBoxButton.OK, MessageBoxImage.Information); return; } if (txtSize.Text == null || txtSize.Text == "") { MessageBox.Show("Room size is required!", "Information", MessageBoxButton.OK, MessageBoxImage.Information); return; } if (int.Parse(comboBox.SelectedIndex.ToString()) < 0) { MessageBox.Show("Please select building!", "Information", MessageBoxButton.OK, MessageBoxImage.Information); return; } if (txtId.Text == "") { string code = null; for (int i = 1; i < 1000; i++) { code = "r" + i.ToString("000"); DataView data = DBConnection.Select("rooms", "'*'", " code = '" + code + "'").DefaultView; if (data.Count == 0) { break; } } string values = "'" + txtName.Text + "', '" + Convert.ToInt32(txtSize.Text) + "', '" + code.ToString() + "', '" + comboBox.SelectedValue + "', '" + txtNote.Text + "'"; int result = DBConnection.Create("rooms", "name, size, code, building_id, note", values); if (result > 0) { MessageBox.Show("New room has been successfully created!", "Information", MessageBoxButton.OK, MessageBoxImage.Information); } } else if (Convert.ToInt32(txtId.Text) > 0) { string values = " name='" + txtName.Text + "', size='" + Convert.ToInt32(txtSize.Text) + "', code='" + txtCode.Text + "', building_id='" + comboBox.SelectedValue + "', note='" + txtNote.Text + "'"; int result = DBConnection.Update("rooms", values, Convert.ToInt32(txtId.Text)); if (result > 0) { MessageBox.Show("Room has been successfully updated!", "Information", MessageBoxButton.OK, MessageBoxImage.Information); } } Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void pnlBackground_MouseDown(object sender, MouseButtonEventArgs e) { if (e.LeftButton == MouseButtonState.Pressed) { var day = int.Parse(lblColumn.Text); var period = int.Parse(lblRow.Text); if (lblClicked.Text == "false") { if ((Parent.GetType() == typeof(Grid)) && ((Parent as Grid).Parent.GetType() == typeof(Grid)) && (((Parent as Grid).Parent as Grid).Parent.GetType() == typeof(PreferencesWindow))) { if (!(((Parent as Grid).Parent as Grid).Parent as PreferencesWindow).validatePreference()) { MessageBox.Show("Teacher should be available at least half of the week! In order to add other constraints, please remove some existing constraints!", "Information", MessageBoxButton.OK, MessageBoxImage.Information); return; } } lblClicked.Text = "true"; PanelBackground = "#5E716A"; if (TeacherId > 0) { DataTable preferences = DBConnection.Select("teacher_periods_preferences", "day, period", "teacher_id=" + TeacherId + " AND day=" + day + " AND period=" + period); if (preferences.Rows.Count == 0) { string values = "'" + TeacherId + "', '" + day + "', '" + period + "'"; int result = DBConnection.Create("teacher_periods_preferences", "teacher_id, day, period", values); } } if (CourseIds.Count > 0) { foreach (int cId in CourseIds) { DataView data = DBConnection.Select("course_periods_preferences", "'*'", " course_id = " + cId + " AND day=" + day + " AND period=" + period).DefaultView; if (data.Count == 0) { string values = "'" + cId + "', '" + day + "', '" + period + "'"; int result = DBConnection.Create("course_periods_preferences", "course_id, day, period", values); } } } } else { lblClicked.Text = "false"; PanelBackground = "#EEEEEE"; if (TeacherId > 0) { int result = DBConnection.Delete("teacher_periods_preferences", 0, " teacher_id=" + TeacherId + " AND day=" + day + " AND period=" + period); } if (CourseIds.Count > 0) { foreach (int cId in CourseIds) { int result = DBConnection.Delete("course_periods_preferences", 0, " course_id=" + cId + " AND day=" + day + " AND period=" + period); } } } } }
private void insertCourse(Thread newWindowThread, bool hideMessage = false) { try { string code = null; for (int i = 1; i < 1000; i++) { code = "p" + i.ToString("000"); DataView data = DBConnection.Select("courses", "'*'", " code = '" + code + "'").DefaultView; if (data.Count == 0) { break; } } validateFields(newWindowThread); int departmentRes; List <ComboDepartments> departments = courseDepartmentsGrid.Items.OfType <ComboDepartments>().ToList(); if (departments.Count() == 0) { throw new Exception("No department selected!"); } List <CourseTeachers> teachersGroups = courseTeacherRelationGrid.Items.OfType <CourseTeachers>().ToList(); int nrGroups = int.Parse(comboBoxGroups.SelectedIndex.ToString()) >= 0 ? int.Parse(comboBoxGroups.SelectedValue.ToString()) : 0; int nrExerciseGroups = int.Parse(comboBoxNrExerciseGroups.SelectedIndex.ToString()) >= 0 ? int.Parse(comboBoxNrExerciseGroups.SelectedValue.ToString()) : 0; int nrLaboratoryGroups = int.Parse(comboBoxLabExercisesGroups.SelectedIndex.ToString()) >= 0 ? int.Parse(comboBoxLabExercisesGroups.SelectedValue.ToString()) : 0; string lectures = comboBoxNrLectures.SelectedIndex >= 0 ? comboBoxNrLectures.SelectedValue.ToString() : "0"; string numLectures = comboBoxNrNumExercLectures.SelectedIndex >= 0 ? comboBoxNrNumExercLectures.SelectedValue.ToString() : "0"; string labLectures = comboBoxNrLabExercLectures.SelectedIndex >= 0 ? comboBoxNrLabExercLectures.SelectedValue.ToString() : "0"; string number_of_lectures = lectures + "+" + numLectures + "+" + labLectures; string ects = comboBoxEcts.SelectedValue.ToString(); int min_days = int.Parse(comboBoxMinDays.SelectedValue.ToString()); var type = comboBoxType.SelectedValue; var degree = comboBoxDegree.SelectedValue; var semester_id = comboBoxSemester.SelectedValue; var double_lectures = int.Parse(comboBoxDoubleLectures.SelectedValue.ToString()) == 1 ? true : false; string values = "'" + txtName.Text + "', '" + code.ToString() + "', '" + ects + "', '" + type + "', '" + degree + "', '" + semester_id + "', '" + number_of_lectures + "', '" + min_days + "', '" + double_lectures + "', '" + int.Parse(txtNrStudents.Text) + "', '" + int.Parse(comboBoxGroups.SelectedValue.ToString()) + "', 'base', null, null, null, null"; string columns = "name, code, ects, type, degree, semester_id, number_of_lectures, min_days, double_lectures, number_of_students, groups, detail, lecture_group, numeric_group, laboratory_group, base_parent_code"; int result = DBConnection.Create("courses", columns, values); //Returns last inserted id //Insert Groups of Course if (result > 0) { int gr = 0, ex = 0, lab = 0; int[] groupIds = new int[nrGroups]; int[] numExercisesIds = new int[nrExerciseGroups]; int[] labExercisesIds = new int[nrLaboratoryGroups]; if (nrGroups > 0) { int studentsPerGroup = Convert.ToInt32(txtNrStudents.Text) / nrGroups; for (int i = 1; i <= nrGroups; i++) { int exercisePerGroup = nrExerciseGroups / nrGroups; int exDiff = nrExerciseGroups - (nrGroups * exercisePerGroup); string groupsCode = null; for (int j = 1; j < 1000; j++) { groupsCode = "c" + j.ToString("000"); DataView data = DBConnection.Select("courses", "'*'", " code = '" + groupsCode + "'").DefaultView; if (data.Count == 0) { break; } } string groupValues = "'" + txtName.Text + ((nrGroups > 1) ? (" Gr. " + i.ToString()) : " ") + "', '" + groupsCode.ToString() + "', '" + ects + "', '" + type + "', '" + degree + "', '" + semester_id + "', '" + number_of_lectures + "', '" + min_days + "', '" + double_lectures + "', '" + studentsPerGroup + "', '" + result + "', 'lecture', 'Gr" + i.ToString() + "', null, null, '" + code.ToString() + "'"; string grColumns = "name, code, ects, type, degree, semester_id, number_of_lectures, min_days, double_lectures, number_of_students, parent_id, detail, lecture_group, numeric_group, laboratory_group, base_parent_code"; int grResult = DBConnection.Create("courses", grColumns, groupValues); groupIds[gr] = grResult; gr++; //Insert Exercises per Group if (exercisePerGroup > 0) { if (i == 1 && exDiff > 0) { exercisePerGroup += exDiff; } int studentsPerExercises = studentsPerGroup / exercisePerGroup; for (int a = 1; a <= exercisePerGroup; a++) { int laboratoryPerGroup = nrLaboratoryGroups / nrExerciseGroups; int labDiff = nrLaboratoryGroups - (nrExerciseGroups * laboratoryPerGroup); string exerciseCode = null; for (int j = 1; j < 1000; j++) { exerciseCode = groupsCode + j; DataView data = DBConnection.Select("courses", "'*'", " code = '" + exerciseCode + "'").DefaultView; if (data.Count == 0) { break; } } string exGroup = i.ToString() + alphabet[a - 1]; string exerciseValues = "'" + txtName.Text + ((nrGroups > 1) ? (" Gr. ") : " ") + (nrGroups > 1 || exercisePerGroup > 1 ? (i.ToString() + exGroup.Replace(i.ToString(), "")) : "") + "', '" + exerciseCode.ToString() + "', '" + ects + "', '" + type + "', '" + degree + "', '" + semester_id + "', '" + number_of_lectures + "', '" + min_days + "', '" + double_lectures + "', '" + studentsPerExercises + "', '" + grResult + "', 'numeric', 'Gr" + i.ToString() + "', '" + exGroup + "', null, '" + code.ToString() + "'"; int exResult = DBConnection.Create("courses", grColumns, exerciseValues); numExercisesIds[ex] = exResult; ex++; //Insert Laboratory Exercises if (laboratoryPerGroup > 0) { if (a == 1 & labDiff > 0) { laboratoryPerGroup += labDiff; } int studentsPerLaboratory = studentsPerExercises / laboratoryPerGroup; for (int b = 0; b < laboratoryPerGroup; b++) { string labCode = null; for (int j = 1; j < 1000; j++) { labCode = exerciseCode + j; DataView data = DBConnection.Select("courses", "'*'", " code = '" + labCode + "'").DefaultView; if (data.Count == 0) { break; } } string labValues = "'" + txtName.Text + ((nrGroups > 1) ? (" Gr. ") : " ") + (nrGroups > 1 || (exercisePerGroup > 1 && laboratoryPerGroup >= 1) ? (i.ToString() + exGroup.Replace(i.ToString(), "") + primes[b]) : "") + "', '" + labCode.ToString() + "', '" + ects + "', '" + type + "', '" + degree + "', '" + semester_id + "', '" + number_of_lectures + "', '" + min_days + "', '" + double_lectures + "', '" + studentsPerLaboratory + "', '" + exResult + "', 'laboratory', 'Gr" + i.ToString() + "', '" + exGroup + "', '" + exGroup + primes[b] + "', '" + code.ToString() + "'"; int labResult = DBConnection.Create("courses", grColumns, labValues); labExercisesIds[lab] = labResult; lab++; } } } } //Insert Laboratory if no exercise groups else if (nrLaboratoryGroups / nrGroups > 0) { int laboratoryPerGroup = nrLaboratoryGroups / nrGroups; int studentsPerLaboratory = studentsPerGroup / laboratoryPerGroup; int labDiff = nrLaboratoryGroups - (nrGroups * laboratoryPerGroup); if (i == 1 && labDiff > 0) { laboratoryPerGroup += labDiff; } for (int b = 1; b <= laboratoryPerGroup; b++) { string labCode = null; for (int j = 1; j < 1000; j++) { labCode = groupsCode + j; DataView data = DBConnection.Select("courses", "'*'", " code = '" + labCode + "'").DefaultView; if (data.Count == 0) { break; } } double des = Convert.ToDouble(b / 2.0); double devideRes = Math.Ceiling(Convert.ToDouble(b / 2.0)); int remain = b % 2; string labName = i.ToString() + alphabet[Convert.ToInt32(devideRes) - 1] + primes[remain]; string labValues = "'" + txtName.Text + ((nrGroups > 1) ? (" Gr. ") : " ") + (laboratoryPerGroup > 1 ? (labName) : "") + "', '" + labCode.ToString() + "', '" + ects + "', '" + type + "', '" + degree + "', '" + semester_id + "', '" + number_of_lectures + "', '" + min_days + "', '" + double_lectures + "', '" + studentsPerLaboratory + "', '" + grResult + "', 'laboratory', 'Gr" + i.ToString() + "', null, '" + labName + "', '" + code.ToString() + "'"; int labResult = DBConnection.Create("courses", grColumns, labValues); labExercisesIds[lab] = labResult; lab++; } } } //Get teacher and their number of groups List <CourseTeachers> teachers = courseTeacherRelationGrid.Items.OfType <CourseTeachers>().ToList(); int lecture = 0, exercise = 0, laboratory = 0; foreach (CourseTeachers row in teachers) { while (row.Lectures > 0) { if (lecture >= groupIds.Count()) { break; } string vals = "'" + groupIds[lecture] + "', '" + row.TeacherID + "'"; int res = DBConnection.Create("course_teacher_rel", "course_id, teacher_id", vals); //Insert course preferences insertCoursePreferences(groupIds[lecture], row.TeacherID); lecture++; row.Lectures--; } while (row.NumericExercises > 0) { if (exercise >= numExercisesIds.Count()) { break; } string vals = "'" + numExercisesIds[exercise] + "', '" + row.TeacherID + "'"; int res = DBConnection.Create("course_teacher_rel", "course_id, teacher_id", vals); //Innsert course preferences insertCoursePreferences(numExercisesIds[exercise], row.TeacherID); exercise++; row.NumericExercises--; } while (row.LaboratoryExercises > 0) { if (laboratory >= labExercisesIds.Count()) { break; } string vals = "'" + labExercisesIds[laboratory] + "', '" + row.TeacherID + "'"; int res = DBConnection.Create("course_teacher_rel", "course_id, teacher_id", vals); //Insert course preferences insertCoursePreferences(labExercisesIds[laboratory], row.TeacherID); laboratory++; row.LaboratoryExercises--; } } // Get all registered rooms DataTable rooms = DBConnection.Select("rooms", "id, name"); List <CourseRooms> allRooms = new List <CourseRooms>(); if (rooms.Rows.Count != 0) { foreach (DataRow room in rooms.Rows) { //First column is ID allRooms.Add(new CourseRooms() { RoomID = int.Parse(room.ItemArray[0].ToString()), RoomName = room.ItemArray[1].ToString() }); } } //Get rooms and their number of groups int roomRes; List <CourseRooms> lectureRooms = courseRoomRelationLecturesGrid.Items.OfType <CourseRooms>().ToList(); List <CourseRooms> numericExRooms = courseRoomRelationNumExGrid.Items.OfType <CourseRooms>().ToList(); List <CourseRooms> laboratoryExRooms = courseRoomRelationLabExGrid.Items.OfType <CourseRooms>().ToList(); foreach (CourseRooms row in allRooms) { if (lectureRooms.Count > 0 && !lectureRooms.Where(x => x.RoomID == row.RoomID).Any()) { foreach (int m in groupIds) { roomRes = DBConnection.Create("courses_rooms_rel", "course_id, room_id", "'" + m + "', '" + row.RoomID + "'"); } } if (numericExRooms.Count > 0 && !numericExRooms.Where(x => x.RoomID == row.RoomID).Any()) { foreach (int n in numExercisesIds) { roomRes = DBConnection.Create("courses_rooms_rel", "course_id, room_id", "'" + n + "', '" + row.RoomID + "'"); } } if (laboratoryExRooms.Count > 0 && !laboratoryExRooms.Where(x => x.RoomID == row.RoomID).Any()) { foreach (int o in labExercisesIds) { roomRes = DBConnection.Create("courses_rooms_rel", "course_id, room_id", "'" + o + "', '" + row.RoomID + "'"); } } } foreach (ComboDepartments row in departments) { departmentRes = DBConnection.Create("course_department_rel", "course_id, department_id", "'" + result + "', '" + row.DepartmentID + "'"); if (groupIds.Length > 0) { foreach (int m in groupIds) { departmentRes = DBConnection.Create("course_department_rel", "course_id, department_id", "'" + m + "', '" + row.DepartmentID + "'"); } } if (numExercisesIds.Length > 0) { foreach (int n in numExercisesIds) { departmentRes = DBConnection.Create("course_department_rel", "course_id, department_id", "'" + n + "', '" + row.DepartmentID + "'"); } } if (labExercisesIds.Length > 0) { foreach (int o in labExercisesIds) { departmentRes = DBConnection.Create("course_department_rel", "course_id, department_id", "'" + o + "', '" + row.DepartmentID + "'"); } } } } } newWindowThread.Abort(); if (!hideMessage) { MessageBox.Show("Successfully generated new Courses!", "Information", MessageBoxButton.OK, MessageBoxImage.Information); } else { MessageBox.Show("Courses were successfully updated!", "Information", MessageBoxButton.OK, MessageBoxImage.Information); } Close(); if (parentGrid.IsVisible) { parentGrid.filterCourse(); } } catch (Exception e) { newWindowThread.Abort(); MessageBox.Show(e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }