Himanshu Tyagi

Himanshu Tyagi

  • NA
  • 119
  • 15.7k

How to find id using hiddenfield in gridview on button click

Feb 2 2018 3:35 AM
My update button is outside gridview
 
I am trying but its showing error "Object reference not set to an instance of an object." on "string strhfId = (gvRow.FindControl("hfId") as HiddenField).Value;"
 
I have tried different too
 
HiddenField hfId = (HiddenField)gridData.SelectedRow.Cells[0].FindControl("hfId");
 
but error is same
 
protected void lnkUpdate_Click(object sender, EventArgs e)
{
//HiddenField hfId = (HiddenField)gridData.SelectedRow.Cells[0].FindControl("hfId");   // Error is here
//HiddenField strId = ((HiddenField)gridData.Rows[0].FindControl("hfIdValue"));
//String value = strId.Value;
LinkButton btn = sender as LinkButton;
GridViewRow gvRow = btn.NamingContainer as GridViewRow;
string strhfId = (gvRow.FindControl("hfId") as HiddenField).Value;  // Error is here
try
{
//HiddenField hfIdc = (HiddenField)gridData.SelectedRow.Cells[0].FindControl("hfIdValue");
List parameters = new List
{
};
parameters.Add(new SqlParameter("@P_APPLICATION_ID", strhfId));
parameters.Add(new SqlParameter("@P_APPLICATION_NAME", txtApplicationName.Text));
parameters.Add(new SqlParameter("@P_MODE", "E"));
string strResponse = objAppFunctionModule.ExecuteNonQueryMethod(strUSP_APPLICATION_MST, parameters);
PopulateGrid();
}
catch (Exception ex)
{
lblResponse.Text = ex.Message;
}
}

Answers (7)