public Boolean Add(DataTable dt, ChiTietShopping cts) { try { return(new ChiTietShoppingDAL().Add(dt, cts)); } catch (SqlException ex) { throw ex; } }
public Boolean Add(DataTable dt, ChiTietShopping cts) { foreach (DataRow dr in dt.Rows) { if (Convert.ToInt32(dr["IDShopping"]) == cts.iDShopping && Convert.ToInt32(dr["IDSanPham"]) == cts.iDSanPham) { throw new Exception("IDShopping và IDSanPham đã tồn tại!"); } } DataRow newRow = dt.NewRow(); newRow["IDShopping"] = cts.iDShopping; newRow["IDSanPham"] = cts.iDSanPham; newRow["Soluong"] = cts.soLuong; newRow["ThanhTien"] = cts.thanhTien; dt.Rows.Add(newRow); return(true); }
private void btnBuy_Click(object sender, EventArgs e) { if (lookKhachHang.ItemIndex == -1) { MessageBox.Show("Vui lòng chọn Khách Hàng...", "Chú ý!", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (dtChoose.Rows.Count == 0) { MessageBox.Show("Giỏ hàng trống...", "Chú ý!", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } // Insert Shopping string sql = "SELECT * FROM Shopping"; DataTable dtS = new ShoppingBLL().GetShopping(sql); Shopping s = new Shopping(iD, Convert.ToInt64(lookKhachHang.EditValue), frmDichVu.iDNhanVien, DateTime.Now, tongTien, "", true); try { new ShoppingBLL().Add(dtS, s); new ShoppingBLL().SaveData(ref dtS); } catch (Exception ex) { MessageBox.Show("Lỗi: " + ex.Message, "Chú ý!", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // Insert CTShopping sql = "SELECT * FROM ChiTietShopping"; DataTable dtCTS = new ChiTietShoppingBLL().GetChiTietShopping(sql); foreach (DataRow dr in dtChoose.Rows) { ChiTietShopping cts = new ChiTietShopping(iD, Convert.ToInt32(dr[colID.FieldName]), soLuong, thanhTien); try { new ChiTietShoppingBLL().Add(dtCTS, cts); } catch (Exception ex) { MessageBox.Show("Lỗi: " + ex.Message, "Chú ý!", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // Update SanPham: SoLuong try { new SanPhamBLL().SubstractSoLuong(dtSP, Convert.ToInt32(dr[colID.FieldName]), Convert.ToInt32(dr[colSoLuong.FieldName])); } catch (Exception ex) { MessageBox.Show("Lỗi: " + ex.Message, "Chú ý!", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } // Save CTShopping try { new ChiTietShoppingBLL().SaveData(ref dtCTS); } catch (Exception ex) { MessageBox.Show("Lỗi: " + ex.Message, "Chú ý!", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // Save CTShopping try { new SanPhamBLL().SaveData(ref dtSP); } catch (Exception ex) { MessageBox.Show("Lỗi: " + ex.Message, "Chú ý!", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } MessageBox.Show("Mua thành công...", "Thông báo!", MessageBoxButtons.OK, MessageBoxIcon.Information); uscShopping_Load(sender, e); }