protected void GetGames() { // populate teh form with existing data from the database int GameID = Convert.ToInt32(Request.QueryString["ID"]); // connect to the EF DB using (ProjectConnection db = new ProjectConnection()) { // populate a game object instance with the gameID from the URL Parameter MAIN_GAME updatedGame = (from game in db.MAIN_GAME where game.GAME_NAME == ID select game).FirstOrDefault(); // map the game properties to the form controls if (updatedGame != null) { GameNameTextBox.Text = updatedGame.GAME_NAME; DescriptionTextBox.Text = updatedGame.DESCRIPTION; TeamATextBox.Text = updatedGame.TEAM_A; TeamBTextBox.Text = updatedGame.TEAM_B; TotalPointsTextBox.Text = updatedGame.TOTAL_POINTS.ToString(); SpectatorsTextBox.Text = updatedGame.SPECTATORS.ToString(); WinningTeamTextBox.Text = updatedGame.WINNING_TEAM; } } }
/** * <summary> * This event handler deletes a student from the db using EF * </summary> * * @method StudentsGridView_RowDeleting * @param {object} sender * @param {GridViewDeleteEventArgs} e * @returns {void} */ protected void GameGridView_RowDeleting(object sender, GridViewDeleteEventArgs e) { // store which row was clicked int selectedRow = e.RowIndex; // get the selected StudentID using the Grid's DataKey collection int gameID = Convert.ToInt32(GameGridView.DataKeys[selectedRow].Values["ID"]); // use EF to find the selected student in the DB and remove it using (ProjectConnection db = new ProjectConnection()) { // create object of the Student class and store the query string inside of it MAIN_GAME deletedStudent = (from gameRecords in db.MAIN_GAME where gameRecords.ID == gameID select gameRecords).FirstOrDefault(); // remove the selected student from the db db.MAIN_GAME.Remove(deletedStudent); // save my changes back to the database db.SaveChanges(); // refresh the grid this.GetGames(); } }
protected void SaveButton_Click(object sender, EventArgs e) { // Use EF to connect to the server using (ProjectConnection db = new ProjectConnection()) { // use the game model to create a new game object and // save a new record MAIN_GAME newGame = new MAIN_GAME(); int GameID = 0; if (Request.QueryString.Count > 0) // our URL has a GameID in it { // get the id from the URL GameID = Convert.ToInt32(Request.QueryString["ID"]); // get the current Game from EF DB newGame = (from game in db.MAIN_GAME where game.ID == GameID select game).FirstOrDefault(); } // add form data to the new Game record newGame.GAME_NAME = GameNameTextBox.Text; newGame.DESCRIPTION = DescriptionTextBox.Text; newGame.TEAM_A = TeamATextBox.Text; newGame.TEAM_B = TeamBTextBox.Text; newGame.TOTAL_POINTS = Convert.ToInt32(TotalPointsTextBox.Text); newGame.SPECTATORS = Convert.ToInt32(SpectatorsTextBox.Text); newGame.WINNING_TEAM = WinningTeamTextBox.Text; // use LINQ to ADO.NET to add / insert new Game into the database if (GameID == 0) { db.MAIN_GAME.Add(newGame); } // save our changes - also updates and inserts db.SaveChanges(); // Redirect back to the updated Game page Response.Redirect("~/Admin/Game3.aspx"); } }