/**
         * <summary>
         * This event handler deletes a game 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(GamesGridView.DataKeys[selectedRow].Values["GameID"]);

            // use EF to find the selected game in the DB and remove it
            using (GTConnection db = new GTConnection())
            {
                // create object of the Student 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.GetGames(week);
            }
        }
Exemple #2
0
        /**
         * <summary>
         * This method gets the input from GridView and save/updated the Game object
         * into the DB
         * </summary>
         *
         * @return {void}
         * @param {object}sender
         * @param {EventArgs}e
         */
        protected void SaveButton_Click(object sender, EventArgs e)
        {
            //Use EF to connect to  the server
            using (GTConnection db = new GTConnection())
            {
                //save the information to the database
                //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 student from EF DB
                    newGame = (from game in db.Games
                               where game.GameID == GameID
                               select game).FirstOrDefault();
                }

                newGame.Week               = Convert.ToInt32(WeekDropDownList.SelectedValue);
                newGame.GameName           = GameNameTextBox.Text;
                newGame.GameDescription    = GameDescriptionTextBox.Text;
                newGame.Team1ID            = Convert.ToInt32(Team1DropDownList.SelectedValue);
                newGame.Team2ID            = Convert.ToInt32(Team2DropDownList.SelectedValue);
                newGame.Team1Score         = Convert.ToInt32(Team1ScoreTextBox.Text);
                newGame.Team2Score         = Convert.ToInt32(Team2ScoreTextBox.Text);
                newGame.NumberOfSpectators = Convert.ToInt32(NumberOfSpectatorsTextBox.Text);

                //add the game object to
                if (GameID == 0)
                {
                    db.Games.Add(newGame);
                }

                //save changes
                db.SaveChanges();

                //Redirect back to the updated students page
                Response.Redirect("~/Games.aspx");
            }
        }