コード例 #1
0
        public ActionResult Score()
        {
            if ((Session["User"] == null) || ((bool)Session["Finshed"] == false)) //Checks that a User is logged in and All the questions have been answered.
            {
                return(Redirect("../Home/Index"));                                //return Redirect used instead of return View because Return View simply displays the View without calling the relevant controller method causing the page to be static.
            }
            else
            {
                QuestionDB        questions = (QuestionDB)Session["Questions"]; //The QuestionDB instance is set from the Session Questions
                QuizDBDataContext db        = new QuizDBDataContext();          //Creates a instance of the Database
                tblResult         result    = new tblResult                     //Creates a new instance of tblResult to be saved into the Database
                {
                    PlayerID       = (int)Session["UserID"],                    //Sets the PlayerID from the ID stored in the Session
                    Score          = questions.QuestionRoundScore(),            //Sets the Score for the round as well as the current DateTime stamp
                    ResultDateTime = DateTime.Now
                };

                db.tblResults.InsertOnSubmit(result);
                try     //Try catch used to submit the changes to the Database
                {
                    db.SubmitChanges();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }

                ViewBag.TotalScore = questions.TotalScore();

                return(View("Score"));
            }
        }
コード例 #2
0
        public ActionResult Register(string Username, string passWord)
        {
            QuizDBDataContext db = new QuizDBDataContext(); //Creates a instance of the Database

            tblPlayer player = new tblPlayer                //Creates a new instance of tblPlayer to be added to the DataBase
            {
                PlayerName     = Username,
                PlayerPassword = passWord
            };

            db.tblPlayers.InsertOnSubmit(player);

            try                 //Try catch statement used to save the new User into the DataBase
            {
                db.SubmitChanges();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);       //Writes the Error to the console
            }
            return(Redirect("Login"));      //return Redirect used instead of return View because Return View simply displays the View without calling the relevant controller method causing the page to be static.
        }