/** * <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(); } }
/** * Gets the game to populate fields * relating to the current page * * @private * @method GetGame * @return {void} */ protected void GetGame() { // Populate the form with existing data from the database int GameID = Convert.ToInt32(Request.QueryString["GameID"]); // Connect to the EF DB using (TrackingConnection db = new TrackingConnection()) { // Populate a department object instance with the GametID from the URL Parameter Game updatedGame = (from Game in db.Games where Game.GameID == GameID select Game).FirstOrDefault(); // Map the Game properties to the form controls if (updatedGame != null) { Player1TextBox.Text = updatedGame.Player1; Player2TextBox.Text = updatedGame.Player2; SpectatorsTextBox.Text = updatedGame.Spectators.ToString(); DescriptionTextArea.Text = updatedGame.Description; WinningPlayerTextBox.Text = updatedGame.WinningPlayer; DuelDateTextBox.Text = updatedGame.Created.ToString(); } } }
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"); } } }
/** * <summary> * This method gets the game from the database and populates the fields * </summary> * @method GetGames * @return {void} * */ protected void GetGames() { // Connect to EF using (TrackingConnection db = new TrackingConnection()) { DateTime date1 = new DateTime(); DateTime date2 = new DateTime(); date1 = Convert.ToDateTime(TrackingWeekDropDown.SelectedValue); date2 = Convert.ToDateTime(TrackingWeekDropDown.Items[TrackingWeekDropDown.SelectedIndex + 1].Value); //query the Games table using EF and LINQ var Games = (from allGames in db.Games where allGames.Created >= date1.Date && allGames.Created < date2.Date select allGames); //bind results to gridview GamesGridView.DataSource = Games.AsQueryable().ToList(); GamesGridView.DataBind(); TrackingDateLabel.Text = date1.ToString("MMMM dd, yyyy") + " To " + date2.ToString("MMMM dd, yyyy"); } }