protected void RemoveVisitorButton_Click(object sender, EventArgs e) { VisitorWrapper selected_visitor; try { selected_visitor = Visitors[VisitorsDropDownList.SelectedIndex]; } catch { removeVisitorMessage.Text = "No visitor selected"; return; } using (var db = new VisitContext()) { VisitorWrapper visitor_to_remove = (VisitorWrapper)db.Visitors.Where(b => b.Id == selected_visitor.Id).First(); db.Visitors.Remove(visitor_to_remove); db.SaveChanges(); Visitor vis = visitor_to_remove.Visitor; removeVisitorMessage.Text = $"Removed: {vis.FirstName} {vis.LastName} from {vis.CompanyName}"; } UpdateVisitorList(); UpdateVisitorDropDownList(); }
private void AddVisitorToDatabase(Visitor visitor) { VisitorWrapper visitor_w = new VisitorWrapper { Visitor = visitor, Id = visitor.FirstName + visitor.LastName + visitor.CompanyName }; using (var db = new VisitContext()) { var visitors_to_remove = db.Visitors.Where(b => b.Id == visitor_w.Id); foreach (VisitorWrapper vis in visitors_to_remove) { db.Visitors.Remove(vis); } db.Visitors.Add(visitor_w); db.SaveChanges(); } }