protected void GetGame() { int GameID = Convert.ToInt32(Request.QueryString["GameID"]); // connect to the EF DB using (VJKMConnection db = new VJKMConnection()) { Game updatedGame = (from game in db.Games where game.GameID == GameID select game).FirstOrDefault(); { GameNameTextBox.Text = updatedGame.GameName; GLocationTextBox.Text = updatedGame.GameLocation; GameDesc.Text = updatedGame.GameDescription; TeamA.Text = updatedGame.TeamA; TeamB.Text = updatedGame.TeamB; TeamAScore.Text = updatedGame.TeamAScore.ToString(); TeamBScore.Text = updatedGame.TeamBScore.ToString(); Spectators.Text = updatedGame.Spectators.ToString(); WinningTeam.Text = updatedGame.WinningTeam; GameDateTextBox.Text = updatedGame.GameDate.ToString("yyyy-MM-dd"); } } }
protected void GameGridView_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(GameGridView.DataKeys[selectedRow].Values["GameID"]); // use EF to find the selected game in the DB and remove it using (VJKMConnection db = new VJKMConnection()) { // 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 my changes back to the database db.SaveChanges(); // refresh the grid this.ConnectionToDB(); } }
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { using (VJKMConnection db = new VJKMConnection()) { //starting and ending date of a week. DateTime mondayDate = DateTime .Today .AddDays(((int)(DateTime.Today.DayOfWeek) * -1) + 1); DateTime sundayDate = mondayDate.AddDays(6); Heading.Text = mondayDate.ToShortDateString(); //return results between starting and ending date. var gameEdit4 = (from allgames in db.Games where allgames.GameDate >= mondayDate && allgames.GameDate <= sundayDate select allgames); display(gameEdit4); } } }
protected void ConnectionToDB() { using (VJKMConnection db = new VJKMConnection()) { var gameEdit = (from allgames in db.Games select allgames); // bind the result to the GridView GameGridView.DataSource = gameEdit.AsQueryable().ToList(); GameGridView.DataBind(); } }
protected void SaveButton_Click(object sender, EventArgs e) { // Use EF to connect to the server using (VJKMConnection db = new VJKMConnection()) { // 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 Game from EF DB newGame = (from game in db.Games where game.GameID == GameID select game).FirstOrDefault(); } // add form data to the new game record newGame.GameName = GameNameTextBox.Text; newGame.GameLocation = GLocationTextBox.Text; newGame.GameDescription = GameDesc.Text; newGame.TeamA = TeamA.Text; newGame.TeamB = TeamB.Text; newGame.TeamAScore = Convert.ToInt32(TeamAScore.Text); newGame.TeamBScore = Convert.ToInt32(TeamBScore.Text); newGame.Spectators = Convert.ToInt32(Spectators.Text); newGame.WinningTeam = WinningTeam.Text; newGame.GameDate = Convert.ToDateTime(GameDateTextBox.Text); // use LINQ to ADO.NET to add / insert new game into the database if (GameID == 0) { db.Games.Add(newGame); } // save our changes - also updates and inserts db.SaveChanges(); // Redirect back to the updated GameEditPage Response.Redirect("~/SecurePages/GameEditPage.aspx"); } }
protected void NextButton_Click(object sender, EventArgs e) { using (VJKMConnection db = new VJKMConnection()) { DateTime current1 = Convert.ToDateTime(Heading.Text); //starting and ending date of a week. DateTime NextMonday = current1.AddDays(+7); DateTime NextSunday = current1.AddDays(+13); Heading.Text = NextMonday.ToLongDateString(); //return results between starting and ending date. var gameEdit1 = (from allgames in db.Games where allgames.GameDate >= NextMonday && allgames.GameDate <= NextSunday select allgames); display(gameEdit1); } }