public void update_Product(Product p) { using (var context = new ShopReviewsdb()) { var old = context.Products.Find(p.ProductID); old.ProductID = p.ProductID; old.Name = p.Name; old.NutritionalValues = p.NutritionalValues; old.Price = p.Price; old.ShopID = p.ShopID; old.SugarFree = p.SugarFree; old.Vegan = p.Vegan; old.ShopID = p.ShopID; old.Description = p.Description; try { old.GetType().GetProperty("FreeExtras").SetValue(old, p.GetType().GetProperty("FreeExtras").GetValue(p)); } catch (Exception e) { } try { old.GetType().GetProperty("Fat").SetValue(old, p.GetType().GetProperty("Fat").GetValue(p)); } catch (Exception e) { } try { old.GetType().GetProperty("MilkType").SetValue(old, p.GetType().GetProperty("MilkType").GetValue(p)); } catch (Exception e) { } try { old.GetType().GetProperty("Flaver").SetValue(old, p.GetType().GetProperty("Flaver").GetValue(p)); } catch (Exception e) { } try { old.GetType().GetProperty("GlutenFree").SetValue(old, p.GetType().GetProperty("GlutenFree").GetValue(p)); } catch (Exception e) { } context.SaveChanges(); } }
private List <SqlParameter> GenerateSQLParameters(Product product) { var paramList = new List <SqlParameter>(); Type modelType = product.GetType(); var properties = modelType.GetProperties(); foreach (var property in properties) { if (property.GetValue(product) == null) { paramList.Add(new SqlParameter(property.Name, DBNull.Value)); } else { paramList.Add(new SqlParameter(property.Name, property.GetValue(product))); } } return(paramList); }