public void usersGrid_DeleteItem(int userId) { using (PetanqueContext db = new PetanqueContext()) { // Change any games that reference this user to reference a dummy user instead List <Game> refGames = GetGamesByUserReference(userId); int dummyUserId = GetUserIdByName("[Deleted]"); foreach (Game game in refGames) { if (game.Winner1Id == userId) { game.Winner1Id = dummyUserId; db.Entry(game).State = EntityState.Modified; } if (game.Winner2Id == userId) { game.Winner2Id = dummyUserId; db.Entry(game).State = EntityState.Modified; } if (game.Winner3Id == userId) { game.Winner3Id = dummyUserId; db.Entry(game).State = EntityState.Modified; } if (game.Loser1Id == userId) { game.Loser1Id = dummyUserId; db.Entry(game).State = EntityState.Modified; } if (game.Loser2Id == userId) { game.Loser2Id = dummyUserId; db.Entry(game).State = EntityState.Modified; } if (game.Loser3Id == userId) { game.Loser3Id = dummyUserId; db.Entry(game).State = EntityState.Modified; } } db.SaveChanges(); var item = new User { UserID = userId }; db.Entry(item).State = EntityState.Deleted; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { ModelState.AddModelError("", String.Format("Item with id {0} no longer exists in the database.", userId)); } } }
public void gamesGrid_UpdateItem(int gameID) { using (PetanqueContext db = new PetanqueContext()) { Game item = null; item = db.Games.Find(gameID); if (item == null) { ModelState.AddModelError("", String.Format("Item with id {0} was not found", gameID)); return; } // change foreign keys to correct ids based on name selected during update item.Winner1Id = (from u in db.Users where u.Name == newWinner1Name select u.UserID).SingleOrDefault(); item.Winner1 = (from u in db.Users where u.UserID == item.Winner1Id select u).SingleOrDefault(); item.Winner2Id = (from u in db.Users where u.Name == newWinner2Name select u.UserID).SingleOrDefault(); item.Winner2 = (from u in db.Users where u.UserID == item.Winner2Id select u).SingleOrDefault(); item.Winner3Id = (from u in db.Users where u.Name == newWinner3Name select u.UserID).SingleOrDefault(); item.Winner3 = (from u in db.Users where u.UserID == item.Winner3Id select u).SingleOrDefault(); item.Loser1Id = (from u in db.Users where u.Name == newLoser1Name select u.UserID).SingleOrDefault(); item.Loser1 = (from u in db.Users where u.UserID == item.Loser1Id select u).SingleOrDefault(); item.Loser2Id = (from u in db.Users where u.Name == newLoser2Name select u.UserID).SingleOrDefault(); item.Loser2 = (from u in db.Users where u.UserID == item.Loser2Id select u).SingleOrDefault(); item.Loser3Id = (from u in db.Users where u.Name == newLoser3Name select u.UserID).SingleOrDefault(); item.Loser3 = (from u in db.Users where u.UserID == item.Loser3Id select u).SingleOrDefault(); TryUpdateModel(item); if (ModelState.IsValid) { db.SaveChanges(); } } }
public void addUserForm_InsertItem() { var item = new User(); TryUpdateModel(item); if (ModelState.IsValid) { using (PetanqueContext db = new PetanqueContext()) { db.Users.Add(item); db.SaveChanges(); } } }
public void gamesGrid_DeleteItem(int gameID) { using (PetanqueContext db = new PetanqueContext()) { var item = new Game { GameID = gameID }; db.Entry(item).State = EntityState.Deleted; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { ModelState.AddModelError("", String.Format("Item with id {0} no longer exists in the database.", gameID)); } } }
public void usersGrid_UpdateItem(int userId) { using (PetanqueContext db = new PetanqueContext()) { User item = null; item = db.Users.Find(userId); if (item == null) { ModelState.AddModelError("", String.Format("Item with id {0} was not found", userId)); return; } TryUpdateModel(item); if (ModelState.IsValid) { db.SaveChanges(); } } }
protected void insertButton_Click(object sender, EventArgs e) { // return and show error if date isn't valid DateTime dt; if (!DateTime.TryParseExact(tbDate.Text, "dd-MMM-yy", null, DateTimeStyles.None, out dt) == true) { // not a valid DateTime in short date string format lValidation.Text = "Please enter a valid date using the calendar"; return; } using (PetanqueContext db = new PetanqueContext()) { var item = new Game(); item.Winner1Id = (from u in db.Users where u.Name == ddWinner1.SelectedItem.Text select u.UserID).SingleOrDefault(); item.Winner1 = (from u in db.Users where u.UserID == item.Winner1Id select u).SingleOrDefault(); item.Winner2Id = (from u in db.Users where u.Name == ddWinner2.SelectedItem.Text select u.UserID).SingleOrDefault(); item.Winner2 = (from u in db.Users where u.UserID == item.Winner2Id select u).SingleOrDefault(); item.Winner3Id = (from u in db.Users where u.Name == ddWinner3.SelectedItem.Text select u.UserID).SingleOrDefault(); item.Winner3 = (from u in db.Users where u.UserID == item.Winner3Id select u).SingleOrDefault(); item.Loser1Id = (from u in db.Users where u.Name == ddLoser1.SelectedItem.Text select u.UserID).SingleOrDefault(); item.Loser1 = (from u in db.Users where u.UserID == item.Loser1Id select u).SingleOrDefault(); item.Loser2Id = (from u in db.Users where u.Name == ddLoser2.SelectedItem.Text select u.UserID).SingleOrDefault(); item.Loser2 = (from u in db.Users where u.UserID == item.Loser2Id select u).SingleOrDefault(); item.Loser3Id = (from u in db.Users where u.Name == ddLoser3.SelectedItem.Text select u.UserID).SingleOrDefault(); item.Loser3 = (from u in db.Users where u.UserID == item.Loser3Id select u).SingleOrDefault(); item.LosingScore = Int32.Parse(ddLosingScore.SelectedItem.Text); item.Date = Convert.ToDateTime(tbDate.Text); item.League = (LeagueType)Enum.Parse(typeof(LeagueType), ddLeague.SelectedItem.Text); db.Games.Add(item); db.SaveChanges(); } Response.Redirect("~/Games"); }