private void btAdd_Click(object sender, EventArgs e) { var DTQuery = (from DT in db.communes where DT.commune_id == txtCID.Text select DT).SingleOrDefault(); if (DTQuery != null) { MessageBox.Show("Commune ID is already existed", "Lỗi!"); } else { try { commune DT = new commune(); DT.commune_id = txtCID.Text; DT.commune_name = txtCName.Text; DT.degree = int.Parse(txtDegree.Text); DT.district_id = txtDID.Text; db.communes.Add(DT); db.SaveChanges(); } catch { MessageBox.Show("District_id chưa tồn tại trong Bảng District.Vui lòng cập nhật District_id trong bảng District trước!", "Lỗi khóa ngoại!"); } MySetCommune(); } }
private void btDelete_Click(object sender, EventArgs e) { int r = dtGridView.CurrentCell.RowIndex; string tempCID = dtGridView.Rows[r].Cells[0].Value.ToString(); commune CommQ = db.communes.Single(x => x.commune_id == tempCID); //db.districts.DeleteOnSubmit(DistQ); //db.SubmitChanges(); db.communes.Remove(CommQ); db.SaveChanges(); MySetDistrict(); }
private void btSave_Click(object sender, EventArgs e) { //db = new SalesDBMF(); int r = dtGridView.CurrentCell.RowIndex; string tempCID = dtGridView.Rows[r].Cells[0].Value.ToString(); commune DTQuery = db.communes.Single(x => x.commune_id == tempCID); if (DTQuery != null) { district DQuery2 = db.districts.Single(x => x.district_id == txtDID.Text); var DTQuery2 = (from DT in db.communes where DT.commune_id == txtCID.Text select DT).SingleOrDefault(); if (DQuery2 == null) { MessageBox.Show("District_id chưa tồn tại trong Bảng District, Vui lòng cập nhật District_id trong bảng District trước!", "Lỗi khóa ngoại!"); return; } if (DTQuery.commune_id == txtCID.Text) { //DTQuery.district_id = txtDID.Text; DTQuery.commune_name = txtCName.Text; DTQuery.degree = int.Parse(txtDegree.Text); DTQuery.district_id = txtDID.Text; db.SaveChanges(); } else { if (DTQuery2 != null) { MessageBox.Show("Commune_ID đã tồn tại trong Bảng Commune không sửa được!", "Lỗi khóa!"); return; } db.communes.Remove(DTQuery); db.SaveChanges(); commune DT = new commune(); DT.commune_id = txtDID.Text; DT.commune_name = txtCName.Text; DT.degree = int.Parse(txtDegree.Text); DT.district_id = txtDID.Text; db.communes.Add(DT); db.SaveChanges(); } } MySetCommune(); }