// ADD public static bool AddFilterData(filterdata f) { Context.filterdata.Add(f); try { Context.SaveChanges(); } catch (DbUpdateException dbEx) { return false; } catch (DbEntityValidationException ex) { foreach (DbEntityValidationResult item in ex.EntityValidationErrors) { // Get entry DbEntityEntry entry = item.Entry; string entityTypeName = entry.Entity.GetType().Name; // Display or log error messages foreach (DbValidationError subItem in item.ValidationErrors) { string message = string.Format("Error '{0}' occurred in {1} at {2}", subItem.ErrorMessage, entityTypeName, subItem.PropertyName); Console.WriteLine(message); } // Rollback changes switch (entry.State) { case EntityState.Added: entry.State = EntityState.Detached; break; case EntityState.Modified: entry.CurrentValues.SetValues(entry.OriginalValues); entry.State = EntityState.Unchanged; break; case EntityState.Deleted: entry.State = EntityState.Unchanged; break; } } return false; } Context.Entry(f).Reload(); return true; }
// DELETE public static bool DeleteFilterData(filterdata f) { filterdata filterDataToDelete = GetFilterDataById(f.Id); filterDataToDelete.IsDeleted = true; int affectedRows; try { affectedRows = Context.SaveChanges(); } catch (DbEntityValidationException ex) { foreach (DbEntityValidationResult item in ex.EntityValidationErrors) { // Get entry DbEntityEntry entry = item.Entry; string entityTypeName = entry.Entity.GetType().Name; // Display or log error messages foreach (DbValidationError subItem in item.ValidationErrors) { string message = string.Format("Error '{0}' occurred in {1} at {2}", subItem.ErrorMessage, entityTypeName, subItem.PropertyName); Console.WriteLine(message); } // Rollback changes switch (entry.State) { case EntityState.Added: entry.State = EntityState.Detached; break; case EntityState.Modified: entry.CurrentValues.SetValues(entry.OriginalValues); entry.State = EntityState.Unchanged; break; case EntityState.Deleted: entry.State = EntityState.Unchanged; break; } } return false; } return affectedRows > 0; }
protected void AddControl_OnClick(object sender, EventArgs e) { components newComponent = new components(); int oNo; newComponent.webpages_Id = int.Parse(HiddenFieldWebPageId.Value); newComponent.OrderingNumber = int.TryParse(tbAddOrderingNumber.Text, out oNo) ? oNo : 1; newComponent.controls_Id = int.Parse(ddlAddComControls.SelectedValue); if (ComponentDB.AddComponent(newComponent)) { ActionStatusComponentsList.Text = "New component has successfully been added!"; ActionStatusComponentsList.ForeColor = Color.CornflowerBlue; controls c = ControlDB.GetControlsById(newComponent.controls_Id); var filterTypeNameList = new List<string>(); if (c != null) { //EventHandlingSystem.Components. Type cls = Type.GetType("EventHandlingSystem.Components."+c.Name); if (cls != null) { foreach (var prop in cls.GetProperties()) { filterTypeNameList.Add(prop.Name); } } } ActionStatusComponentsList.Text += "<br/>FilterData added:"; foreach (var type in filterTypeNameList) { filterdata newFilterData = new filterdata {Type = type, Components_Id = newComponent.Id, Data = ""}; if (FilterDataDB.AddFilterData(newFilterData)) { ActionStatusComponentsList.Text += " " + newFilterData.Type + "=\""+ newFilterData.Data+"\""; } } } else { ActionStatusComponentsList.Text = "Sorry! New component was not added."; ActionStatusComponentsList.ForeColor = Color.Red; } PopulateDropDownListControls(); DisplayComponentsForWebPage(); }