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