/** * <summary> * This method gets teams data from DB * </summary> * * @method GetTeams * @return {void} */ protected void GetTeams() { string sortString = Session["SortColumn"].ToString() + " " + Session["SortDirection"].ToString(); int selectedWeek = Convert.ToInt32(Session["SelectedWeek"].ToString()); // connect to EF using (GTConnection db = new GTConnection()) { //query the teams from table using EF and LINQ var teams1 = (from allTeams in db.Teams join allGames in db.Games on allTeams.TeamID equals allGames.Team1ID where allGames.Week == selectedWeek select new { TeamID = allTeams.TeamID, TeamName = allTeams.TeamName, TeamDescription = allTeams.TeamDescription, PointsScored = allGames.Team1Score, PointsLost = allGames.Team2Score }); var teams2 = (from allTeams in db.Teams join allGames in db.Games on allTeams.TeamID equals allGames.Team2ID where allGames.Week == selectedWeek select new { TeamID = allTeams.TeamID, TeamName = allTeams.TeamName, TeamDescription = allTeams.TeamDescription, PointsScored = allGames.Team2Score, PointsLost = allGames.Team1Score }); //combine queries results var teams = teams1.Concat(teams2); //bind the results to GridView TeamsGridView.DataSource = teams.AsQueryable().OrderBy(sortString).ToList(); TeamsGridView.DataBind(); } }
/** * <summary> * This method gets the game data from the DB * </summary> * * @method GetGames * @param {int} week * @returns {void} */ protected void GetGames(int week) { string sortString = Session["SortColumn"].ToString() + " " + Session["SortDirection"].ToString(); //connect to EF using (GTConnection db = new GTConnection()) { //query the Games Table using EF and LINQ var Games = (from allGames in db.Games join allTeams in db.Teams on allGames.WinningID equals allTeams.TeamID where allGames.Week == week select new { allGames.GameID, allGames.Week, allGames.GameName, allGames.GameDescription, allGames.NumberOfSpectators, TotalScore = allGames.Team1Score + allGames.Team2Score, Winner = allTeams.TeamName }); //bind the result to the GridView GamesGridView.DataSource = Games.AsQueryable().OrderBy(sortString).ToList(); GamesGridView.DataBind(); } }
/** * <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); } }
/** * <summary> * This method gets the game data from DB and displays in the GridView * </summary> * * @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 (GTConnection db = new GTConnection()) { // populate a game object instance with the GameID 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) { WeekDropDownList.SelectedValue = updatedGame.Week.ToString(); GameNameTextBox.Text = updatedGame.GameName; GameDescriptionTextBox.Text = updatedGame.GameDescription; Team1DropDownList.SelectedValue = updatedGame.Team1ID.ToString(); Team1ScoreTextBox.Text = updatedGame.Team1Score.ToString(); Team2DropDownList.SelectedValue = updatedGame.Team2ID.ToString(); Team2ScoreTextBox.Text = updatedGame.Team2Score.ToString(); NumberOfSpectatorsTextBox.Text = updatedGame.NumberOfSpectators.ToString(); } } }
/** * <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"); } }