3
Answers

ASP.net Gridview operation using Entity Framework

I am performing Crud operation in asp.net using Entity framework
 
Everything thing is working fine but when i am updating the rows, it showing me null reference exception .
 
protected void datagrid1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = datagrid1.Rows[e.RowIndex];
int customerId = (int)(datagrid1.DataKeys[e.RowIndex].Value);
string firstname = (row.FindControl("txtFirstName") as TextBox).Text; //here i am getting exception, nullreference exception
string lastname = (row.FindControl("txtLastName") as TextBox).Text;
int age = Convert.ToInt32(row.FindControl("txtAge") as TextBox);
using (NORTHWNDEntities entities = new NORTHWNDEntities())
{
studentDetail customer = (from c in entities.studentDetails
where c.Id == customerId
select c).FirstOrDefault();

customer.FirstName =firstname;
customer.LastName= lastname;
customer.Age = age;
entities.SaveChanges();
}
datagrid1.EditIndex = -1;
GetStudent();
 
Any help or suggestion would be much appreciated
 
 
Answers (3)