Error while editing gridview

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SK2010
    New Member
    • Jul 2010
    • 6

    #1

    Error while editing gridview

    I have a gridview with edit and delete. When i click Edit, I donot get all the textboxes. I just get 1. on top of that, when I try to update anything in that textbox, I get object Reference not set to an instance of object.

    Can anyone please help me with this?

    my aspx for gridview is:
    Code:
        <asp:GridView ID="grdItems"  runat="server" AllowSorting="True"  
                   BackColor="White" BorderColor="Black" BorderStyle="Solid" BorderWidth="1px"  
                   CellPadding="4" ForeColor="Black" GridLines="Vertical" width="670px" OnRowCancelingEdit="grdItems_RowCancelingEdit" OnRowEditing="grdItems_RowEditing" OnRowUpdating="grdItems_RowUpdating" DataKeyNames="rowid" AutoGenerateColumns="False" OnRowDeleting="grdItems_RowDeleting" Font-Size="Smaller" ShowFooter="True" OnRowCreated="grdItems_RowCreated">  
                   <Columns>  
                   <asp:TemplateField HeaderText="ROWID" SortExpression="rowid" Visible="False">  
                           <EditItemTemplate>  
                               <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("rowid") %>'></asp:TextBox>  
                           </EditItemTemplate>  
                           <ItemTemplate>  
                              <asp:Label ID="lblRowID" runat="server" Text='<%# Bind("rowid") %>'></asp:Label>  
                          </ItemTemplate>  
                          <HeaderStyle Font-Size="Small" HorizontalAlign="Center" />  
                          <ItemStyle Font-Names="Arial" Font-Size="Smaller" />  
                      </asp:TemplateField>  
                        
                      <asp:TemplateField SortExpression="invItemDesc" HeaderText="DESCRIPTION">  
                      <HeaderTemplate>   
                       <table cellpadding="0" cellspacing="0" width="100%">     
                        <tr ><td colspan="4" align=center>DESCRIPTION</td></tr>   
                        </table>                   
                        <table cellpadding="0"   cellspacing="0" width="100%">    
                        <tr><td width="25%">MTN NO.</td><td width="25%">ACCOUNT NO.</td><td width="25%">INVOICE NO.</td><td width="25%">AMOUNT</td> </tr>     
                        </table>   
                        </HeaderTemplate>                      
                        <EditItemTemplate>  
                         <td width="25%"><asp:TextBox ID="txtMTN" runat="server" Text='<%# Eval("MTN") %>'></asp:TextBox></td>  
                         </EditItemTemplate>  
                         <EditItemTemplate>  
                         <td width="25%"><asp:TextBox ID="txtcAccNum" runat="server" Text='<%# Eval("cAccNum") %>'></asp:TextBox></td>  
                         </EditItemTemplate>  
                         <EditItemTemplate>  
                         <td width="25%"><asp:TextBox ID="txtinvNum" runat="server" Text='<%# Eval("summInvNum") %>'></asp:TextBox></td>  
                         </EditItemTemplate>                     
                         <EditItemTemplate>  
                         <td width="25%"><asp:TextBox ID="txtAmt" runat="server" Text='<%# Eval("invItemAmt") %>'></asp:TextBox></td>  
                         </EditItemTemplate>   
                         <ItemTemplate><table border="0" cellpadding="0" cellspacing="0" width="100%">     
                        <tr><td width="25%"><%# Eval("MTN")%></td><td width="25%"><%# Eval("cAccNum")%></td><td width="25%"><%# Eval("summInvNum")%></td><td width="25%"><%# Eval("invItemAmt")%></td> </tr>     
                        </table></ItemTemplate>    
                        <FooterTemplate>     
                        <table border="0" cellpadding="0" cellspacing="0" width="100%">     
                        <tr><td width="25%"></td><td width="25%"></td><td width="25%"></td><td width="25%">Total for MTN:</td> </tr>     
                        <tr><td width="25%"></td><td width="25%"></td><td width="25%"></td><td width="25%">Total Amt Due:</td> </tr>     
                        </table>     
                        </FooterTemplate>  
                   </asp:TemplateField>  
            <asp:TemplateField HeaderText = "Total Amount">   
            <ItemTemplate>  
            <asp:Literal ID = "AmountLiteral" runat="server"></asp:Literal>  
            </ItemTemplate>          
            <FooterTemplate> <table border="0" cellpadding="0" cellspacing="0" width="100%">         
            <tr><asp:Literal ID = "amountTotal"  runat = "server"></asp:Literal></tr>  
            <tr><asp:Literal ID = "amountTotal1"  runat = "server"></asp:Literal></tr>  
            </table>        
            </FooterTemplate>     
            </asp:TemplateField>     
                                       
                      <asp:CommandField ShowEditButton="True" ShowDeleteButton="True">  
                          <HeaderStyle Font-Size="Small" HorizontalAlign="Center" />  
                          <ItemStyle Font-Names="Arial" Font-Size="Smaller" />  
                      </asp:CommandField>                  
                  </Columns>                                    
                  <FooterStyle ForeColor="Black" />  
                  <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />  
                  <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />  
                  <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" /> 
                  <EditRowStyle BorderColor="Black" BorderStyle="Solid" />  
              </asp:GridView>
    My .cs for edit is:
    Code:
        protected void grdItems_RowEditing(object sender, GridViewEditEventArgs e)  
        {  
                grdItems.EditIndex = e.NewEditIndex;  
                populateItems();  
                  
                TextBox txtMTN = (TextBox)grdItems.Rows[e.NewEditIndex].Cells[1].FindControl("txtMTN");  
                txtMTN.Width = Unit.Pixel(75);  
                  
                TextBox txtAccNum = (TextBox)grdItems.Rows[e.NewEditIndex].Cells[2].FindControl("txtAccNum");  
               txtAccNum.Width = Unit.Pixel(75);  
                 
               TextBox txtSummInvNo = (TextBox)grdItems.Rows[e.NewEditIndex].Cells[3].FindControl("txtSummInvNo");  
               txtSummInvNo.Width = Unit.Pixel(75);  
                 
               TextBox txtAmt = (TextBox)grdItems.Rows[e.NewEditIndex].Cells[4].FindControl("txtAmt");  
               txtAmt.Width = Unit.Pixel(75);  
        }
    Last edited by Frinavale; Jul 23 '10, 07:31 PM. Reason: Removed line numbers from pasted code
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    There are a lot of errors in your code.
    First of all the reason why only one TextBox is being displayed is because you are supplying too many EditItemTemplat es for the template with the sortExpression= "invItemDes c".

    You should only be providing one EditItemTemplat e per TemplateField.

    You have a Table in the HeaderTemplate for the "invItemDes c" TemplateField.

    You have added a Table in the ItemTemplate that matches up with the one in the HeaderTemplate (Good).

    But you did not add a Table that matches up to the Table in the HeaderTemplate in the EditItemTemplat e section. (Bad)

    Instead of adding a Table, you added a whole bunch of EditItemTemplat es...obviously this isn't going to work. You should have added a table to the EditItemTemplat e that matches up with the one in the HeaderTemplate and contains the TextBoxes.

    Like this:
    Code:
    <asp:GridView ID="grdItems" runat="server" AllowSorting="True" BackColor="White"
        BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" CellPadding="4" ForeColor="Black"
        GridLines="Vertical" Width="670px" OnRowCancelingEdit="grdItems_RowCancelingEdit"
        OnRowEditing="grdItems_RowEditing" OnRowUpdating="grdItems_RowUpdating" DataKeyNames="rowid"
        AutoGenerateColumns="False" OnRowDeleting="grdItems_RowDeleting" Font-Size="Smaller"
        ShowFooter="True" OnRowCreated="grdItems_RowCreated"
        OnRowDataBound="grdItems_RowDataBound">
        <Columns>
            <asp:TemplateField HeaderText="ROWID" SortExpression="rowid" Visible="False">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("rowid") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblRowID" runat="server" Text='<%# Bind("rowid") %>'></asp:Label>
                </ItemTemplate>
                <HeaderStyle Font-Size="Small" HorizontalAlign="Center" />
                <ItemStyle Font-Names="Arial" Font-Size="Smaller" />
            </asp:TemplateField>
            <asp:TemplateField SortExpression="invItemDesc" HeaderText="DESCRIPTION">
                <HeaderTemplate>
                    <table cellpadding="0" cellspacing="0" width="100%">
                        <tr>
                            <td colspan="4" align="center">
                                DESCRIPTION
                            </td>
                        </tr>
                    </table>
                    <table cellpadding="0" cellspacing="0" width="100%">
                        <tr>
                            <td width="25%">
                                MTN NO.
                            </td>
                            <td width="25%">
                                ACCOUNT NO.
                            </td>
                            <td width="25%">
                                INVOICE NO.
                            </td>
                            <td width="25%">
                                AMOUNT
                            </td>
                        </tr>
                    </table>
                </HeaderTemplate>
                <EditItemTemplate>
                    <table cellpadding="0" cellspacing="0" width="100%">
                        <tr>
                            <td width="25%">
                                <asp:TextBox ID="txtMTN" runat="server" Text='<%# Eval("MTN") %>'></asp:TextBox>
                            </td>
                            <td width="25%">
                                <asp:TextBox ID="txtcAccNum" runat="server" Text='<%# Eval("cAccNum") %>'></asp:TextBox>
                            </td>
                            <td width="25%">
                                <asp:TextBox ID="txtinvNum" runat="server" Text='<%# Eval("summInvNum") %>'></asp:TextBox>
                            </td>
                            <td width="25%">
                                <asp:TextBox ID="txtAmt" runat="server" Text='<%# Eval("invItemAmt") %>'></asp:TextBox>
                            </td>
                    </table>
                </EditItemTemplate>
                <ItemTemplate>
                    <table border="0" cellpadding="0" cellspacing="0" width="100%">
                        <tr>
                            <td width="25%">
                                <%# Eval("MTN")%>
                            </td>
                            <td width="25%">
                                <%# Eval("cAccNum")%>
                            </td>
                            <td width="25%">
                                <%# Eval("summInvNum")%>
                            </td>
                            <td width="25%">
                                <%# Eval("invItemAmt")%>
                            </td>
                        </tr>
                    </table>
                </ItemTemplate>
                <FooterTemplate>
                    <table border="0" cellpadding="0" cellspacing="0" width="100%">
                        <tr>
                            <td width="25%">
                            </td>
                            <td width="25%">
                            </td>
                            <td width="25%">
                            </td>
                            <td width="25%">
                                Total for MTN:
                            </td>
                        </tr>
                        <tr>
                            <td width="25%">
                            </td>
                            <td width="25%">
                            </td>
                            <td width="25%">
                            </td>
                            <td width="25%">
                                Total Amt Due:
                            </td>
                        </tr>
                    </table>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Total Amount">
                <ItemTemplate>
                    <asp:Literal ID="AmountLiteral" runat="server"></asp:Literal>
                </ItemTemplate>
                <FooterTemplate>
                    <table border="0" cellpadding="0" cellspacing="0" width="100%">
                        <tr>
                            <asp:Literal ID="amountTotal" runat="server"></asp:Literal></tr>
                        <tr>
                            <asp:Literal ID="amountTotal1" runat="server"></asp:Literal></tr>
                    </table>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:CommandField ShowEditButton="True" ShowDeleteButton="True">
                <HeaderStyle Font-Size="Small" HorizontalAlign="Center" />
                <ItemStyle Font-Names="Arial" Font-Size="Smaller" />
            </asp:CommandField>
        </Columns>
        <FooterStyle ForeColor="Black" />
        <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
        <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
        <EditRowStyle BorderColor="Black" BorderStyle="Solid" />
    </asp:GridView>
    Fixing this will fix the problems with the "one TextBox only" displaying.

    But it doesn't fix your Null Reference Exception that you're getting (the "Object Reference not set to an instance of object." error)

    First of all, you shouldn't be attempting to access the TextBoxes in your OnEditing Event. They are not available at this point. You should be doing this during the OnRowDataBound event. Notice in the GridView markup that I just posted, that I added the code it needs in order to call the "grdItems_RowDa taBound" method during the OnRowDataBound event.

    So...In your RowEditing Event all you want to do is set the grdItems GridView's edit index to the index being edited:
    Code:
    protected void grdItems_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grdItems.EditIndex = e.NewEditIndex;
    }
    I realize that you want to set the Width of the TextBoxes using your C# code (for some weird reason...you could just simplify everything and set the Width property of the TextBoxes in the GridView markup).

    You do this in the RowDataBound event. This event occurs when data is bound to the rows. You just want to modify the controls when binding is happening for the row being edited...

    Then you want to use the FindControl method on the row (not the cells of the row) to retrieve the TextBox controls and set their width value.

    When you use the FindControl method you should ALWAYS make sure that the FindControl method was able to retrieve the control before you use it. If the FindControl method does not find the control that you are looking for it will return "null". When you try to access a property or method on a control that is "null" then you will get a Null Reference Exception (the "Object Reference not set to an instance of object." error)

    So, with all of this in mind your grdItems_RowDat aBound method should look like this:
    Code:
    protected void grdItems_RowDataBound(Object sender, GridViewRowEventArgs e)
    {
       if (grdItems.EditIndex > 0 && e.Row.RowIndex == grdItems.EditIndex)
       {
           GridViewRow rowBeingBound = e.Row;
    
            TextBox txtMTN = (TextBox)rowBeingBound.FindControl("txtMTN");
            if (txtMTN != null)
            {
                txtMTN.Width = Unit.Pixel(75);
            }
    
            TextBox txtAccNum = (TextBox)rowBeingBound.FindControl("txtAccNum");
            if (txtAccNum != null)
            {
                txtAccNum.Width = Unit.Pixel(75);
            }
    
            TextBox txtSummInvNo = (TextBox)rowBeingBound.FindControl("txtSummInvNo");
            if (txtSummInvNo != null)
            {
                txtSummInvNo.Width = Unit.Pixel(75);
            }
    
            TextBox txtAmt = (TextBox)rowBeingBound.FindControl("txtAmt");
            if (txtAmt != null)
            {
                txtAmt.Width = Unit.Pixel(75);
            }
        }
    }
    Now, when I tested the above code not all of the TextBox controls can be retrieved using the FindControl method. The reason is because the IDs for the TextBoxes that you are providing to FindControl method don't match with the IDs of the TextBoxes in the EditItemTemplat e. You will have to fix this.

    -Frinny
    Last edited by Frinavale; Jul 23 '10, 08:31 PM.

    Comment

    • SK2010
      New Member
      • Jul 2010
      • 6

      #3
      Thanks a lott for such a detailed explanation..I really appreciate your efforts on this.

      My this problem is fixed and I corrected all the code one by one and the errors were ofcourse what you said...:-)

      Comment

      Working...