Guest User

Guest User

  • Tech Writer
  • 271
  • 36.2k

Update data in Codefirst approach in asp.net entity framework

Sep 17 2020 2:51 AM
Hello Experts..
 
my scenario is update data in Code First approach in asp.net EF and I am using gridview to do CRUD operation..... I am stuck in Update operation...
 
I am using this scenario....
  1. protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)  
  2. {  
  3. GridViewRow row = GridView1.Rows[e.RowIndex];  
  4. TextBox txtName = row.FindControl("txtName"as TextBox;  
  5. TextBox txtDesignation = row.FindControl("txtDesignation"as TextBox;  
  6. using (DatabaseContext context = new DatabaseContext())  
  7. {  
  8. int Id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);  
  9. Designation obj = context.Designations.First(x => x.Id == Id);  
  10. obj.Name = txtName.Text;  
  11. obj.Designationn = txtDesignation.Text;  
  12. db.Entry(modal).State = EntityState.Modified;  
  13. context.SaveChanges();  
  14. bindGridview();  
  15. }  
  16. }  

Answers (3)