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();
}
{
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

Ravi PatelRank #439 · 5.7K reputationPosted Jun 3, 2016, 3:32 AM EDT
Vinay SinghRank #604 · 4K reputationPosted Jun 3, 2016, 3:36 AM EDT
Ashwanikumar singh31 reputationPosted Jun 4, 2016, 6:09 AM EDT