protected void GameGridView_RowDeleting(object sender, GridViewDeleteEventArgs e) { // store which row was clicked int selectedRow = e.RowIndex; // get the selected GameId using the Grid's DataKey collection int GameID = Convert.ToInt32(GameGridView.DataKeys[selectedRow].Values["GameID"]); // use EF to find the selected game in the DB and remove it using (VJKMConnection db = new VJKMConnection()) { // create object of the game class and store the query string inside of it Game deletedGame = (from GameRecords in db.Games where GameRecords.GameID == GameID select GameRecords).FirstOrDefault(); // remove the selected game from the db db.Games.Remove(deletedGame); // save my changes back to the database db.SaveChanges(); // refresh the grid this.ConnectionToDB(); } }
protected void SaveButton_Click(object sender, EventArgs e) { // Use EF to connect to the server using (VJKMConnection db = new VJKMConnection()) { // use the game model to create a new game object and // save a new record Game newGame = new 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["GameID"]); // get the current Game from EF DB newGame = (from game in db.Games where game.GameID == GameID select game).FirstOrDefault(); } // add form data to the new game record newGame.GameName = GameNameTextBox.Text; newGame.GameLocation = GLocationTextBox.Text; newGame.GameDescription = GameDesc.Text; newGame.TeamA = TeamA.Text; newGame.TeamB = TeamB.Text; newGame.TeamAScore = Convert.ToInt32(TeamAScore.Text); newGame.TeamBScore = Convert.ToInt32(TeamBScore.Text); newGame.Spectators = Convert.ToInt32(Spectators.Text); newGame.WinningTeam = WinningTeam.Text; newGame.GameDate = Convert.ToDateTime(GameDateTextBox.Text); // use LINQ to ADO.NET to add / insert new game into the database if (GameID == 0) { db.Games.Add(newGame); } // save our changes - also updates and inserts db.SaveChanges(); // Redirect back to the updated GameEditPage Response.Redirect("~/SecurePages/GameEditPage.aspx"); } }