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