protected void DeleteGame(object sender, GridViewDeleteEventArgs e, GridView Games)
        {
            int selectedRow = e.RowIndex;
            int gameId      = Convert.ToInt32(Games.DataKeys[selectedRow].Values["Id"]);

            if (Session["userName"] == null)
            {
                Response.Redirect("Login.aspx");
            }
            using (comp2007db db = new comp2007db())
            {
                game deletedGame = (from gameRecord in db.games
                                    where gameRecord.Id == gameId
                                    select gameRecord).FirstOrDefault();
                db.games.Remove(deletedGame);
                db.SaveChanges();
            }
        }
Beispiel #2
0
        protected void AddGameButton_Click(object sender, EventArgs e)
        {
            using (comp2007db db = new comp2007db())
            {
                // use the syudent model to save a new record
                game newGame = new game();

                int GameID = 0;

                if (Request.QueryString.Count > 0)
                {
                    // get game id from url
                    GameID = Convert.ToInt32(Request.QueryString["Id"]);
                    // get the current Game from the DB
                    newGame = (from game in db.games where game.Id == GameID select game).FirstOrDefault();
                }
                // add for data to new student record
                newGame.homeName    = homeNameTextBox.Text;
                newGame.awayName    = awayNameTextBox.Text;
                newGame.homePoints  = Convert.ToInt32(homePointsTextBox.Text);
                newGame.awayPoints  = Convert.ToInt32(awayPointsTextBox.Text);
                newGame.attendances = Convert.ToInt32(attendancesTextBox.Text);
                newGame.stadium     = stadiumTextBox.Text;
                newGame.sportType   = sportTypeTextBox.SelectedValue;
                newGame.datePlayed  = Convert.ToDateTime(datePlayedTextBox.Text);

                // add a new game to Games Table Collection
                if (GameID == 0)
                {
                    db.games.Add(newGame);
                }

                // run insert commands to database


                db.SaveChanges();


                Response.Redirect("~/Default.aspx");
            }
        }