ASP.NET Elements are null when assigning data source

Posted by deccks on Stack Overflow See other posts from Stack Overflow or by deccks
Published on 2012-12-15T19:30:54Z Indexed on 2012/12/15 23:03 UTC
Read the original article Hit count: 227

Filed under:
|
|

For some reason, all of the objects in my ASP.NET markup are now null when I try to assign values to their properties in the code behind.

My project was going fine and then now when I try to assign a data source to a GridView, I get a null reference error.

I have no idea why it's doing this. I am not doing nothing special. I am just trying to assign a value to a property to an asp.net element in on the page. The intellisense knows that the element is there and I get no errors when I build the project. It's just when I am running the website I get the null reference.

I have been trying to fix this issue for a couple weeks now. Please Help.

Thanks.

Here is the code:

protected void Page_PreRender(object sender, EventArgs e)
{
    LoadData();
}

private void LoadData()
{
    Entities context = new Entities();
    var types = (from t in context.CustomerTypes
                 select t).OrderBy(t => t.TypeName);

    gvCustomerTypes.DataSource = types;
    gvCustomerTypes.DataBind();

}

and on in the markup the gridview looks like this:

<asp:GridView ID="gvCustomerTypes" runat="server" ShowHeader="true" GridLines="Both"
    AutoGenerateColumns="false" AlternatingRowStyle-BackColor="AliceBlue" Width="100%">
    <Columns>
        <asp:TemplateField HeaderText="Customer Type Name" HeaderStyle-HorizontalAlign="Left"
            ItemStyle-HorizontalAlign="Left">
            <ItemTemplate>
                <asp:Label ID="lblType" runat="server" Text='<%# Eval("TypeName") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Edit" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <asp:HyperLink ID="HyperLink1" NavigateUrl='<%#Eval("CustomerTypeID", "CreateEditCustomerType.aspx?ID={0}") %>'
                    Text="Edit" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Delete" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <asp:LinkButton ID="LinkButton1" CommandName='<%#Eval("CustomerTypeID") %>' OnClientClick="javascript:return confirm('Are you sure you want to delete this Customer Type?');"
                    OnCommand="DeleteCustomerType" Text="Delete" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET