//------------------------------------------------------------------------------------------------------- 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); }