question

XanderTodor-5936 avatar image
0 Votes"
XanderTodor-5936 asked YijingSun-MSFT answered

Editable GridView Add New Row throws NullReference

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?






dotnet-csharpdotnet-aspnet-webforms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

XanderTodor-5936 avatar image
0 Votes"
XanderTodor-5936 answered

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);
    
     }
 }


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DuaneArnold-0443 avatar image
0 Votes"
DuaneArnold-0443 answered

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

YijingSun-MSFT avatar image
0 Votes"
YijingSun-MSFT answered

Hi @XanderTodor-5936 ,
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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.