/// <summary>
        /// Confirmation button click event handler. Removes or adds the course 
        /// from/to the student's courses. Redirects user to previous page afterwards.
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">event args</param>
        protected void ConfirmButton_Click(object sender, EventArgs e)
        {
            using (TablesDataContext db = new TablesDataContext())
            {
                if (action.Equals("add"))
                {
                    StudentCourse sc = new StudentCourse();
                    sc.CourseID = courseID;
                    sc.StudentID = (int)Session["StudentID"];
                    db.StudentCourses.InsertOnSubmit(sc);
                    SubmitChanges(db);
                }
                else if (action.Equals("remove"))
                {
                    var course = from sc in db.StudentCourses
                                 where (sc.CourseID.Equals(courseID)
                                 && sc.StudentID == (int)Session["StudentID"])
                                 select sc;
                    db.StudentCourses.DeleteOnSubmit(course.FirstOrDefault());
                    SubmitChanges(db);
                }
                else
                {
                    Response.Redirect("default.aspx");
                }

                Session["Action"] = null;
                Session["CourseID"] = null;
                Response.Redirect("courses.aspx");
            }
        }
        /// <summary>
        /// Confirmation button click event handler. Removes or adds the course
        /// from/to the student's courses. Redirects user to previous page afterwards.
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">event args</param>
        protected void ConfirmButton_Click(object sender, EventArgs e)
        {
            using (TablesDataContext db = new TablesDataContext())
            {
                if (action.Equals("add"))
                {
                    StudentCourse sc = new StudentCourse();
                    sc.CourseID  = courseID;
                    sc.StudentID = (int)Session["StudentID"];
                    db.StudentCourses.InsertOnSubmit(sc);
                    SubmitChanges(db);
                }
                else if (action.Equals("remove"))
                {
                    var course = from sc in db.StudentCourses
                                 where (sc.CourseID.Equals(courseID) &&
                                        sc.StudentID == (int)Session["StudentID"])
                                 select sc;
                    db.StudentCourses.DeleteOnSubmit(course.FirstOrDefault());
                    SubmitChanges(db);
                }
                else
                {
                    Response.Redirect("default.aspx");
                }

                Session["Action"]   = null;
                Session["CourseID"] = null;
                Response.Redirect("courses.aspx");
            }
        }
 /// <summary>
 /// Submits the changes to the datacontext.
 /// </summary>
 private void SubmitChanges(TablesDataContext db)
 {
     try
     {
         db.SubmitChanges();
     }
     catch (ChangeConflictException cce)
     {
         foreach (var conflict in db.ChangeConflicts)
         {
             conflict.Resolve(RefreshMode.KeepCurrentValues);
         }
         db.SubmitChanges();
     }
 }
        /// <summary>
        /// Loads the course lists and various labels.
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">event args</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            db = new TablesDataContext();
            if(!IsPostBack)
            {
                try
                {
                    if (Session["Email"] == null)
                    {
                        Response.Redirect("default.aspx");
                    }

                    var student = from s in db.Students
                                    where s.Email.Equals((String)Session["Email"])
                                    select s.StudentID;
                    if (student.Count() == 0)
                    {
                        //Create new entry.
                        Student newStudent = new Student();
                        newStudent.Email = (String)Session["Email"];
                        db.Students.InsertOnSubmit(newStudent);
                        SubmitChanges();
                        student = from s in db.Students
                                    where s.Email.Equals((String)Session["Email"])
                                    select s.StudentID;
                    }

                    Session["StudentID"] = student.FirstOrDefault();

                    emailLabel.Text += (String)Session["Email"];
                }
                catch (HttpException exc)
                {
                    Response.Write("<script type='text/javascript'>alert('An error has occured: " + exc.Message + "');</script>");
                }
                loadCourses();
            }
        }
示例#5
0
        /// <summary>
        /// Loads the course lists and various labels.
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">event args</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            db = new TablesDataContext();
            if (!IsPostBack)
            {
                try
                {
                    if (Session["Email"] == null)
                    {
                        Response.Redirect("default.aspx");
                    }

                    var student = from s in db.Students
                                  where s.Email.Equals((String)Session["Email"])
                                  select s.StudentID;
                    if (student.Count() == 0)
                    {
                        //Create new entry.
                        Student newStudent = new Student();
                        newStudent.Email = (String)Session["Email"];
                        db.Students.InsertOnSubmit(newStudent);
                        SubmitChanges();
                        student = from s in db.Students
                                  where s.Email.Equals((String)Session["Email"])
                                  select s.StudentID;
                    }

                    Session["StudentID"] = student.FirstOrDefault();

                    emailLabel.Text += (String)Session["Email"];
                }
                catch (HttpException exc)
                {
                    Response.Write("<script type='text/javascript'>alert('An error has occured: " + exc.Message + "');</script>");
                }
                loadCourses();
            }
        }
        /// <summary>
        /// Sets the confirmation information labels.
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">event args</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            action   = (String)Session["Action"];
            courseID = (String)Session["CourseID"];

            if (!IsPostBack)
            {
                if (action == null || courseID == null)
                {
                    Response.Redirect("default.aspx");
                }
                else
                {
                    ActionLabel.Text = action + " ";
                    if (action.Equals("add"))
                    {
                        ToFromLabel.Text = "to ";
                    }
                    else if (action.Equals("remove"))
                    {
                        ToFromLabel.Text = "from ";
                    }
                    else
                    {
                        Response.Redirect("default.aspx");
                    }
                    using (TablesDataContext db = new TablesDataContext())
                    {
                        var course = from c in db.Courses
                                     where c.CourseID.Equals(courseID)
                                     select c;
                        CourseTitle.InnerHtml = course.FirstOrDefault().CourseID + " "
                                                + course.FirstOrDefault().Title;
                        var courseTimes = from ct in db.CourseTimes
                                          where ct.CourseID.Equals(courseID)
                                          select ct;
                        String[] days = { "", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
                        String   start, end;
                        CourseTimesContent.InnerHtml = "";
                        foreach (var ct in courseTimes)
                        {
                            start = "" + ct.Start;
                            if (start.Length == 3)
                            {
                                start = start.Substring(0, 1) + ":" + start.Substring(1);
                            }
                            else
                            {
                                start = start.Substring(0, 2) + ":" + start.Substring(2);
                            }

                            end = "" + ct.End;
                            if (end.Length == 3)
                            {
                                end = end.Substring(0, 1) + ":" + end.Substring(1);
                            }
                            else
                            {
                                end = end.Substring(0, 2) + ":" + end.Substring(2);
                            }

                            CourseTimesContent.InnerHtml += days[(int)ct.DayOfWeek] + " from " +
                                                            start + " to " + end + "<br />";
                        }
                    }
                }
            }
        }
        /// <summary>
        /// This loads cancelled classes from a feed and sends emails to registered students.
        /// </summary>
        public static void LoadStream()
        {
            XElement cancel = XElement.Load(FEED);
            var test = from t in cancel.Descendants("item")
                       select t.Element("title").Value;
            if(!test.FirstOrDefault().Equals("No classes cancelled."))
            {
                IEnumerable<CancelledClass> classes = from c in cancel.Descendants("item")
                                                  select new CancelledClass(c.Element("title").Value,
                                                  c.Element("course").Value,
                                                  c.Element("datecancelled").Value,
                                                  c.Element("teacher").Value, c.Element("notes").Value);
                List<CancelledClass> newCancelled = new List<CancelledClass>();
                bool isNew = true;
                using (TablesDataContext db = new TablesDataContext())
                {
                    IEnumerable<CancelledClass> allCancelled = from c in db.CourseCancelleds
                                                               select new CancelledClass(c.CourseID, c.Title, c.DateCancelled, c.Teacher, c.Notes);
                    string previousCourseID = "";
                    string previousDateCancelled = "";

                    foreach (CancelledClass c in classes)
                    {
                        isNew = true;
                        if (c.CourseID.Equals(previousCourseID) && c.DateCancelled.Equals(previousDateCancelled))
                            isNew = false;
                        else
                            foreach (CancelledClass allC in allCancelled)
                            {
                                if(c.Equals(allC))
                                {
                                    isNew = false;
                                    break;
                                }
                            }
                        if (isNew)
                            newCancelled.Add(c);
                        previousCourseID = c.CourseID;
                        previousDateCancelled = c.DateCancelled;
                    }

                    //Add the new cancelled courses to the database
                    CourseCancelled course;
                    foreach (CancelledClass c in newCancelled)
                    {
                        course = new CourseCancelled();
                        course.CourseID = c.CourseID;
                        course.DateCancelled = c.DateCancelled;
                        course.Teacher = c.Teacher;
                        course.Notes = c.Notes;
                        course.Title = c.Title;
                        db.CourseCancelleds.InsertOnSubmit(course);

                        //Cycle through new cancellations and send emails.
                        var students = from s in db.StudentCourses
                                       where s.CourseID.Equals(c.CourseID)
                                       select s.StudentID;
                        foreach (int studentID in students)
                        {
                            //Select email, send email.
                            var listOfEmails = from s in db.Students
                                        where s.StudentID == studentID
                                        select s.Email;
                            string email = listOfEmails.FirstOrDefault();
                            sendEmail(email, c);
                        }
                    }
                    try
                    {
                        db.SubmitChanges();
                    }
                    catch (ChangeConflictException cce)
                    {
                        foreach (var conflict in db.ChangeConflicts)
                            conflict.Resolve(RefreshMode.KeepCurrentValues);
                        db.SubmitChanges();
                    }
                }//end of using
            }
        }
        /// <summary>
        /// Sets the confirmation information labels.
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">event args</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            action = (String)Session["Action"];
            courseID = (String)Session["CourseID"];

            if (!IsPostBack)
            {
                if (action == null || courseID == null)
                {
                    Response.Redirect("default.aspx");
                }
                else
                {
                    ActionLabel.Text = action + " ";
                    if (action.Equals("add"))
                    {
                        ToFromLabel.Text = "to ";
                    }
                    else if (action.Equals("remove"))
                    {
                        ToFromLabel.Text = "from ";
                    }
                    else
                    {
                        Response.Redirect("default.aspx");
                    }
                    using (TablesDataContext db = new TablesDataContext())
                    {
                        var course = from c in db.Courses
                                     where c.CourseID.Equals(courseID)
                                     select c;
                        CourseTitle.InnerHtml = course.FirstOrDefault().CourseID + " "
                                            + course.FirstOrDefault().Title;
                        var courseTimes = from ct in db.CourseTimes
                                          where ct.CourseID.Equals(courseID)
                                          select ct;
                        String[] days = { "","Monday", "Tuesday","Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
                        String start, end;
                        CourseTimesContent.InnerHtml = "";
                        foreach(var ct in courseTimes)
                        {
                            start = "" + ct.Start;
                            if (start.Length == 3)
                                start = start.Substring(0, 1) + ":" + start.Substring(1);
                            else
                                start = start.Substring(0, 2) + ":" + start.Substring(2);

                            end = "" + ct.End;
                            if (end.Length == 3)
                                end = end.Substring(0, 1) + ":" + end.Substring(1);
                            else
                                end = end.Substring(0, 2) + ":" + end.Substring(2);

                            CourseTimesContent.InnerHtml += days[(int)ct.DayOfWeek] + " from " +
                                                            start + " to " + end + "<br />";
                        }
                    }
                }
            }
        }
 /// <summary>
 /// Submits the changes to the datacontext.
 /// </summary>
 private void SubmitChanges(TablesDataContext db)
 {
     try
     {
         db.SubmitChanges();
     }
     catch (ChangeConflictException cce)
     {
         foreach (var conflict in db.ChangeConflicts)
             conflict.Resolve(RefreshMode.KeepCurrentValues);
         db.SubmitChanges();
     }
 }
        /// <summary>
        /// This loads cancelled classes from a feed and sends emails to registered students.
        /// </summary>
        public static void LoadStream()
        {
            XElement cancel = XElement.Load(FEED);
            var      test   = from t in cancel.Descendants("item")
                              select t.Element("title").Value;

            if (!test.FirstOrDefault().Equals("No classes cancelled."))
            {
                IEnumerable <CancelledClass> classes = from c in cancel.Descendants("item")
                                                       select new CancelledClass(c.Element("title").Value,
                                                                                 c.Element("course").Value,
                                                                                 c.Element("datecancelled").Value,
                                                                                 c.Element("teacher").Value, c.Element("notes").Value);
                List <CancelledClass> newCancelled = new List <CancelledClass>();
                bool isNew = true;
                using (TablesDataContext db = new TablesDataContext())
                {
                    IEnumerable <CancelledClass> allCancelled = from c in db.CourseCancelleds
                                                                select new CancelledClass(c.CourseID, c.Title, c.DateCancelled, c.Teacher, c.Notes);
                    string previousCourseID      = "";
                    string previousDateCancelled = "";

                    foreach (CancelledClass c in classes)
                    {
                        isNew = true;
                        if (c.CourseID.Equals(previousCourseID) && c.DateCancelled.Equals(previousDateCancelled))
                        {
                            isNew = false;
                        }
                        else
                        {
                            foreach (CancelledClass allC in allCancelled)
                            {
                                if (c.Equals(allC))
                                {
                                    isNew = false;
                                    break;
                                }
                            }
                        }
                        if (isNew)
                        {
                            newCancelled.Add(c);
                        }
                        previousCourseID      = c.CourseID;
                        previousDateCancelled = c.DateCancelled;
                    }

                    //Add the new cancelled courses to the database
                    CourseCancelled course;
                    foreach (CancelledClass c in newCancelled)
                    {
                        course               = new CourseCancelled();
                        course.CourseID      = c.CourseID;
                        course.DateCancelled = c.DateCancelled;
                        course.Teacher       = c.Teacher;
                        course.Notes         = c.Notes;
                        course.Title         = c.Title;
                        db.CourseCancelleds.InsertOnSubmit(course);

                        //Cycle through new cancellations and send emails.
                        var students = from s in db.StudentCourses
                                       where s.CourseID.Equals(c.CourseID)
                                       select s.StudentID;
                        foreach (int studentID in students)
                        {
                            //Select email, send email.
                            var listOfEmails = from s in db.Students
                                               where s.StudentID == studentID
                                               select s.Email;
                            string email = listOfEmails.FirstOrDefault();
                            sendEmail(email, c);
                        }
                    }
                    try
                    {
                        db.SubmitChanges();
                    }
                    catch (ChangeConflictException cce)
                    {
                        foreach (var conflict in db.ChangeConflicts)
                        {
                            conflict.Resolve(RefreshMode.KeepCurrentValues);
                        }
                        db.SubmitChanges();
                    }
                } //end of using
            }
        }         //end of method