private void btDelete_Click(object sender, EventArgs e) { int r = dtGridView.CurrentCell.RowIndex; string tempDID = dtGridView.Rows[r].Cells[1].Value.ToString(); brand BraQ = db.brands.Single(x => x.brand_name == tempDID); //db.districts.DeleteOnSubmit(DistQ); //db.SubmitChanges(); db.brands.Remove(BraQ); db.SaveChanges(); MySetBrand(); }
private void btSave_Click(object sender, EventArgs e) { //db = new SalesDBMF(); int r = dtGridView.CurrentCell.RowIndex; string tempDID = dtGridView.Rows[r].Cells[1].Value.ToString(); brand BrandQuery = db.brands.Single(x => x.brand_name == tempDID); if (BrandQuery != null) { var BrandQuery2 = (from DT in db.brands where DT.brand_name == txtBrandname.Text select DT).SingleOrDefault(); if (BrandQuery.brand_name == txtBrandname.Text) { //BrandQuery.district_id = txtDID.Text; BrandQuery.brand_name = txtBrandname.Text; db.SaveChanges(); } else { if (BrandQuery2 != null) { MessageBox.Show("Brand_name đã tồn tại trong Bảng Brand không sửa được!", "Lỗi khóa!"); return; } try { db.brands.Remove(BrandQuery); db.SaveChanges(); brand DT = new brand(); DT.brand_name = txtBrandname.Text; db.brands.Add(DT); db.SaveChanges(); } catch { MessageBox.Show("Brand_ID đã tồn tại khóa ngoại trong bảng Product, Vui lòng cập nhật Brand_ID trong bảng Product trước!", "Lỗi khóa ngoại!"); } } MySetBrand(); } }
private void btAdd_Click(object sender, EventArgs e) { var BrandQuery = (from DT in db.brands where DT.brand_name == txtBrandname.Text select DT).SingleOrDefault(); if (BrandQuery != null) { MessageBox.Show("Brand name is already existed", "Lỗi!"); } else { brand DT = new brand(); DT.brand_name = txtBrandname.Text; db.brands.Add(DT); db.SaveChanges(); MySetBrand(); } }
private void btSave_Click(object sender, EventArgs e) { //db = new SalesDBMF(); int r = dtGridView.CurrentCell.RowIndex; string tempDID = dtGridView.Rows[r].Cells[0].Value.ToString(); product DTQuery = db.products.Single(x => x.product_id == int.Parse(tempDID)); if (DTQuery != null) { brand BraQuery2 = db.brands.Single(x => x.brand_id == int.Parse(cbBrandID.Text)); category CateQuery2 = db.categories.Single(x => x.category_id == int.Parse(cbCateID.Text)); var DTQuery2 = (from DT in db.products where DT.product_id == int.Parse(txtProID.Text) select DT).SingleOrDefault(); if (BraQuery2 == null) { MessageBox.Show("Brand_id chưa tồn tại trong Bảng Brand, Vui lòng cập nhật Brand_id trong bảng Brand trước!", "Lỗi khóa ngoại!"); return; } if (CateQuery2 == null) { MessageBox.Show("Category_id chưa tồn tại trong Bảng Category, Vui lòng cập nhật Category_id trong bảng CustoCategorymer trước!", "Lỗi khóa ngoại!"); return; } if (DTQuery.product_id == int.Parse(txtProID.Text)) { //DTQuery.district_id = txtDID.Text; DTQuery.product_name = txtProductname.Text; DTQuery.brand_id = int.Parse(cbBrandID.Text); DTQuery.category_id = int.Parse(cbCateID.Text); DTQuery.list_price = decimal.Parse(txtlistprice.Text); DTQuery.model_year = short.Parse(txtModelyears.Text); db.SaveChanges(); } else { if (DTQuery2 != null) { MessageBox.Show("Product_ID đã tồn tại trong Bảng Product không sửa được!", "Lỗi khóa!"); return; } db.products.Remove(DTQuery); db.SaveChanges(); product DT = new product(); DT.product_id = int.Parse(txtProID.Text); DT.product_name = txtProductname.Text; DT.brand_id = int.Parse(cbBrandID.Text); DT.category_id = int.Parse(cbCateID.Text); DT.list_price = decimal.Parse(txtlistprice.Text); DT.model_year = short.Parse(txtModelyears.Text); db.products.Add(DT); db.SaveChanges(); } } MySetProduct(); }