Exemple #1
0
        private void cmdDrop2_Click(object sender, EventArgs e)
        {
            if (lstStuCourses2.SelectedIndex > -1)
            {
                //Code for Delay
                int timeInMS;
                if (System.Int32.TryParse(this.txtDelay.Text, out timeInMS) == true)
                {
                    ;
                }
                else
                {
                    timeInMS = 0;
                }
                System.Threading.Thread.Sleep(timeInMS);

                int retries = 0;
                while (retries < 3)
                {
                    try
                    {
                        using (var transaction = db.Database.BeginTransaction())
                        {
                            Student stu    = lstStudents2.SelectedItem as Student;
                            Course  toDrop = lstStuCourses2.SelectedItem as Course;

                            if (toDrop == null)
                            {
                                string   courseString = lstStuCourses2.SelectedItem as String;
                                string[] val          = courseString.Split(' ');
                                //MessageBox.Show(val[2]);
                                int crnLookup = Convert.ToInt32(val[2]);
                                toDrop = db.Courses.Single <Course>(c => c.CRN == crnLookup);
                            }

                            if (db.StudentCourses.Any <StudentCourse>(sc => sc.NetID == stu.NetID && sc.CRN == toDrop.CRN))
                            {
                                var c = db.Courses.Find(toDrop.CRN);
                                c.NumEnrolled--;
                                db.StudentCourses.Remove(db.StudentCourses.Single <StudentCourse>(sc => sc.CRN == toDrop.CRN && stu.NetID == sc.NetID));
                                db.SaveChanges();
                                MessageBox.Show(stu.NetID + " has successfully dropped " + toDrop.Department + " " + toDrop.CourseNum + "!");
                                if (db.Waitlists.Any <Waitlist>(w => w.CRN == toDrop.CRN))
                                {
                                    Waitlist nextStudent  = db.Waitlists.First <Waitlist>(w => w.CRN == toDrop.CRN);
                                    var      stuCourseObj = new StudentCourse {
                                        CRN = nextStudent.CRN, NetID = nextStudent.NetID
                                    };
                                    db.StudentCourses.Add(stuCourseObj);
                                    c.NumEnrolled++;
                                    db.Waitlists.Remove(nextStudent);
                                    db.SaveChanges();
                                    MessageBox.Show(nextStudent.NetID + " has been enrolled in " + toDrop.Department + " " + toDrop.CourseNum + " from the waitlist!");
                                }
                            }

                            else if (db.Waitlists.Any <Waitlist>(w => w.NetID == stu.NetID && w.CRN == toDrop.CRN))
                            {
                                db.Waitlists.Remove(db.Waitlists.First <Waitlist>(w => w.CRN == toDrop.CRN && stu.NetID == w.NetID));
                                db.SaveChanges();
                                MessageBox.Show(stu.NetID + " has successfully ben removed from the waitlist for " + toDrop.Department + " " + Convert.ToString(toDrop.CourseNum) + "!");
                            }

                            else
                            {
                                MessageBox.Show("Error student is neither enrolled, nor on the waitlist for this course!");
                            }

                            transaction.Commit();
                            break;
                        }//end transaction
                    }
                    catch (SqlException ex)
                    {
                        if (ex.Number == 1205)
                        {
                            retries++;
                        }
                        else
                        {
                            MessageBox.Show(ex.Message);
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        break;
                    }
                    finally
                    {
                        cmdLoadCourses_Click(sender, e);
                        cmdLoadStudents_Click(sender, e);
                    }
                }
            }
            else if (lstStuCourses2.Items.Count == 0)
            {
                MessageBox.Show("You currently have no courses to drop!");
            }
            else
            {
                MessageBox.Show("Please select a course to add from the respective list of student courses.");
            }
        }
Exemple #2
0
        private void cmdEnroll2_Click(object sender, EventArgs e)
        {
            if (lstCourses.SelectedIndex > -1)
            {
                //Code for Delay
                int timeInMS;
                if (System.Int32.TryParse(this.txtDelay.Text, out timeInMS) == true)
                {
                    ;
                }
                else
                {
                    timeInMS = 0;
                }
                System.Threading.Thread.Sleep(timeInMS);

                int retries = 0;
                while (retries < 3)
                {
                    try
                    {
                        using (var transaction = db.Database.BeginTransaction())
                        {
                            //db = new SampleContext();
                            Course  cur = lstCourses.SelectedItem as Course;
                            Student stu = lstStudents2.SelectedItem as Student;
                            if (db.StudentCourses.Any <StudentCourse>(sc => sc.NetID == stu.NetID && sc.CRN == cur.CRN))
                            {
                                MessageBox.Show("Student is already enrolled in this course!");
                            }
                            else if (cur.NumEnrolled == cur.ClassSize)
                            {
                                //Add student to the waitlist
                                var waitlistObj = new Waitlist {
                                    CRN = cur.CRN, NetID = stu.NetID
                                };
                                db.Waitlists.Add(waitlistObj);
                                db.SaveChanges();
                                MessageBox.Show("This course is currently at capacity, you have been added to the Waitlist");
                            }
                            else
                            {
                                //Add student to the course enrollments.
                                string num          = Convert.ToString(cur.CourseNum);
                                var    stuCourseObj = new StudentCourse {
                                    CRN = cur.CRN, NetID = stu.NetID
                                };
                                db.StudentCourses.Add(stuCourseObj);
                                var c = db.Courses.Find(cur.CRN);
                                c.NumEnrolled++;
                                db.SaveChanges();
                                MessageBox.Show("You have been added to the course " + cur.Department + " " + num + "!");
                            }
                            transaction.Commit();
                            break;
                        }//end transaction
                    }
                    catch (SqlException ex)
                    {
                        if (ex.Number == 1205)
                        {
                            retries++;
                        }
                        else
                        {
                            MessageBox.Show(ex.Message);
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        break;
                    }
                    finally
                    {
                        cmdLoadCourses_Click(sender, e);
                        cmdLoadStudents_Click(sender, e);
                    }
                }
            }
            else
            {
                MessageBox.Show("Please select a course to add from the list at the left");
            }
        }