Search code examples
c#asp.netuser-controls

Ascx DrodownList always gives error Object reference not set to an instance of an object while editing a GridView


I created a Gridview and tried to inline edit.i created separate dropdownList inside of EditItemTemplate.but for data binding for that dropdown always gives that error.

 Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:



           DdlCountry.DataSource = cachedData.Rows[0];
           DdlCountry.DataTextField = "CHANNEL_COL_NAME";
           DdlCountry.DataValueField = "CHANNEL_COL_NAME";


ascx page:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" EnableModelValidation="True"
    ShowFooter="True" Width="100%" OnRowDeleting="GridView1_RowDeleting" CssClass="grid" OnRowEditing="OnRowEditing"  OnRowCancelingEdit="GridView1_RowCancelingEdit" onrowdatabound="gv_RowDataBound" >
    <Columns>
        <asp:TemplateField HeaderText="Handling Order">
            <ItemTemplate>
                <asp:Label ID="ItemParmNameLabel" runat="server" ><%# Container.DataItemIndex +1 %></asp:Label>
            </ItemTemplate>
            <HeaderStyle Width="60px" VerticalAlign="Middle" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Channel">
            <FooterTemplate>
                <asp:DropDownList ID="ChannelColumnDropDownList" runat="server" Width="100%" ToolTip="Select the column to which you want to create a filter.">
                </asp:DropDownList>
            </FooterTemplate>

            <EditItemTemplate>  
                        <asp:DropDownList ID="ChannelColumnDropDownList1" runat="server" ></asp:DropDownList>  
             </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="ItemColumnLabel" runat="server" Text='<%# Eval("CHANNEL_COL_NAME") %>'></asp:Label>
            </ItemTemplate>
            <HeaderStyle Width="260px" VerticalAlign="Middle" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Employee">
            <FooterTemplate>
                <asp:DropDownList ID="EmployeeColumnDropDownList" Enabled="False" runat="server" Width="100%" ToolTip="Select the column to which you want to create a filter.">
                </asp:DropDownList>
            </FooterTemplate>
            <ItemTemplate>
                <asp:Label ID="ItemColumnLabe2" runat="server" Text='<%# Eval("EMP_COL_NAME") %>'></asp:Label>
            </ItemTemplate>
            <HeaderStyle Width="360px" VerticalAlign="Middle" />
        </asp:TemplateField>

        <asp:TemplateField>
            <FooterTemplate>
                <asp:ImageButton ID="cmdSelectRecipient" runat="server"  ImageUrl="~/Content/Images/empsearch.gif" UpdateMode="Always"/>
            </FooterTemplate>
            <HeaderStyle VerticalAlign="Middle"  Width="30px" />
        </asp:TemplateField>

        <asp:TemplateField>
            <ItemTemplate>
                <asp:ImageButton ID="ImageButton1" runat="server" CommandName="Delete" ImageUrl="~/Content/Images/grid_delete.gif" />
            </ItemTemplate>
            <HeaderStyle VerticalAlign="Middle" Width="60px" />
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:ImageButton ID="ItemEditImageButton" runat="server" ImageUrl="~/Content/Images/Edit.gif" CommandName ="Edit"
                    ToolTip="Edit channel Configurations" />
            </ItemTemplate>
             <EditItemTemplate>  
                        <asp:Button ID="btn_Update" runat="server" Text="Update" CommandName="Update"/>  
                        <asp:Button ID="btn_Cancel" runat="server" Text="Cancel" CommandName="Cancel"/>  
                    </EditItemTemplate>   
            <HeaderStyle VerticalAlign="Middle" Width="120px" />
        </asp:TemplateField>
    </Columns>
    <HeaderStyle HorizontalAlign="Left" />
</asp:GridView>

code behind for editing:

protected void OnRowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            DropDownList DdlCountry = GridView1.Rows[e.NewEditIndex].FindControl("ChannelColumnDropDownList1") as DropDownList;
            DataTable cachedData = null;
            cachedData = (DataTable)this.Session[c_SessionKey];
       
            DdlCountry.DataSource = cachedData.Rows[0];
            DdlCountry.DataTextField = "CHANNEL_COL_NAME";
            DdlCountry.DataValueField = "CHANNEL_COL_NAME";
            DdlCountry.DataBind();
        }

please help me to bind the values to DropdownList inside EditItemTemplate tags.


Solution

  • The answer is simple as of why, but I need more information to provide the solution

    The following line returns null as the Session[c_SessionKey] has a null value.

    cachedData = (DataTable)this.Session[c_SessionKey];
    

    Since null is a perfectly valid option for any reference type, that line does not break.

    Please provide the session code for more information.

    You can also try to avoid the exception with this:

    DdlCountry.DataSource = cachedData?.Rows[0];
    

    Bear in mind that this will set the DataSource to null as well.