/** * <summary> * This event handler deletes a student from the db using EF * </summary> * * @method GamesGridView_RowDeleting * @param {object} sender * @param {GridViewDeleteEventArgs} e * @returns {void} */ protected void GamesGridView_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(AllGamesGridView.DataKeys[selectedRow].Values["gameID"]); // use EF to find the selected student in the DB and remove it using (DefaultContent db = new DefaultContent()) { // create object of the Student class and store the query string inside of it baseballgametracker deletedGame = (from gameRecords in db.baseballgametrackers where gameRecords.gameID == gameID select gameRecords).FirstOrDefault(); // remove the selected game from the db db.baseballgametrackers.Remove(deletedGame); // 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 (DefaultContent db = new DefaultContent()) { baseballgametracker newGame = new baseballgametracker(); 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.baseballgametrackers where game.gameID == gameID select game).FirstOrDefault(); } // add form data to the new game newGame.homeTeamName = homeTeamName.Text; newGame.awayTeamName = awayTeamName.Text; newGame.homeScore = Convert.ToInt32(homeScore.Text); newGame.awayScore = Convert.ToInt32(awayScore.Text); newGame.description = description.Text; newGame.gameDate = Convert.ToDateTime(gameDateTextBox.Text); newGame.spectators = spectators.Text; // use LINQ to ADO.NET to add / insert new student into the database if (gameID == 0) { db.baseballgametrackers.Add(newGame); } // save our changes - also updates and inserts db.SaveChanges(); // Redirect back to the updated games view page Response.Redirect("~/ViewGames.aspx"); } }