private void btnDelete_Click(object sender, EventArgs e) { if (MessageBox.Show("Are You Shure ?", "EF CURD Operation", MessageBoxButtons.YesNo) == DialogResult.Yes) { using (MYDATAEntities db = new MYDATAEntities()) { var entry = db.Entry(model); if (entry.State == EntityState.Detached) { db.Customers.Attach(model); } db.Customers.Remove(model); db.SaveChanges(); PopulateDataGridView(); Clear(); MessageBox.Show("Deleted SuccessFully..!"); } } }
private void btnSave_Click(object sender, EventArgs e) { model.FirstName = txtFirstName.Text.Trim(); model.LastName = txtLastName.Text.Trim(); model.City = txtCity.Text.Trim(); model.Address = txtAddress.Text.Trim(); using (MYDATAEntities db = new MYDATAEntities()) { if (model.CustomerID == 0) { db.Customers.Add(model); } else { db.Entry(model).State = System.Data.Entity.EntityState.Modified; } db.SaveChanges(); } Clear(); PopulateDataGridView(); MessageBox.Show("Saved Successfully..."); }