Click here to Skip to main content
15,888,401 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi!
I am a bit of newbie in system developing and I have get problem.
I cant find out why my code runs when I debug it (but I have to open quickwatch too), and not when I run it locally or in "without debug"-mode. It is like it need some kind of delay(?). But I really have to click and open the quickwatch to get a value to the ID. Otherwise it is null and I get an exception.

What I want to do is:
Get the ID (ctl103) of the autogenerated row in the table.
In the browser it looks like this:
id="ctl00_ContentMainPage_gridViewId_ctl03_Panel"

What I have tried:

Aspx code:
(I have removed what I think is to unnecessary code.)
ASP.NET
<div>
<!-- gridview -->
<asp:GridView ID="gridViewId" runat="server" AllowSorting="True" AutoGenerateColumns="False" CssClass="cssClass" CellPadding="3">
 <Columns>
  <asp:BoundField DataField="datafield1" HeaderText="datafield1" SortExpression="datafield1" ItemStyle-CssClass="HiddenCol">
  <asp:BoundField DataField="datafield2" HeaderText="datafield2">
  <asp:BoundField DataField="datafield3" HeaderText="datafield3">
  <asp:BoundField DataField="datafield4" HeaderText="datafield4">

  <asp:TemplateField>
    <ItemTemplate>
      <div>
<!-- collapsible panel inside the tabel -->
       <cc1:CollapsiblePanelExtender ID="CollapsiblePanelExtender1" runat="server"TargetControlID="StretchPanel" ExpandControlID="Panel1" CollapseControlID="Panel1" TextLabelID="Label1" Collapsed="true"> 
       </cc1:CollapsiblePanelExtender>
         <asp:Label ID="Label1" runat="server"></asp:Label>
         <asp:Panel ID="Panel1" runat="server">
         <asp:LinkButton ID="LinkButton" runat="server" ToolTip="pointer" CausesValidation="false" CommandArgument='<%# Container.DataItemIndex %>' OnCommand="LinkButton_Command">LinkbuttonName</asp:LinkButton>
          </asp:Panel>

          <%# AlterRow(Eval("Vägnr")) %>
          <asp:Panel ID="StretchPanel" runat="server">
<!-- ........more code....... -->

C# code
C#
protected void LinkButton_Command(object sender, CommandEventArgs e)        
{
            int rowIndex = Convert.ToInt32(e.CommandArgument);
            var id = e.CommandArgument.ToString();
            GridViewRow row = (gridViewId.Rows[int.Parse(id)]);
            var variable1 = row.Cells[0].Text;
            var variable2 = row.ID.ToString();    //here is where I got error 
                       //and need to open quickwatch to get an ID for the row

          // ......more code....


The exception message:
System.NullReferenceException: 'The object reference was not set to an instance of an object.'

Posted
Updated 5-Feb-20 1:12am

C#
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.
C#
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();    //here is where I got error 
                       //and need to open quickwatch to get an ID for the row

          // ......more code....
       }
       else
       {
          // Index is out-of-range.
       }
       // ......more code....
    }
    else
    {
       // e.CommandArgument is not a valid 32-bits integer's string representation
    }
    // ......more code....
}
 
Share this answer
 
v6
Comments
Richard Deeming 4-Feb-20 11:47am    
The previous line references row.Cells[0].Text, so unless the debugger is pointing to the wrong line, it's probably the row.ID which is null. :)
phil.o 4-Feb-20 11:52am    
Good point! :thumbsup:
I will update the solution.
Richard Deeming 4-Feb-20 12:11pm    
It does have an ID property, which it inherits from Control. But it almost certainly won't be set to anything. :)

Control.ID Property (System.Web.UI) | Microsoft Docs[^]
phil.o 4-Feb-20 12:25pm    
This answers my question :)
mmmariaaa 5-Feb-20 2:37am    
Thanks for your feedback! :)
I see now what you mean by taking the string to an integer and back again, it seems really stupid really, so I changed it, thanks!
But my problem with "var variable2 = rad.ID.ToString ();" still remains. I thought it had a value to ID. Because when I open quickwatch and look at both row and ID it really seems to have values, and the code works. But when I just run the code without looking at the quickwatch I got that error message.
This one seems to work for me now.

I wanted to get the ID so I could use it in another method where I had a FindControl(). And then I thought I could compare that ID with the FindControl(). But I found another way (see solution), there I jumped over the line that got me the error, and instead put FindControl() in an foreach loop that have called my own method GetList(find_all_the_data_I_want). I thought from the beginning that I needed to find out the specific ID for that road to check if it was a match, but I think that wouldnt be neccesary anymore. But still, I dont really know why I couldnt use the first exemple without debugging it.

C#
protected void LinkButton_Command(object sender, CommandEventArgs e)        
{
   int rowIndex = Convert.ToInt32(e.CommandArgument);
   if (rowIndex > -1 && rowIndex < gridViewId.Rows.Count)
   {
      GridViewRow row = gridViewId.Rows[rowIndex];
      var variable1 = row.Cells[0].Text;
      var variable2 = row.ID.ToString();    //here is where I got error 
                       //and need to open quickwatch to get an ID for the row

          // ......more code....

      foreach (Section section in _sectionManager.GetList(sectionId))
       {
         if (sectionId != 0)
         {
          ((TextBox)row.FindControl("TextBox1")).Text = section.TextBox1;
          ((TextBox)row.FindControl("TextBox2")).Text = section.TextBox2;
          ((TextBox)row.FindControl("TextBox3")).Text = section.TextBox3;
         }
       }
    }
    else
    {
          // Index is out-of-range.
    }
       // ......more code....
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900