//------------------------------------------------------------------------------------------------------- //reset DB to original state where registration is 0 private void fILEToolStripMenuItem_Click(object sender, EventArgs e) { using (var db = new CoursemoEntities()) { //remove from students classes var clases = from s in db.StudentClasses select s; foreach (var p in clases) { db.StudentClasses.Remove(p); } //remove from studen waitlist var waitList = from s in db.StudentWaitLists select s; foreach (var p in waitList) { db.StudentWaitLists.Remove(p); } var query = from a in db.Classes select a; foreach (var p in query) { p.CurrentTotal = 0; } try { db.SaveChanges(); getStudentLists(); getWaitAndEnrolmentLists(); } catch (Exception ex) { Console.WriteLine(ex); clearGui(); } MessageBox.Show("DATABASE WAS RESET"); try { getWaitAndEnrolmentLists(); } catch (Exception ex) { Console.WriteLine(ex); } } }
private void button1_Click(object sender, EventArgs e) { //same students is sellected if (this.listBoxStudentX.SelectedIndex == this.listBoxStudentY.SelectedIndex) { MessageBox.Show("Select Two different Students !!!"); return; } //same class is selected try { if (this.listBoxCoursesX.SelectedItem.ToString() == this.listBoxCoursesY.SelectedItem.ToString()) { MessageBox.Show("Both Students are enrolled to this class! Swap Cancel !!!"); return; } }catch (Exception ex) { Console.WriteLine(ex); MessageBox.Show("No class selected !!! Select classes for both students!"); return; } //check student x if he is not in the class that other what to swapp foreach (var x in this.listBoxCoursesX.Items) { if (x.ToString() == this.listBoxCoursesY.SelectedItem.ToString()) { Console.WriteLine("oops"); MessageBox.Show("Illegal swap Student already has this class"); return; } } foreach (var y in this.listBoxCoursesY.Items) { if (y.ToString() == this.listBoxCoursesX.SelectedItem.ToString()) { Console.WriteLine("oops"); MessageBox.Show("Illegal swap Student already has this class"); return; } } string classX = this.listBoxCoursesX.SelectedItem.ToString(); string classY = this.listBoxCoursesY.SelectedItem.ToString(); string[] valuesX = classX.Split(' '); string[] valuesY = classY.Split(' '); int crnX = Convert.ToInt32(valuesX[3]); int crnY = Convert.ToInt32(valuesY[3]); Student sX = (Student)students[this.listBoxStudentX.SelectedIndex]; int stidX = sX.STID; Student sY = (Student)students[this.listBoxStudentY.SelectedIndex]; int stidY = sY.STID; int cidX = getCID(crnX); int cidY = getCID(crnY); //now we can perform transaction of swap using (var trans = db.Database.BeginTransaction()) { try { form1.delay(); int scid_X = getSCID(cidX, stidX); int countX = getCount(cidX, stidX, scid_X); if (countX != 1) { throw new System.InvalidOperationException("Aready on the list rollback"); } int scid_Y = getSCID(cidY, stidY); int countY = getCount(cidY, stidY, scid_Y); if (countY != 1) { throw new System.InvalidOperationException("Aready on the list rollback"); } //remove student x from class var x = from c in db.StudentClasses where c.CID == cidX && c.STID == stidX select c; foreach (var p in x) { p.STID = stidY; } //remove student y from class var y = from c in db.StudentClasses where c.CID == cidY && c.STID == stidY select c; foreach (var p in y) { p.STID = stidX; } //check if student is on the wait list after swap //if so remove from wait list after complete swap //do selection from left window var SCIDleft = from s in db.StudentWaitLists where s.CID == cidY && s.STID == stidX select s.SCID; int sc = -1; foreach (var p in SCIDleft) { sc = p; } var removeLeft = from sw in db.StudentWaitLists where sw.SCID == sc select sw; foreach (var a in removeLeft) { db.StudentWaitLists.Remove(a); } //check if student is on the wait list after swap //if so remove from wait list after complete swap //do selection from rigth window var SCIDrigth = from s in db.StudentWaitLists where s.CID == cidX && s.STID == stidY select s.SCID; int scrigth = -1; foreach (var p in SCIDrigth) { scrigth = p; } var removeRigth = from sw in db.StudentWaitLists where sw.SCID == scrigth select sw; foreach (var a in removeRigth) { db.StudentWaitLists.Remove(a); } db.SaveChanges(); trans.Commit(); } catch (Exception ex) { trans.Rollback(); Console.WriteLine(ex); MessageBox.Show("Rollback Swap Cancel"); } } //refresh list of classes getStudentClasses(this.listBoxStudentX, this.listBoxCoursesX); getStudentClasses(this.listBoxStudentY, this.listBoxCoursesY); }
//------------------------------------------------------------------------------------------------------- //withdraw from wait list using transaction private void button4_Click(object sender, EventArgs e) { if (this.listBoxWait.SelectedItem == null) { MessageBox.Show("Select Class you want to drop"); return; } string selectedClass = this.listBoxWait.SelectedItem.ToString(); int index = this.listBox1.SelectedIndex; Student student = (Student)students[index]; int stid = student.STID; string[] values = selectedClass.Split(' '); int crn = Convert.ToInt32(values[3]); int cid = -1; using (var db = new CoursemoEntities()) { //begin transaction using (var trans = db.Database.BeginTransaction()) { try { delay(); var xyz = (from h in db.StudentWaitLists join i in db.Classes on h.CID equals i.CID join s in db.Students on h.STID equals s.STID where i.CRN == crn && h.STID == stid select h).Count(); Console.WriteLine("to jest drop wait x: {0}", xyz); if (xyz == 0) { throw new System.InvalidOperationException("Aready on the list rollback"); } var query = from a in db.Classes where a.CRN == crn select a; foreach (var p in query) { cid = p.CID; } var q = from c in db.StudentWaitLists where c.CID == cid && c.STID == stid select c; foreach (var p in q) { db.StudentWaitLists.Remove(p); } db.SaveChanges(); trans.Commit(); } catch (Exception ex) { trans.Rollback(); Console.WriteLine(ex); MessageBox.Show("Rollback"); } try { getStudentLists(); getWaitAndEnrolmentLists(); clearTXTboxes(); } catch (Exception ex) { Console.WriteLine(ex); } } } }
//------------------------------------------------------------------------------------------------------- private void button3_Click(object sender, EventArgs e) { if (this.listBoxRegitered.SelectedItem == null) { MessageBox.Show("Select Class you want to drop"); return; } string selectedClass = this.listBoxRegitered.SelectedItem.ToString(); int index = this.listBox1.SelectedIndex; Student student = (Student)students[index]; int stid = student.STID; string[] values = selectedClass.Split(' '); int crn = Convert.ToInt32(values[3]); int cid = -1; using (var db = new CoursemoEntities()) { //begin transaction using (var trans = db.Database.BeginTransaction()) { try { delay(); //check if registerd on this class for multi-user operation var xyz = (from h in db.StudentClasses join i in db.Classes on h.CID equals i.CID join s in db.Students on h.STID equals s.STID where i.CRN == crn && h.STID == stid select h).Count(); Console.WriteLine("to jest drop x: {0}", xyz); //if its not on the list throw exception and rollback transaction if (xyz == 0) { throw new System.InvalidOperationException("Aready on the list rollback"); } //search class by crn var query = from a in db.Classes where a.CRN == crn select a; //update current total -1 and get course id foreach (var p in query) { p.CurrentTotal = p.CurrentTotal - 1; cid = p.CID; } //find student in sthe classes registered and remove var q = from c in db.StudentClasses where c.CID == cid && c.STID == stid select c; foreach (var p in q) { db.StudentClasses.Remove(p); } //now find if someone on the wait list if so remove from waitlist //and add to registered var x = -1; try { x = (from s in db.StudentWaitLists where s.CID == cid select s).Min(c => c.SCID); } catch (Exception ex) { Console.WriteLine(ex); x = -1; } //if x =-1 no one on the wait list ignore and coninue if (x == -1) { Console.WriteLine("wait list is empty "); } //else create new student and put on the list else { var h = from s in db.StudentWaitLists where s.SCID == x select new { STID = s.STID, CID = s.CID, YEARS = s.YEARS }; StudentClass studentClass = new StudentClass(); foreach (var i in h) { studentClass.YEARS = i.YEARS; studentClass.CID = i.CID; studentClass.STID = i.STID; } //remove from waitlist var z = from a in db.StudentWaitLists where a.SCID == x select a; foreach (var i in z) { db.StudentWaitLists.Remove(i); } //update classes curent enrolment var o = from i in db.Classes where i.CID == cid select i; foreach (var i in o) { i.CurrentTotal = i.CurrentTotal + 1; } db.StudentClasses.Add(studentClass); } db.SaveChanges(); trans.Commit(); } catch (Exception ex) { trans.Rollback(); MessageBox.Show("Rollback"); Console.WriteLine(ex); } } } //try catch if seleted item is null try { clearTXTboxes(); getStudentLists(); getWaitAndEnrolmentLists(); } catch (Exception ex) { Console.WriteLine(ex); } }
//------------------------------------------------------------------------------------------------------- //register for a course using transaction if fail then will rollback private void button2_Click(object sender, EventArgs e) { if (this.listBox1.SelectedItem == null) { MessageBox.Show("Select Student"); return; } if (this.listBoxAvailableCourses.SelectedItem == null) { MessageBox.Show("Select Class from available courses."); return; } using (var db = new CoursemoEntities()) { using (var trans = db.Database.BeginTransaction()) { ArrayList regiteredClasses = new ArrayList(); ArrayList waitList = new ArrayList(); delay(); string selectedClass = this.listBoxAvailableCourses.SelectedItem.ToString(); int index = this.listBox1.SelectedIndex; int i = this.listBoxAvailableCourses.SelectedIndex; Student student = (Student)students[index]; Class myClass = (Class)classes[i]; StudentClass studentClass = new StudentClass(); studentClass.YEARS = myClass.Years; studentClass.STID = student.STID; studentClass.CID = myClass.CID; StudentWaitList studentWaitList = new StudentWaitList(); studentWaitList.YEARS = myClass.Years; studentWaitList.STID = student.STID; studentWaitList.CID = myClass.CID; string[] values = selectedClass.Split(' '); int crn = Convert.ToInt32(values[3]); var x = (from c in db.Classes join s in db.StudentClasses on c.CID equals s.CID where c.CRN == crn && s.STID == student.STID select c).Count(); var y = (from c in db.Classes join s in db.StudentWaitLists on c.CID equals s.CID where c.CRN == crn && s.STID == student.STID select c).Count(); Console.WriteLine("this is x: {0}", x); Console.WriteLine("this is y: {0}", y); try { //check is student is on the wait list or class registerd list //if is on either one cancel transaction if (x > 0 || y > 0) { MessageBox.Show("You are on the list"); throw new System.InvalidOperationException("Aready on the list rollback"); } //update classes total curretn var query = from a in db.Classes where a.CID == studentClass.CID select a; foreach (var p in query) { //if current enrolled equals capacity put student on the wait list if (p.CurrentTotal == p.Capcity) { db.StudentWaitLists.Add(studentWaitList); } //else put on the registered list and update db else { p.CurrentTotal = p.CurrentTotal + 1; db.StudentClasses.Add(studentClass); } } db.SaveChanges(); db.Database.CurrentTransaction.Commit(); } catch (Exception ex) { trans.Rollback(); MessageBox.Show("rollback"); Console.WriteLine(ex); } } } //update gui getStudentLists(); getWaitAndEnrolmentLists(); selectCourse(this.listBoxAvailableCourses.Text); }