here in the 7th line i am unable to get selected index cell value pls help me to resolve.
C#
protected void lnkbtnVerify_Click(object sender, EventArgs e)
{
try
{
LinkButton btn = (LinkButton)sender;
GridViewRow Grow = (GridViewRow)btn.NamingContainer;
Label lblBeneName = (Label)Grow.FindControl("RowNo");
}
catch(Exception e)
{
throw e;
}
}
Naimish MakwanaRank #324 · 7.7K reputationPosted May 4, 2023, 7:40 AM EDT
Based on the code you have provided, it seems that you are trying to retrieve the value of a label control named "RowNo" in the selected GridView row when the user clicks on a LinkButton with the ID "lnkbtnVerify". However, you are encountering a null reference exception when trying to retrieve the GridView row index.
To fix this issue, you need to ensure that the LinkButton control is inside a GridView control and that the GridView has been populated with data. You can also check if the GridViewRow object returned by the NamingContainer property is not null before trying to access the controls inside it.
Here's an updated version of your code that includes these checks:
protected void lnkbtnVerify_Click(object sender, EventArgs e) { try { LinkButton btn = (LinkButton)sender; GridViewRow grow = (GridViewRow)btn.NamingContainer; // Check if GridViewRow object is not null if (grow != null) { int rowIndex = grow.RowIndex; Label lblBeneName = (Label)grow.FindControl("RowNo"); // Use rowIndex and lblBeneName as needed } } catch (Exception ex) { // Handle the exception here } }Rajkiran SwainRank #165 · 14.4K reputationPosted May 4, 2023, 7:47 AM EDT
protected void lnkbtnVerify_Click(object sender, EventArgs e) { try { LinkButton btn = (LinkButton)sender; GridView gv = (GridView)btn.NamingContainer.NamingContainer; // Get reference to the GridView control int rowIndex = ((GridViewRow)btn.NamingContainer).RowIndex; // Get the row index Label lblBeneName = (Label)gv.Rows[rowIndex].FindControl("RowNo"); // Find the control in the row } catch(Exception ex) { throw ex; } }