private void CRUD_Product(int? proId, UnileverObject.BLL.CRUDOPTION option) { Product p = new Product(); string name = this.txtpName.Text; string price = this.txtpPrice.Text; string remain = this.txtpRemain.Text; string descript = this.txtpDescript.Text; string catid = null; try { catid = ((Category)this.cbxpCategory.SelectedItemValue).ID.ToString(); } catch (Exception) { UnileverError.Show("Choose a category for this product", UnileverError.WARN_CAPTION, System.Windows.Forms.MessageBoxIcon.Warning); return; } string importdate = this.depImportDate.EditValue.ToString(); if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(price) || string.IsNullOrEmpty(remain) || string.IsNullOrEmpty(descript) || string.IsNullOrEmpty(catid) || string.IsNullOrEmpty(importdate)) { UnileverError.Show("Missing field !", UnileverError.WARN_CAPTION, System.Windows.Forms.MessageBoxIcon.Warning ); } else { try { if (this.UnileverBll.SaveChangesProduct(proId.Value, name, catid, price, importdate, remain, descript, option)) { LoadProductToControl(); } } catch (Exception) // lưu dữ liệu bị lỗi { UnileverError.Show("Something fail, try again!", UnileverError.ERR_CAPTION, System.Windows.Forms.MessageBoxIcon.Warning ); } } }
public bool SaveChangesProduct(int proId, string name, string catid, string price, string importdate, string remain, string descript, CRUDOPTION option = CRUDOPTION.CREATE) { Product p = null; try { switch (option) { case CRUDOPTION.CREATE: { p = new Product(); p.Name = name; p.Descript = descript; int rm; int pr; int cid; int.TryParse(remain, out rm); int.TryParse(price, out pr); int.TryParse(catid, out cid); p.Price = pr; p.RemainingAmount = rm; p.CatID = cid; DateTime ipd = DateTime.Parse(importdate); p.ImportDate = ipd; this.UnileverEntities.Products.Add(p); break; } case CRUDOPTION.UPDATE: { p = this.UnileverEntities.Products.Where(p1 => p1.ID == proId).FirstOrDefault(); p.Name = name; p.Descript = descript; int rm; int pr; int cid; int.TryParse(remain, out rm); int.TryParse(price, out pr); int.TryParse(catid, out cid); p.Price = pr; p.RemainingAmount = rm; p.CatID = cid; break; } case CRUDOPTION.DELETE: { p = this.UnileverEntities.Products.Where(p1 => p1.ID == proId).FirstOrDefault(); this.UnileverEntities.Products.Remove(p); break; } default: break; } this.UnileverEntities.SaveChanges(); } catch (Exception ex) { throw ex; } return true; }