Esempio n. 1
0
 private void btnDelete_Click(object sender, EventArgs e)
 {
     if (MessageBox.Show("Are you  sure to Delete this Record ?", "EF CURD operation ", MessageBoxButtons.YesNo) == DialogResult.Yes)
     {
         ;
     }
     using (EFDBEntities db = new EFDBEntities())
     {
         var entry = db.Entry(model);
         if (entry.State == EntityState.Detached)
         {
             db.Customers.Attach(model);
         }
         db.Customers.Remove(model);
         db.SaveChanges();
         PopulateDateGridView();
         Clear();
     }
 }
Esempio n. 2
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            model.FirstName = textBox1.Text.Trim();
            model.LastName  = textBox2.Text.Trim();
            model.City      = textBox3.Text.Trim();
            model.Address   = textBox4.Text.Trim();
            using (EFDBEntities db = new EFDBEntities())
            {
                if (model.CustomerID == 0) //insert
                {
                    db.Customers.Add(model);
                }

                else         //update
                {
                    db.Entry(model).State = EntityState.Modified;
                }

                db.SaveChanges();
            }
            Clear();
            PopulateDateGridView();
            MessageBox.Show("Submitted successfully");
        }