private void OkBtn_Click(object sender, EventArgs e) { try { Cursor.Current = Cursors.WaitCursor; var db = new CkpEntities(); if (TenDgTBox.Text.Length == 0) { TenDgLbl.ForeColor = Color.Red; return; } else { TenDgLbl.ForeColor = Color.Black; } foreach (TextBox tb in this.Controls.OfType <TextBox>()) { int val = 0; if (tb.Name == "TenDgTBox" || tb.Name == "CthucTBox" || tb.Name == "CtrTBox") { continue; } else { if (tb.Text.Length > 0 && !int.TryParse(tb.Text.Replace(",", ""), out val)) { MessageBox.Show("Tất cả các thông tin số liệu phải là số", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } } var dg = (from d in db.DonGiaBom where d.Id == DgId select d).FirstOrDefault(); dg.TenDonGia = TenDgTBox.Text; dg.DonGia = Dg0TBox.Text.Length > 0 ? int.Parse(Dg0TBox.Text.Replace(",", "")) : (int?)null; dg.DonGiaVAT = Dg1TBox.Text.Length > 0 ? int.Parse(Dg1TBox.Text.Replace(",", "")) : (int?)null; dg.VAT = VatTBox.Text.Length > 0 ? int.Parse(VatTBox.Text) : (int?)null; dg.MinKlgBomCan = MinBcTBox.Text.Length > 0 ? int.Parse(MinBcTBox.Text) : (int?)null; dg.MinKlgBomTinh = MinBtTBox.Text.Length > 0 ? int.Parse(MinBtTBox.Text) : (int?)null; dg.CC1 = Cc1TBox.Text.Length > 0 ? int.Parse(Cc1TBox.Text.Replace(",", "")) : (int?)null; dg.TongCC2 = TgCc2TBox.Text.Length > 0 ? int.Parse(TgCc2TBox.Text.Replace(",", "")) : (int?)null; dg.PhanTramBackCC2 = PtCc2TBox.Text.Length > 0 ? int.Parse(PtCc2TBox.Text) : (int?)null; dg.TienBackCC2 = BackCc2TBox.Text.Length > 0 ? int.Parse(BackCc2TBox.Text.Replace(",", "")) : (int?)null; dg.NetCC2 = NetCc2TBox.Text.Length > 0 ? int.Parse(NetCc2TBox.Text.Replace(",", "")) : (int?)null; dg.TongThuVAT = Kl1TBox.Text.Length > 0 ? int.Parse(Kl1TBox.Text.Replace(",", "")) : (int?)null; dg.TongVAT = KlVatTBox.Text.Length > 0 ? int.Parse(KlVatTBox.Text.Replace(",", "")) : (int?)null; dg.TongThu = Kl0TBox.Text.Length > 0 ? int.Parse(Kl0TBox.Text.Replace(",", "")) : (int?)null; dg.TongCC = KlCcTBox.Text.Length > 0 ? int.Parse(KlCcTBox.Text.Replace(",", "")) : (int?)null; dg.CongThuc = CthucTBox.Text; dg.GhiChu = GcRtb.Text; dg.NgaySuaCuoi = DateTime.Now; db.SaveChanges(); Cursor.Current = Cursors.Default; MessageBox.Show("Thay đổi đơn giá thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); parentForm.UpdateDataGrid(); Close(); } catch (Exception ex) { MessageBox.Show(ex.GetBaseException().Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void OkBtn_Click(object sender, EventArgs e) { try { Cursor.Current = Cursors.WaitCursor; var db = new CkpEntities(); if (TenDgTBox.Text.Length == 0) { TenDgLbl.ForeColor = Color.Red; return; } else { TenDgLbl.ForeColor = Color.Black; } if (db.DonGiaBom.Where(dg => dg.CongTrinhId == CtrId && dg.TenDonGia == TenDgTBox.Text).Any()) { MessageBox.Show("Tên đơn giá này đã tồn tại", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } foreach (TextBox tb in this.Controls.OfType <TextBox>()) { int val = 0; if (tb.Name == "TenDgTBox" || tb.Name == "CthucTBox" || tb.Name == "CtrTBox") { continue; } else { if (tb.Text.Length > 0 && !int.TryParse(tb.Text.Replace(",", ""), out val)) { MessageBox.Show("Tất cả các thông tin số liệu phải là số", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } } var newDg = new DonGiaBom { CongTrinhId = CtrId, TenDonGia = TenDgTBox.Text, DonGia = Dg0TBox.Text.Length > 0 ? int.Parse(Dg0TBox.Text.Replace(",", "")) : (int?)null, DonGiaVAT = Dg1TBox.Text.Length > 0 ? int.Parse(Dg1TBox.Text.Replace(",", "")) : (int?)null, VAT = VatTBox.Text.Length > 0 ? int.Parse(VatTBox.Text) : (int?)null, MinKlgBomCan = MinBcTBox.Text.Length > 0 ? int.Parse(MinBcTBox.Text) : (int?)null, MinKlgBomTinh = MinBtTBox.Text.Length > 0 ? int.Parse(MinBtTBox.Text) : (int?)null, CC1 = Cc1TBox.Text.Length > 0 ? int.Parse(Cc1TBox.Text.Replace(",", "")) : (int?)null, TongCC2 = TgCc2TBox.Text.Length > 0 ? int.Parse(TgCc2TBox.Text.Replace(",", "")) : (int?)null, PhanTramBackCC2 = PtCc2TBox.Text.Length > 0 ? int.Parse(PtCc2TBox.Text) : (int?)null, TienBackCC2 = BackCc2TBox.Text.Length > 0 ? int.Parse(BackCc2TBox.Text.Replace(",", "")) : (int?)null, NetCC2 = NetCc2TBox.Text.Length > 0 ? int.Parse(NetCc2TBox.Text.Replace(",", "")) : (int?)null, TongThuVAT = Kl1TBox.Text.Length > 0 ? int.Parse(Kl1TBox.Text.Replace(",", "")) : (int?)null, TongVAT = KlVatTBox.Text.Length > 0 ? int.Parse(KlVatTBox.Text.Replace(",", "")) : (int?)null, TongThu = Kl0TBox.Text.Length > 0 ? int.Parse(Kl0TBox.Text.Replace(",", "")) : (int?)null, TongCC = KlCcTBox.Text.Length > 0 ? int.Parse(KlCcTBox.Text.Replace(",", "")) : (int?)null, CongThuc = CthucTBox.Text, GhiChu = GcRtb.Text, TrangThai = 1, NgayTao = DateTime.Now, NgaySuaCuoi = DateTime.Now }; db.DonGiaBom.Add(newDg); db.SaveChanges(); Cursor.Current = Cursors.Default; MessageBox.Show("Tạo đơn giá thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); parentForm.UpdateDataGrid(); Close(); } catch (Exception ex) { MessageBox.Show(ex.GetBaseException().Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error); } }