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 and LINQ to find the selected game in the DB and remove it using (CricketInfo db = new CricketInfo()) { var current = db.CricketInfoMains.Count(); //create object to the game class and store the query inside of it CricketInfoMain deletedgame = (from gameRecords in db.CricketInfoMains where gameRecords.GameID == GameID select gameRecords).FirstOrDefault(); // remove the selected game from teh db db.CricketInfoMains.Remove(deletedgame); //save my changes back to the db db.SaveChanges(); db.Database.ExecuteSqlCommand("DBCC CHECKIDENT (CricketInfoMain, RESEED, " + (current - 1) + ")"); // refresh the grid this.GetGames(); } }
protected void AddGameButton_Click(object sender, EventArgs e) { // connect to the EF DB using (CricketInfo db = new CricketInfo()) { // use the cricket model to create a new student object and save a new record CricketInfoMain newGame = new CricketInfoMain(); int GameID = 0; if (Request.QueryString.Count > 0)// our URL has a GameId in it { //get the id from teh URL GameID = Convert.ToInt32(Request.QueryString["GameID"]); // get the current game from EF db newGame = (from game in db.CricketInfoMains where game.GameID == GameID select game).FirstOrDefault(); } // add form data to the new stuent record newGame.Team_1_Name = Team1Name.Text; newGame.Team_1_Outs = Convert.ToInt32(Team1Out.Text); newGame.Team_1_Runs = Convert.ToInt32(Team1Runs.Text); newGame.Team_2_Name = Team2Name.Text; newGame.Team_2_Outs = Convert.ToInt32(Team2Out.Text); newGame.Team_2_Runs = Convert.ToInt32(Team2Runs.Text); newGame.Spectators = Convert.ToInt32(Spectators.Text); newGame.DateEntered = DateTime.Now; // use LINQ to ADO.NET to add / insert new game into the db if (GameID == 0) { db.CricketInfoMains.Add(newGame); } // save our changes - also updates and inserts db.SaveChanges(); //Redirect back to the GameList Response.Redirect("~/Admin/AdminPage"); } }