/** * <summary> * This method gets the Games from the DB depending on selected calendar week and handles edit functionality. * </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 (GameTrackConnection db = new GameTrackConnection()) { // 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) { GameNameTextBox.Text = updatedGame.GameName; TeamOneTextBox.Text = updatedGame.TeamOne; TeamOneScoreTextBox.Text = updatedGame.TeamOneScore; TeamTwoTextBox.Text = updatedGame.TeamTwo; TeamTwoScoreTextBox.Text = updatedGame.TeamTwoScore; GameResultTextBox.Text = updatedGame.GameResult; CalendarWeekTextBox.Text = updatedGame.CalendarWeek.ToString("yyyy-MM-dd"); } } }
/** * <summary> * This method gets the Games from the DB depending on selected calendar week. * </summary> * * @method GetGames * @return {void} */ protected void GetGames() { // connect to EF using (GameTrackConnection db = new GameTrackConnection()) { // query the games table using EF and LINQ var Games = (from allGames in db.Games where allGames.CalendarWeek == this.week select allGames); //Count SQL Row results var gameCount = (from allGames in db.Games where allGames.CalendarWeek == this.week select allGames).Count(); //Shows warning if week selection contains no data. String thisWeek = Convert.ToString(this.week); if ((gameCount < 1) && (thisWeek != "1/1/0001 12:00:00 AM")) { StatusLabel.Text = "There is nothing in this week!"; AlertFlash.Visible = true; } else { AlertFlash.Visible = false; } // bind the result to the DataList GameDataList.DataSource = Games.ToList(); GameDataList.DataBind(); } }
/** * <summary> * This method * </summary> * * @method SaveButton_Click * @return {void} */ protected void SaveButton_Click(object sender, EventArgs e) { // Use Ef to connect to the server using (GameTrackConnection db = new GameTrackConnection()) { // Use the game model to create a new game object and also 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 game from the EF database newGame = (from game in db.Games where game.GameID == GameID select game).FirstOrDefault(); } // add data to the new game record newGame.GameName = GameNameTextBox.Text; newGame.TeamOne = TeamOneTextBox.Text; newGame.TeamOneScore = TeamOneScoreTextBox.Text; newGame.TeamTwo = TeamTwoTextBox.Text; newGame.TeamTwoScore = TeamTwoScoreTextBox.Text; newGame.GameResult = GameResultTextBox.Text; newGame.CalendarWeek = Convert.ToDateTime(CalendarWeekTextBox.Text); //Check if more than 4 records for selected week. this.checkGame(CalendarWeekTextBox.Text); // use LINQ to ADO.NET to add / insert new game into the database if (GameID == 0) { db.Games.Add(newGame); } // save our changes db.SaveChanges(); // redirect back to the updated manage games page Response.Redirect("~/GameTrack/ManageGames.aspx"); } }
/** * <summary> * This method gets checks if there are 4 games in the selected calendar week. If yes then redirect. * </summary> * * @method checkGAme * @return {void} */ protected void checkGame(String selectedWeek) { DateTime formattedWeek = Convert.ToDateTime(selectedWeek); // connect to EF using (GameTrackConnection db = new GameTrackConnection()) { // query the games table using EF and LINQ var gameCount = (from allGames in db.Games where allGames.CalendarWeek == formattedWeek select allGames).Count(); System.Diagnostics.Debug.WriteLine(gameCount); if (gameCount >= 4) { // redirect back to the updated manage games page Response.Redirect("~/GameTrack/GameDetails.aspx?error=1"); } } }
/** * <summary> * This method processes the game delete functionality. * </summary> * * @method GameDataList_DeleteCommand * @return {void} */ protected void GameDataList_DeleteCommand(object source, DataListCommandEventArgs e) { int GameID = Convert.ToInt32(GameDataList.DataKeys[e.Item.ItemIndex]); using (GameTrackConnection db = new GameTrackConnection()) { // create object of the game 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 changes back to the db db.SaveChanges(); // refresh the grid this.GetGames(); } }