private void btnInsert_Click(object sender, EventArgs e) { model.First_Name = textFName.Text.Trim(); model.Last_Name = textLName.Text.Trim(); model.Birthday = textBirthday.Text.Trim(); model.City = textCity.Text.Trim(); model.Phone = textPhone.Text.Trim(); model.E_mail = textMail.Text.Trim(); using (RailwayDbEntities db = new RailwayDbEntities()) { if (model.CustomerID == 0) { db.Customers.Add(model); } else { db.Entry(model).State = EntityState.Modified; } db.SaveChanges(); } Clear(); PopulateDataGrid(); MessageBox.Show("Submitted Succesfully !"); }
private void btnDelete_Click(object sender, EventArgs e) { if (MessageBox.Show("Are you sure you want to delete ?", "Delete Operation", MessageBoxButtons.YesNo) == DialogResult.Yes) { using (RailwayDbEntities db = new RailwayDbEntities()) { var entry = db.Entry(model); if (entry.State == EntityState.Detached) { db.Customers.Attach(model); } db.Customers.Remove(model); db.SaveChanges(); PopulateDataGrid(); Clear(); MessageBox.Show("Deleted Successfully !"); } } }