private void btnAdd_Click(object sender, EventArgs e) { if (!string.IsNullOrWhiteSpace(txtid.Text)) { MessageBox.Show("Xin nhập sản phẩm mới, bấm nút Nhập lại để xoá các field."); return; } if (!ValidateInput()) { MessageBox.Show("Lỗi dữ liệu!"); } else { //todo db sanpham nhap kho using (CuaHangTienLoiDbContext dbCxt = new CuaHangTienLoiDbContext(ClassKetnoi.contextOptions)) { Repository <SanPham> sanPhamRepo = new Repository <SanPham>(dbCxt); Repository <LoSanPham> loSanPhamRepo = new Repository <LoSanPham>(dbCxt); Repository <LoaiSanPham> loaiSanPhamRepo = new Repository <LoaiSanPham>(dbCxt); Repository <DonViSanPham> donViSanPhamRepo = new Repository <DonViSanPham>(dbCxt); Repository <NhaCungCap> nccRepo = new Repository <NhaCungCap>(dbCxt); var sanphamMoi = new SanPham { Id = Guid.NewGuid(), TenSanPham = txttensp.Text, GiaTien = double.Parse(txtgiaban.Text), LoaiSanPhamId = (comboloai.SelectedItem as LoaiSanPham).Id, DonViSanPhamId = (combodonvi.SelectedItem as DonViSanPham).Id, }; var loSanPhamMoi = new LoSanPham { Id = Guid.NewGuid(), SanPhamId = sanphamMoi.Id, NhaCungCapId = (combonhaCungCap.SelectedItem as NhaCungCap).Id, NgayNhap = DateTime.Now, SoLuong = int.Parse(txtsl.Text) }; sanPhamRepo.Insert(sanphamMoi); loSanPhamRepo.Insert(loSanPhamMoi); MessageBox.Show("Đã nhập xong"); } txtid.ReadOnly = true; RefreshGridview(); } }
private void btnsave_Click(object sender, EventArgs e) { if (string.IsNullOrWhiteSpace(txtid.Text)) { MessageBox.Show("Trống mã sản phẩm!"); txtid.Select(); return; } if (!ValidateInput()) { MessageBox.Show("Lỗi dữ liệu!"); } else { using (CuaHangTienLoiDbContext dbCxt = new CuaHangTienLoiDbContext(ClassKetnoi.contextOptions)) { Repository <SanPham> sanPhamRepo = new Repository <SanPham>(dbCxt); Repository <LoSanPham> loSanPhamRepo = new Repository <LoSanPham>(dbCxt); Repository <LoaiSanPham> loaiSanPhamRepo = new Repository <LoaiSanPham>(dbCxt); Repository <DonViSanPham> donViSanPhamRepo = new Repository <DonViSanPham>(dbCxt); Repository <NhaCungCap> nccRepo = new Repository <NhaCungCap>(dbCxt); var loSanPhamMoi = new LoSanPham { Id = Guid.NewGuid(), SanPhamId = Guid.Parse(txtid.Text), NhaCungCapId = (combonhaCungCap.SelectedItem as NhaCungCap).Id, NgayNhap = DateTime.Now, SoLuong = int.Parse(txtsl.Text) }; loSanPhamRepo.Insert(loSanPhamMoi); MessageBox.Show("Đã nhập xong"); } RefreshGridview(); } }