private void button1_Click(object sender, EventArgs e) { pmsstoreEntities entties = new pmsstoreEntities(); foreach (var j in this.checkedListBox1.CheckedItems) { tbl_Student studentItem = (tbl_Student)j; tbl_CSLecturer lecturerItem = (tbl_CSLecturer)this.listBox1.SelectedItem; int sessionId = Convert.ToInt32(this.comboBox1.SelectedValue); //check if the student has been allocated to any lecturer before in selected session var queryCheck = (from p in entties.tbl_Allocation where p.StudentID == studentItem.GUID && p.SessionID == sessionId select p).FirstOrDefault(); if (queryCheck != null) { //this student is already assigned to this or another lecturer this session, therefore continue to the next student continue; } tbl_Allocation allocation = new tbl_Allocation(); allocation.DateAllocated = DateTime.Now; allocation.LecturerID = lecturerItem.GUID; allocation.SessionID = Convert.ToInt32(this.comboBox1.SelectedValue); allocation.StudentID = studentItem.GUID; entties.tbl_Allocation.Add(allocation); } entties.SaveChanges(); }
private void RefreshItems() { try { pmsstoreEntities entties = new pmsstoreEntities(); for (int i = 0; i < this.checkedListBox1.Items.Count; i++) { foreach (var j in this.checkedListBox1.CheckedItems) { this.checkedListBox1.SetItemChecked(i, false); break; } } tbl_CSLecturer lecturerItem = (tbl_CSLecturer)this.listBox1.SelectedItem; int sessionId = Convert.ToInt32(this.comboBox1.SelectedValue); var queryAllocation = (from p in entties.tbl_Allocation where p.LecturerID == lecturerItem.GUID && p.SessionID == sessionId select p).ToList(); foreach (var allocation in queryAllocation) { foreach (var j in this.checkedListBox1.Items) { tbl_Student studentItem = (tbl_Student)j; if (studentItem.GUID == allocation.StudentID) { this.checkedListBox1.SelectedItem = j; int index = this.checkedListBox1.SelectedIndex; this.checkedListBox1.SetItemChecked(index, true); break; } } } } catch (Exception ex) { } }
private void button2_Click(object sender, EventArgs e) { pmsstoreEntities entties = new pmsstoreEntities(); foreach (var j in this.checkedListBox1.CheckedItems) { tbl_Student studentItem = (tbl_Student)j; tbl_CSLecturer lecturerItem = (tbl_CSLecturer)this.listBox1.SelectedItem; int sessionId = Convert.ToInt32(this.comboBox1.SelectedValue); //check if the student has been allocated to any lecturer before in selected session var queryCheck = (from p in entties.tbl_Allocation where p.StudentID == studentItem.GUID && p.SessionID == sessionId select p).FirstOrDefault(); if (queryCheck != null) { //remove entties.tbl_Allocation.Remove(queryCheck); } } entties.SaveChanges(); RefreshItems(); }