Editable GridView Add New Row throws NullReference

Xander Todor 81 Reputation points
2021-06-21T19:25:53.787+00:00

Hello/ I have an editable GridView but I would also like to have an add new row functionality, so I made a button using FooterTemplate for all editable fields and set CommandName="AddNew" . Everything looks as expected on the front end, but Add New Row cannot find txtGrantId, txtPotId or txtBudget at all and throws System.NullReferenceException 'Object reference not set to an instance of an object.'. The issue has to be in the code behind, but I am attaching the front end as well. The data source is pretty long but it does call OnRowCommand="gvPotsMoneyGrants_RowCommand". Why is it not binding?

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,231 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,153 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Xander Todor 81 Reputation points
    2021-06-21T19:26:33.097+00:00

    It did not let me add the code so I am adding it here.

    aspx:

    <%--Primary Key LinkId--%>
        <asp:TemplateField HeaderText="LinkId" SortExpression="LinkId">
            <ItemTemplate>
                <asp:Label ID="lblLinkId" runat="server" Text='<%# Eval("LinkId") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    
    <%--GrantId--%>
    <asp:TemplateField HeaderText="GrantId" SortExpression="GrantId">
        <ItemTemplate>
            <asp:Label ID="lblGrantIdMain" runat="server" Text='<%#Eval("GrantId") %>'></asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox ID="txtGrantId" runat="server" Text='<%#Bind("GrantId") %>'></asp:TextBox>
        </EditItemTemplate>
        <FooterTemplate>
            <asp:TextBox ID="inGrantId" Width="120px" runat="server" />
            <asp:RequiredFieldValidator ID="vGrantId" runat="server" ControlToValidate="inGrantId" Text="?" ValidationGroup="VG5" />
        </FooterTemplate>
    </asp:TemplateField>
    
    <%--PotId--%>
    <asp:TemplateField HeaderText="PotId" SortExpression="PotId">
        <ItemTemplate>
            <asp:Label ID="lblPotIdMain" runat="server" Text='<%#Eval("PotId") %>'></asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox ID="txtPotId" runat="server" Text='<%#Bind("PotId") %>'></asp:TextBox>
        </EditItemTemplate>
        <FooterTemplate>
            <asp:TextBox ID="inPotId" Width="120px" runat="server" />
            <asp:RequiredFieldValidator ID="vPotId" runat="server" ControlToValidate="inPotId" Text="?" ValidationGroup="VG5" />
        </FooterTemplate>
    </asp:TemplateField>
    
    <%--Budget--%>
    <asp:TemplateField HeaderText="Budget" SortExpression="Budget">
        <ItemTemplate>
            <asp:Label ID="lblBudgetMain" runat="server" Text='<%#Eval("Budget") %>'></asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox ID="txtBudget" runat="server" Text='<%#Bind("Budget") %>'></asp:TextBox>
        </EditItemTemplate>
        <FooterTemplate>
            <asp:TextBox ID="inBudget" Width="120px" runat="server" />
            <asp:RequiredFieldValidator ID="vBudget" runat="server" ControlToValidate="inBudget" Text="?" ValidationGroup="VG5" />
        </FooterTemplate>
    </asp:TemplateField>
    

    code behind:

    protected void gvPotsMoneyGrants_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("AddNew"))
            {
                TextBox txtGrantId = (TextBox)gvPotsMoneyGrants.FooterRow.FindControl("txtGrantId");
                TextBox txtPotId = (TextBox)gvPotsMoneyGrants.FooterRow.FindControl("txtPotId");
                TextBox txtBudget = (TextBox)gvPotsMoneyGrants.FooterRow.FindControl("txtBudget");
            string GrantId, PotId, Budget;
    
            GrantId = txtGrantId.Text;
            PotId = txtPotId.Text;
            Budget = txtBudget.Text;
    
            SqlCommand cmd = new SqlCommand();
            cmd.Parameters.AddWithValue("GrantId", GrantId);
            cmd.Parameters.AddWithValue("PotId", PotId);
            cmd.Parameters.AddWithValue("Budget", Budget);
    
        }
    }
    
    0 comments No comments

  2. Duane Arnold 3,211 Reputation points
    2021-06-22T00:29:55.947+00:00

    It most likely is the name of the controls you are trying to find on a page like txtPotID is not the name of the control when the HTML control is displayed from the DOM.

    While the page is being displayed with a browser, you need to use the browser's F12 Developer tool and look for the control on the page to find out the name of the control that was generated in the DOM.

    https://forum.katalon.com/t/how-to-use-the-browser-developer-tools-f12-devtools/34329

    0 comments No comments

  3. Yijing Sun-MSFT 7,061 Reputation points
    2021-06-22T02:39:43.69+00:00

    Hi @Xander Todor ,
    As far as I think,you can't find the textbox value in edit mode correctly.You need to find the textbox's row index .Just like this:

     GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);  
     TextBox txtGrantId = (TextBox)row.FindControl("txtGrantId");  
    

    If you can't solve the problems yourself,you could post full codes to us.
    Best regards,
    Yijing Sun


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  4. Elena Tsvetkova 1 Reputation point
    2022-10-06T15:10:03.703+00:00

    Hi XanderTodor-5936,

    The type of error System.NullReferenceException 'Object reference not set to an instance of an object.' in functional testing is usually thrown when the element defined in the code does not actually exist on the page, so the action cannot be sent to it. A good coding practice is to use an Assert statement after defining an element to check if that new variable is not null in test run-time.

    If you use Test Studio to record the UI web functional tests, you can rely on the DOM Explorer tab in the Advanced Recording Tools - there you can see the complete HTML output of the loaded page and find the attributes of each element. On top of this Test Studio automatically generates find expression for each added element by using the unique attributes of the HTML elements.

    I hope this can help you in setting up your automation tests.

    Cheers!

    0 comments No comments