private void TaskControl1_SaveEvent(object sender, EventArgs e) { isFind = false; if (!inputIsCorrect()) { return; } taskControl1.isSuccessFul = true; var hangHoa = new HangHoa() { Name = txtProductName.Text, DVT = txtUnit.Text, SoLuong = int.Parse(nbNumber.Value.ToString()), DonGiaNhap = double.Parse(txtPriceBuy.Text), DonGiaBan = double.Parse(txtPriceSale.Text), GhiChu = txtNote.Text, DaXoa = false }; if (isAdd) { var rs = productBus.Add(hangHoa); if (rs == true) { MessageBox.Show("Thêm hàng hóa thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); LoadAll(); } else { MessageBox.Show("Thêm hàng hóa không thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); } } else { hangHoa.ID = int.Parse(txtProductId.Text); var rs = productBus.Update(hangHoa); if (rs == false) { MessageBox.Show("Sửa hóa không thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("Sửa hàng hóa thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); LoadAll(); } } panelContent.Enabled = false; }
public JsonResult SaveData(string name, string description, int id, int brand, int category, int type, int price, int quantity, int warranty) { bool status = false; string message = string.Empty; var model = new Product(); model.Name = name; model.Id = id; model.Description = description; model.Price = price; model.Quantity = quantity; model.Warranty = warranty; model.BrandId = brand; model.CateId = category; model.TypeId = type; if (HttpContext.Request.Files.Count > 0) { // TODO: Add insert logic here var hpf = HttpContext.Request.Files[0]; if (hpf.ContentLength > 0) { string fileName = Guid.NewGuid().ToString(); string fullPathWithFileName = "/Data/Images/" + fileName + ".jpg"; hpf.SaveAs(Server.MapPath(fullPathWithFileName)); model.ImageUrl = fullPathWithFileName; } } //add new productgory if id = 0 if (model.Id == 0) { model.DateCreated = DateTime.Now; model.DateModified = DateTime.Now; model.Status = true; model.Viewed = 0; model.Sold = 0; try { ProductBus.Add(model); for (int i = 1; i < HttpContext.Request.Files.Count; i++) { var proImage = new Image(); var pro = ProductBus.Find(model.Id); proImage.ProductId = pro.Id; proImage.Status = true; var hrf = HttpContext.Request.Files[i]; if (hrf.ContentLength > 0) { string fileName = Guid.NewGuid().ToString(); string fullPathWithFileName = "/Data/Images/" + fileName + ".jpg"; hrf.SaveAs(Server.MapPath(fullPathWithFileName)); proImage.ImageUrl = fullPathWithFileName; ImageBus.Add(proImage); } } status = true; } catch (Exception ex) { status = false; message = ex.Message; } } else { //update existing DB //save db var entity = ProductBus.Find(model.Id); if (HttpContext.Request.Files.Count > 0) { // TODO: Add insert logic here var hpf = HttpContext.Request.Files[0]; if (hpf.ContentLength > 0) { string fileName = Guid.NewGuid().ToString(); string fullPathWithFileName = "/Data/Images/" + fileName + ".jpg"; hpf.SaveAs(Server.MapPath(fullPathWithFileName)); entity.ImageUrl = fullPathWithFileName; } } entity.DateModified = DateTime.Now; entity.Name = model.Name; entity.BrandId = model.BrandId; entity.CateId = model.CateId; entity.TypeId = model.TypeId; entity.Price = model.Price; entity.Quantity = model.Quantity; entity.Description = model.Description; entity.Warranty = model.Warranty; entity.DateModified = DateTime.Now; try { ProductBus.Edit(entity); status = true; } catch (Exception ex) { status = false; message = ex.Message; } } return(Json(new { status = status, message = message })); }