Esempio n. 1
0
 private void btnSave_Click(object sender, EventArgs e)
 {
     /*dicelar mathod for all button*/
     model.FirstName = txtFirstName.Text.Trim();
     model.LastName  = txtLastName.Text.Trim();
     model.City      = txtCity.Text.Trim();
     model.Address   = txtAddress.Text.Trim();
     using (EFDBEntities db = new EFDBEntities())
     {
         /*------------------------*/
         if (model.StudentID == 0) //insert
         {
             db.Students.Add(model);
         }
         else //update
         {
             db.Entry(model).State = EntityState.Modified;
         }
         db.SaveChanges();
     }
     /* call clear funtion here*/
     Clear();
     PopulateDataGridView();
     MessageBox.Show("Well Comme!! Submitted Successfully");
 }
Esempio n. 2
0
 private void btnDelete_Click(object sender, EventArgs e)
 {
     if (MessageBox.Show("Are you sure you want to Delete record?", "EF CRUD Operation", MessageBoxButtons.YesNo) == DialogResult.Yes)
     {
         using (EFDBEntities db = new EFDBEntities())
         {
             var entry = db.Entry(model);
             if (entry.State == EntityState.Detached)
             {
                 db.Students.Attach(model);
             }
             db.Students.Remove(model);
             db.SaveChanges();
             PopulateDataGridView();
             Clear();
             MessageBox.Show("Deleted successfully by Redwan Omer");
         }
     }
 }