var variable1 = row.Cells[0].Text;
var variable2 = row.ID.ToString();
System.NullReferenceException: 'The object reference was not set to an instance of an object.'
This means that the
ID
property of the
row
variable is null. It has not been initialized as you thought it would.
You can use debugging to find out why.
Update:
Re-reading the question, I realize the non-sense to tell you to use debug on a non-debug build.
I then wondered whether you are sure that the
GridViewRow Class (System.Web.UI.WebControls) | Microsoft Docs[
^] holds an
ID
property? Maybe you were thinking of
GridViewRow.RowIndex Property (System.Web.UI.WebControls) | Microsoft Docs[
^] instead? But then, this would be a compile-time issue.
One could also argue against your process consisting as taking the string representation of an integer, just for the sake of parsing it later to its original integer value. This is a much bigger issue and you should review this part carefully: the importance of datatypes and their usage in software development. Here, you do not need the
id
string variable. You already have the value elsewhere, stored with a much effective format.
protected void LinkButton_Command(object sender, CommandEventArgs e)
{
int rowIndex;
if (int.TryParse(e.CommandArgument, out rowIndex))
{
if (rowIndex > -1 && rowIndex < gridViewId.Rows.Count)
{
GridViewRow row = gridViewId.Rows[rowIndex];
var variable1 = row.Cells[0].Text;
var variable2 = row.ID.ToString();
}
else
{
}
}
else
{
}
}