protected void EditPlayHistory()
        {
            //Get the selected courseID from the url
                Int32 PlayboardID = Convert.ToInt32(Request.QueryString["PlayboardID"]);

                using (DefaultConnection db = new DefaultConnection())
                {
                    //query the db
                    Playboard objP = (from p in db.Playboard
                                      where p.PlayboardID == PlayboardID
                                      select p).FirstOrDefault();

                    //populate the top heading title which is located in the Site1.Master page so that the name of the reader will be in the top in bold
                    //((Site1)Master).SetTitle(objR.first_name + " " + objR.last_name);

                    if (objP != null)
                    {
                        //populate the form
                        txtSandboxDate.Text = string.Format("{0:yyyy-MM-dd}", objP.sandbox_date);
                        txtSandboxLandedOn.Text = objP.sandbox_landed_on;

                        txtTreehouseDate.Text = string.Format("{0:yyyy-MM-dd}", objP.treehouse_date);
                        txtTreehouseLandedOn.Text = objP.treehouse_landed_on;

                        txtCastleDate.Text = string.Format("{0:yyyy-MM-dd}", objP.castle_date);
                        txtCastleLandedOn.Text = objP.castle_landed_on;

                        txtTreasureDate.Text = string.Format("{0:yyyy-MM-dd}", objP.treasure_date);
                        txtTreasureLandedOn.Text = objP.treasure_landed_on;

                        txtWin.Text = string.Format("{0:yyyy-MM-dd}", objP.win_free_book);
                    }
                }
        }
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            //connect
            using (DefaultConnection db = new DefaultConnection())
            {
                //create instructor object
                Staff objS = new Staff();

                //first get salt value for this username
                String username = txtUsername.Text;

                objS = (from s in db.Staff
                        where s.username == username
                        select s).FirstOrDefault();

                //did we find this username?
                if (objS != null)
                {
                    String salt = objS.salt;

                    //salt and hash the plain text password
                    String password = txtPassword.Text;

                    String pass_and_salt = password + salt;

                    // Create a new instance of the hash crypto service provider.
                    HashAlgorithm hashAlg = new SHA256CryptoServiceProvider();

                    // Convert the data to hash to an array of Bytes.
                    byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(pass_and_salt);

                    // Compute the Hash. This returns an array of Bytes.
                    byte[] bytHash = hashAlg.ComputeHash(bytValue);

                    // Optionally, represent the hash value as a base64-encoded string,
                    // For example, if you need to display the value or transmit it over a network.
                    string base64 = Convert.ToBase64String(bytHash);

                    //check if the password we just salted and hashed matches the password in the db
                    if (objS.hashed == base64)
                    {
                        //lblError.Text = "Valid Login";
                        //store the identity in the session object
                        Session["AdminID"] = objS.AdminID;

                        //redirect to departments page
                        Response.Redirect("welcome.aspx");
                    }
                    else
                    {
                        lblError.Text = "Invalid Login";
                    }
                }
                else
                {
                    lblError.Text = "Invalid Login";
                }
            }
        }
        protected void GetReaders()
        {
            // populate the grid of readers.  select all.
            using (DefaultConnection db = new DefaultConnection())
            {
                var rdr = from r in db.Reader
                          orderby r.last_name
                               select r;

                grdReaders.DataSource = rdr.ToList();
                grdReaders.DataBind();
            }
        }
        protected void btnRegister_Click(object sender, EventArgs e)
        {
            //connect
            using (DefaultConnection db = new DefaultConnection())
            {
                //create a new instructor
                Staff objI = new Staff();

                //fill the properties from the form inputs
                objI.username = txtUsername.Text;
                // will store objI.hashed later once password is encrypted

                //salt and hash the plain text password
                String password = txtPassword.Text;
                String salt = CreateSalt(8);
                String pass_and_salt = password + salt;

                // Create a new instance of the hash crypto service provider.
                HashAlgorithm hashAlg = new SHA256CryptoServiceProvider();

                // Convert the data to hash to an array of Bytes.
                byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(pass_and_salt);

                // Compute the Hash. This returns an array of Bytes.
                byte[] bytHash = hashAlg.ComputeHash(bytValue);

                // Optionally, represent the hash value as a base64-encoded string,
                // For example, if you need to display the value or transmit it over a network.
                string base64 = Convert.ToBase64String(bytHash);

                objI.hashed = base64;
                objI.salt = salt;

                //save
                db.Staff.Add(objI);
                db.SaveChanges();

                //redirect
                Response.Redirect("login.aspx");
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            using (DefaultConnection db = new DefaultConnection())
            {

                //Get the selected courseID from the url
                Int32 NumberOfReaders;
                Int32 NumberOfBooksWon;

                //query the db
                NumberOfReaders = (from r in db.Reader
                               select r).Count();

                NumberOfBooksWon = (from r in db.Playboard
                                    where r.win_free_book != null
                                    select r).Count();

                //populate the stats page
                lblNumReaders.Text = NumberOfReaders.ToString();
                lblNumBooksWon.Text = NumberOfBooksWon.ToString();
            }
        }
        protected void btnPlayMapEdited_Click(object sender, EventArgs e)
        {
            //use EF to connect to SQL Server
            using (DefaultConnection db = new DefaultConnection())
            {

                //use the Playboard model to save the new record
                Playboard p = new Playboard();
                Int32 PlayboardID = 0;
                Int32 ReaderID = 0;

                //check the querystring for an id so we can determine add / update
                if (Request.QueryString["PlayboardID"] != null)
                {
                    //get the id from the url
                    PlayboardID = Convert.ToInt32(Request.QueryString["PlayboardID"]);

                    //get the current student from EF
                    p = (from objP in db.Playboard
                         where objP.PlayboardID == PlayboardID
                         select objP).FirstOrDefault();
                }

                if (!string.IsNullOrWhiteSpace(txtSandboxDate.Text))
                    p.sandbox_date = (Convert.ToDateTime(txtSandboxDate.Text));

                if (!string.IsNullOrWhiteSpace(txtSandboxLandedOn.Text))
                    p.sandbox_landed_on = txtSandboxLandedOn.Text;

                if (!string.IsNullOrWhiteSpace(txtTreasureDate.Text))
                    p.treehouse_date = (Convert.ToDateTime(txtTreehouseDate.Text));

                if (!string.IsNullOrWhiteSpace(txtTreehouseLandedOn.Text))
                    p.treehouse_landed_on = txtTreehouseLandedOn.Text;

                if (!string.IsNullOrWhiteSpace(txtCastleDate.Text))
                    p.castle_date = (Convert.ToDateTime(txtCastleDate.Text));

                if (!string.IsNullOrWhiteSpace(txtCastleLandedOn.Text))
                    p.castle_landed_on = txtCastleLandedOn.Text;

                if (!string.IsNullOrWhiteSpace(txtTreasureDate.Text))
                    p.treasure_date = (Convert.ToDateTime(txtTreasureDate.Text));

                if (!string.IsNullOrWhiteSpace(txtTreasureLandedOn.Text))
                    p.treasure_landed_on = txtTreasureLandedOn.Text;

                //call add only if we have no Playboard ID
                if (PlayboardID == 0)
                {
                    db.Playboard.Add(p);
                }

                //run the update or insert
                db.SaveChanges();

                //redirect to the updated students page
                ReaderID = p.ReaderID;
                Response.Redirect("editreader.aspx?ReaderID=" + ReaderID);
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            //use EF to connect to SQL Server
            using (DefaultConnection db = new DefaultConnection())
            {

                //use the Student model to save the new record
                Reader rdr = new Reader();
                Int32 ReaderID = 0;

                //check the querystring for an id so we can determine if add or update
                if (Request.QueryString["ReaderID"] != null)
                {
                    //get the id from the url
                    ReaderID = Convert.ToInt32(Request.QueryString["ReaderID"]);

                    //get the current student from EF
                    rdr = (from r in db.Reader
                         where r.ReaderID == ReaderID
                         select r).FirstOrDefault();
                }

                rdr.first_name = txtFirstName.Text;
                rdr.last_name = txtLastName.Text;
                rdr.school_name = txtSchoolName.Text;
                rdr.age = txtAge.Text;
                rdr.phone = txtPhone.Text;

                //call add only if we have no student ID
                if (ReaderID == 0)
                {
                    db.Reader.Add(rdr);

                    //run the insert only (not update)
                    db.SaveChanges();
                    // now: Reader.Id > 0
                    ReaderID = rdr.ReaderID;

                }
                else
                {

                    //run the update only (not insert)
                    db.SaveChanges();
                }

                //redirect to the updated students page
                Response.Redirect("editreader.aspx?ReaderID=" + ReaderID);
            }
        }
        protected void grdPlayHistory_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //store which row was clicked
            Int32 selectedRow = e.RowIndex;

            //get the selected StudentID using the grid's Data Key collection
            Int32 PlayboardID = Convert.ToInt32(grdPlayHistory.DataKeys[selectedRow].Values["PlayboardID"]);

            //use EF to remove the selected student from the db
            using (DefaultConnection db = new DefaultConnection())
            {

                Playboard pb = (from p in db.Playboard
                             where p.PlayboardID == PlayboardID
                             select p).FirstOrDefault();

                //do the delete
                db.Playboard.Remove(pb);
                db.SaveChanges();
            }

            Int32 ReaderID = 0;

            //get the id from the url
            ReaderID = Convert.ToInt32(Request.QueryString["ReaderID"]);

            //redirect
            Response.Redirect("editreader.aspx?ReaderID=" + ReaderID);
        }
        protected void GetReader()
        {
            using (DefaultConnection db = new DefaultConnection())
            {

                   //Get the selected courseID from the url
                Int32 ReaderID = Convert.ToInt32(Request.QueryString["ReaderID"]);

                //query the db
                Reader objR = (from r in db.Reader
                               where r.ReaderID == ReaderID
                               select r).FirstOrDefault();

                //populate the top heading title which is located in the Site1.Master page so that the name of the reader will be in the top in bold
                ((Site1)Master).SetTitle(objR.first_name + " " + objR.last_name);

                //populate the form
                txtFirstName.Text = objR.first_name;
                txtLastName.Text = objR.last_name;
                txtSchoolName.Text = objR.school_name;
                txtAge.Text = objR.age;                     //objr.age.ToString();   use this if datatype = number, not string
                txtPhone.Text = objR.phone;

            }
        }
        protected void GetPlayHistory()
        {
            Int32 ReaderID = Convert.ToInt32(Request.QueryString["ReaderID"]);

            using (DefaultConnection db = new DefaultConnection())
            {
                var Playboard = from pb in db.Playboard
                                where pb.ReaderID == ReaderID
                                select pb;

                grdPlayHistory.DataSource = Playboard.ToList();
                grdPlayHistory.DataBind();
            }
        }
        protected void btnPlay_Click(object sender, EventArgs e)
        {
            //use EF to connect to SQL Server
            using (DefaultConnection db = new DefaultConnection())
            {

                Int32 ReaderID = 0;
                //get the id from the url
                ReaderID = Convert.ToInt32(Request.QueryString["ReaderID"]);

                // flag to determine whether to create a new Playboard record
                bool isWon = false;

                //check the querystring for an id so we can determine if add or update
                if (Request.QueryString["ReaderID"] != null)
                {
                    //get PlayboardId if it exists
                    Playboard objP = (from p in db.Playboard
                                   where p.ReaderID == ReaderID
                                   orderby p.PlayboardID descending
                                   select p).FirstOrDefault();

                    // determine if existing Playboard record exists. if not we need to create a new object.
                    if (objP == null || objP.win_free_book != null)
                    {
                        objP = new Playboard();
                        objP.ReaderID = ReaderID;
                        db.Playboard.Add(objP);
                        db.SaveChanges();
                    }

                        // need to create a flag to determine whether or not to create a new Playboard record
                        // flag is true if current record already has data under win_free_book  field.

                        if (objP.win_free_book != null) {
                             isWon = true;
                        }

                        // get whatever radio button selection was pressed and dropdown dice number
                        if (rdoPlayMove.SelectedValue == "sandbox")
                        {
                            objP.sandbox_date = System.DateTime.Now;
                            objP.sandbox_landed_on = ddlPlayMove.SelectedValue;
                        }
                        else if (rdoPlayMove.SelectedValue == "treehouse")
                        {
                            objP.treehouse_date = System.DateTime.Now;
                            objP.treehouse_landed_on = ddlPlayMove.SelectedValue;
                        }
                        else if (rdoPlayMove.SelectedValue == "castle")
                        {
                            objP.castle_date = System.DateTime.Now;
                            objP.castle_landed_on = ddlPlayMove.SelectedValue;
                        }
                        else if (rdoPlayMove.SelectedValue == "treasurechest")
                        {
                            objP.treasure_date = System.DateTime.Now;
                            objP.treasure_landed_on = ddlPlayMove.SelectedValue;
                        }

                        // we're not playing for the first time because PlayboardID exists.
                        // But we still need to determine whether to add a new record or update the existing one.
                        // This all depends on whether the current record shows that the win_book field is populated
                        // If so, create new Playboard record; If not, just update existing Playboard record.
                        if (isWon) {
                            db.Playboard.Add(objP);
                        }

                    //run the update
                    db.SaveChanges();

                }

                //redirect to the updated students page
                Response.Redirect("editreader.aspx?ReaderID=" + ReaderID);
            }
        }
        protected void btnWin_Click(object sender, EventArgs e)
        {
            //use EF to connect to SQL Server
            using (DefaultConnection db = new DefaultConnection())
            {
                Int32 PlayboardID = 0;

                Int32 ReaderID = 0;
                //get the id from the url
                ReaderID = Convert.ToInt32(Request.QueryString["ReaderID"]);

                //check the querystring for an id so we can determine if add or update
                if (Request.QueryString["ReaderID"] != null)
                {
                    //get PlayboardId if it exists
                    Playboard objP = (from p in db.Playboard
                                      where p.ReaderID == ReaderID
                                      orderby p.ReaderID descending
                                      select p).First();

                    PlayboardID = objP.PlayboardID;

                    if (PlayboardID != 0)
                    {

                        //set date for win field since reader has read all 4 books and so wins a free book
                        objP.win_free_book = System.DateTime.Now;

                        //run the insert
                        db.SaveChanges();
                    }

                }

                //redirect to the updated students page
                Response.Redirect("editreader.aspx?ReaderID=" + ReaderID);
            }
        }