Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello,

I have created a function to populate items in dropdownlist. But I am getting error
"Object reference not set to an instance of an object" 
What can wrong in below code

Code Behind:
C#
 protected void gvShowRequests_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            DropDownList gvddlRequestStatus = (DropDownList)e.Row.FindControl("gvddlRequestStatus");
            this.PopulateRequestStatus(gvddlRequestStatus);
        }


private void PopulateRequestStatus(DropDownList ddl)
        {
            DataTable requestStatus = (new RequestStore()).GetReqStatusList();
            
            ddl.DataSource = requestStatus;
            ddl.DataTextField = "Status";
            ddl.DataValueField = "Id";
            ddl.DataBind();

            if (ddl.Items.Count > 1)
            {
                ddl.Items.Insert(0, new ListItem(ApplicationConstants.dropdownDefaultValue, "0"));
                ddl.Items[0].Value = "0";
            }

        }



Code for gridview:
ASP.NET
<asp:GridView ID="gvShowRequests" runat="server" AllowPaging="true" PageSize="15" AutoGenerateColumns="false" Width="60%" AllowSorting="false" GridLines="Both" BorderColor="Black" PagerStyle-CssClass="pager" OnPageIndexChanging="gvShowRequests_PageIndexChanging" BorderStyle="Groove" OnRowDataBound="gvShowRequests_RowDataBound">
    <RowStyle CssClass="gvRows" />
    <AlternatingRowStyle CssClass="gvRowsalt" />
    <Columns>
    <asp:BoundField DataField="Id" HeaderText="Id" Visible="false" />
    <asp:BoundField DataField="Name" HeaderText="Application" />
    <asp:BoundField DataField="CostCentre" HeaderText="Cost Centre" />
    <asp:BoundField DataField="UserName" HeaderText="User Name" />
    <asp:BoundField DataField="Type" HeaderText="Request Type" />
    <asp:TemplateField HeaderText="Request Status--" SortExpression="Status">
     <ItemTemplate>
      <asp:DropDownList ID="gvddlRequestStatus" runat="server">
      </asp:DropDownList>
      </ItemTemplate>
      </asp:TemplateField>
      <asp:BoundField DataField="Status" HeaderText="Request Status" />
      <asp:BoundField DataField="SentBy" HeaderText="Sent By" />
      <asp:BoundField DataField="AddedOn" HeaderText="Added On" />
      </Columns>
      </asp:GridView>

When I run code in debug mode.. I get 'gvddlRequestStatus' is getting null value.
Somehow FindControl is not working.
Posted
Updated 5-Jan-12 23:17pm
v3
Comments
Sergey Alexandrovich Kryukov 6-Jan-12 4:21am    
No good -- nice people comment the line of code where this exception was thrown. After all, run it under debugger to see where you got the null value and try to de-reference it. Perhaps your question would not be needed.
--SA
Al Moje 6-Jan-12 4:25am    
try to change:
private void PopulateRequestStatus(DropDownList ddl)
to
protected void PopulateRequestStatus(DropDownList ddl)

1 solution

Please see my comment. Your approach to the question is wrong. You need to provide more complete information.

Now, it looks like you gvddlRequestStatus becomes null, because you FindControl returns null; and this is because the control is not found. The whole idea of using FindControl is completely wrong. Should you misspell or change the name of control — the compiler cannot detect a problem.

Look, the property Control.Name is not designed to use it in the code of applications, it's mostly used by the Designer. Why looking for a control by name if all the controls are accessed throw the variables?

—SA
 
Share this answer
 
Comments
tejashri.gandhi 6-Jan-12 4:34am    
Yes you are right.. "gvddlRequestStatus" becomes null..
I am sending this control name to method because there are more dropdowns which can use same function.

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



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