private void btnSua_Click(object sender, EventArgs e) { try { int id = Convert.ToInt32(lblID.Text); int maPhongchieu = Convert.ToInt32(cbbPhongchieu.SelectedValue); GHE ghe = Session.dbContxt.GHEs.Where(x => x.ID == id).FirstOrDefault(); int oldMaPhongchieu = Convert.ToInt32(ghe.PhongChieuID); ghe.Loaighe = cbbLoaighe.SelectedIndex; ghe.Vitri = txtVitri.Text; ghe.PhongChieuID = maPhongchieu; if (maPhongchieu != oldMaPhongchieu) { Session.dbContxt.PHONGCHIEUx.Where(x => x.ID == maPhongchieu).FirstOrDefault().Size += 1; Session.dbContxt.PHONGCHIEUx.Where(x => x.ID == oldMaPhongchieu).FirstOrDefault().Size -= 1; } Session.dbContxt.SaveChanges(); MessageBox.Show("Sửa thành công"); reloadData(); } catch (Exception ex) { MessageBox.Show("Sửa thất bại. Có lỗi xảy ra", ex.ToString()); } }
private void btnThem_Click(object sender, EventArgs e) { try { int maPhongchieu = Convert.ToInt32(cbbPhongchieu.SelectedValue); GHE ghe = new GHE(); ghe.Vitri = txtVitri.Text; ghe.Loaighe = cbbLoaighe.SelectedIndex; ghe.Tinhtrang = false; ghe.PhongChieuID = maPhongchieu; Session.dbContxt.GHEs.Add(ghe); Session.dbContxt.PHONGCHIEUx.Where(x => x.ID == maPhongchieu).FirstOrDefault().Size += 1; Session.dbContxt.SaveChanges(); MessageBox.Show("Thêm thành công"); reloadData(); } catch (Exception ex) { MessageBox.Show("Có lỗi xảy ra", ex.ToString()); } }
private void btnXoa_Click(object sender, EventArgs e) { try { int id = Convert.ToInt32(lblID.Text); GHE ghe = Session.dbContxt.GHEs.Where(x => x.ID == id).FirstOrDefault(); int maphongchieu = Convert.ToInt32(ghe.PhongChieuID); Session.dbContxt.PHONGCHIEUx.Where(x => x.ID == maphongchieu).FirstOrDefault().Size -= 1; Session.dbContxt.GHEs.Remove(ghe); MessageBox.Show("Xóa thành công"); reloadData(); } catch (Exception ex) { MessageBox.Show("Xóa thất bại. Có lỗi xảy ra", ex.ToString()); } }