private void btnCapNhat_Click(object sender, EventArgs e) { try { if (dataGridViewDS.Rows.Count < 1) { HoTro.baoLoi("Nhập dữ liệu để cập nhật"); return; } HoTro ht = new HoTro() ; SqlConnection conn = ht.KetNoi(); conn.Open(); SqlCommand command = new SqlCommand(); int maNguoiDung = DangNhap.idNguoiDung; for (int i = 0; i < dataGridViewDS.Rows.Count; i++) { DataGridViewRow row = dataGridViewDS.Rows[i]; // insert PhieuXuatKho command = new SqlCommand("select CustomerId from Customer where CusTomerName=@Cusname", conn); command.Parameters.Add("@CusNAme", row.Cells[1].Value.ToString()); int cusId = Int32.Parse(command.ExecuteScalar().ToString()) ; command = new SqlCommand("insert into BillSale values(@ngaythang, @nguoiMua, @manguoiDung)"); command.Parameters.Add("@ngaythang", ht.toSQLDateTime(txtNgayThang.Text)); command.Parameters.Add("@nguoiMua", cusId); command.Parameters.Add("@manguoiDung", maNguoiDung); ht.CapNhatDuLieu(command); // update Product Int64 billSaleId = ht.LayGiaTriTruongKhoaVuaChen2("BillSale", "BillSaleId"); command = new SqlCommand("update Product set StatusId = 2, billSaleId=@billSaleId where ProductId = @id"); command.Parameters.Add("@id", Int64.Parse(row.Cells[0].Value.ToString())); command.Parameters.Add("@billSaleId", billSaleId); ht.CapNhatDuLieu(command); } HoTro.thongBao("Đã cập nhật !"); conn.Close(); dataGridViewDS.Rows.Clear(); } catch (Exception ex) { //MessageBox.Show(ex.StackTrace); HoTro.baoLoi(ex.Message); } }
private void btnCapNhat_Click_1(object sender, EventArgs e) { try { ht = new HoTro(); SqlCommand command = new SqlCommand(); int maNguoiDung = DangNhap.idNguoiDung; for (int i = 0; i < dataGridViewDanhSach.Rows.Count; i++) { DataGridViewRow row = dataGridViewDanhSach.Rows[i]; // insert PhieuNhapKho int maNhaCungCap = ht.LayVeKhoa("Provider", "ProviderId", "ProviderName", row.Cells[8].Value.ToString()); command = new SqlCommand("insert into BillPurchase values(@ngaythang, @giaohang, @maNhacungcap, @manguoiDung)"); command.Parameters.Add("@ngaythang", ht.toSQLDateTime(txtNgayThang.Text)); command.Parameters.Add("@giaohang", row.Cells[9].Value.ToString()); command.Parameters.Add("@maNhacungcap", maNhaCungCap); command.Parameters.Add("@manguoiDung", maNguoiDung); ht.CapNhatDuLieu(command); int soLuong = Int32.Parse(row.Cells[1].Value.ToString()); int donvi = ht.LayVeKhoa("Units", "UnitId", "UnitName", row.Cells[4].Value.ToString()); Int64 billPurchaseId = ht.LayGiaTriTruongKhoaVuaChen2("BillPurchase", "BillPurchaseId"); for (int j = 0; j < soLuong; j++) { // insert SanPham int IdtenSp = ht.LayVeKhoa("ProductName", "ProductNameId", "ProdName", row.Cells[0].Value.ToString()); command = new SqlCommand("insert into Product values(" + "@ngaysx, @ngayHetHan, @IdtenSP, @PurchasePrice, null," + "null, @statusId, @billPurchaseId, null, @UnitId, @mota)"); command.Parameters.Add("@ngaysx", row.Cells[2].Value.ToString()); command.Parameters.Add("@ngayHetHan", row.Cells[3].Value.ToString()); command.Parameters.Add("@IdtenSP", IdtenSp); command.Parameters.Add("@PurchasePrice", Int32.Parse(row.Cells[7].Value.ToString())); //VietLH change string to int on 9 Dec 2009 command.Parameters.Add("@statusId", 1); command.Parameters.Add("@billPurchaseId", billPurchaseId); command.Parameters.Add("@UnitId", donvi); command.Parameters.Add("@mota", richTextBoxMota.Text); ht.CapNhatDuLieu(command); } } HoTro.thongBao("Nhập hàng thành công !"); dataGridViewDanhSach.Rows.Clear(); } catch (Exception ex) { HoTro.baoLoi(ex.Message); } }