/** * <summary> * On Delete * </summary> * @method GamesGridView_RowDeleting * @return {void} * */ protected void GamesGridView_RowDeleting(object sender, GridViewDeleteEventArgs e) { // Store which row was clicked int selectedRow = e.RowIndex; // Get the selected DepartmentID using the grids datakey collection int GameID = Convert.ToInt32(GamesGridView.DataKeys[selectedRow].Values["GameID"]); // Use ef to find the selelcted Department and delete it using (TrackingConnection db = new TrackingConnection()) { // Create object of the department 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 department from the db db.Games.Remove(deletedGame); // Save db changes db.SaveChanges(); // Refresh gridview this.GetGames(); } }
protected void SaveButton_Click(object sender, EventArgs e) { // Use EF to connect to the server using (TrackingConnection db = new TrackingConnection()) { // Use the games model to create a new games object and save a new record Game newGame = new Game(); int GameID = 0; if (Request.QueryString.Count > 0) // URL has a DepartmentID in it { // Get the id from the URL GameID = Convert.ToInt32(Request.QueryString["GameID"]); // Get the current games from EF DB newGame = (from Game in db.Games where Game.GameID == GameID select Game).FirstOrDefault(); } // Add data to the new games record newGame.GameName = "HearthStone Duel"; newGame.Player1 = Player1TextBox.Text; newGame.Player2 = Player2TextBox.Text; newGame.Spectators = Convert.ToInt32(SpectatorsTextBox.Text); newGame.Description = DescriptionTextArea.Text; newGame.WinningPlayer = WinningPlayerTextBox.Text; newGame.Created = Convert.ToDateTime(DuelDateTextBox.Text); // Use LINQ to ADO.NET to add or insert new games into the database if (GameID == 0) { db.Games.Add(newGame); } // Save our changes db.SaveChanges(); if (HttpContext.Current.User.Identity.GetUserName() == "admin") { // Redirect back to the updated games page Response.Redirect("~/Admin/AdminGameList.aspx"); } else { // Redirect back to the updated games page Response.Redirect("~/GameList.aspx"); } } }