1

I'm using ASP.NET Repeater for dynamically generating HTML code and pagination. I use totally same code on two aspx pages and on one all works fine, but on the other one I get an Exception: Object reference not set to an instance of an object.

enter image description here

Now I understand why the exception occurred, it simply has a null value, repeater didn't render I guess. But does anyone know why this error happened and how it's possible that it works on one page, and doesn't work on the other? Where should I look for "real" problem?

This is my HTML code on the aspx page:

<div class="accordion-wrap projects">
<asp:Repeater runat="server" ID="projectRepeter">
<ItemTemplate>

    <div class="item">
        <div class="heading">
            <span>
                <asp:Label runat="server" ID="TeamMemberName" Text='<%#Eval("ProjectName")%>'></asp:Label></span><span><em>x</em></span>
            <i>+</i>
        </div>
        <div class="details">
            <ul class="form">
                <li>
                    <label>Project name:</label>
                    <input type="text" class="in-text" value="<%#Eval("ProjectName")%>" />
                </li>
                <li>
                    <label>Lead:</label>
                    <asp:DropDownList runat="server" DataSourceID="TeamMemberNameDS"></asp:DropDownList>
                </li>
            </ul>
            <ul class="form">
                <li>
                    <label>Description:</label>
                    <input type="text" class="in-text" value="<%#Eval("Description")%>" />
                </li>
            </ul>
            <ul class="form last">
                <li>
                    <label>Customer:</label>
                    <asp:DropDownList runat="server" DataSourceID="ClientNameDS"></asp:DropDownList>
                </li>
                <li class="inline">
                    <label>Status:</label>
                    <span class="radio">
                        <label for="inactive">Active:</label>
                        <input type="radio" value="1" name="status" id="inactive" />
                    </span>
                    <span class="radio">
                        <label for="active">Inactive:</label>
                        <input type="radio" value="2" name="status" id="active" />
                    </span>
                    <span class="radio">
                        <label for="active">Archive:</label>
                        <input type="radio" value="3" name="status" id="Radio1" />
                    </span>
                </li>
            </ul>
            <div class="buttons">
                <div class="inner">
                    <a href="javascript:;" class="btn green">Save</a>
                    <a href="javascript:;" class="btn red">Delete</a>
                </div>
            </div>
        </div>
    </div>

</ItemTemplate>
</asp:Repeater>
</div>
<div class="pagination">
<asp:Repeater ID="rptPagingProject" runat="server" OnItemCommand="rptPaging_ItemCommand">
<ItemTemplate>
    <asp:LinkButton ID="btnPage"
        CommandName="Page" CommandArgument="<%# Container.DataItem %>" runat="server" ForeColor="Black" Font-Bold="True"><%# Container.DataItem %>
    </asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
</div>

And this is the code behind:

protected void BindRepeater()
{
    string ConnectionString = "Data Source=PRACTICE-001;Initial Catalog=n.mosorinski;User ID=n.mosorinski;Password=n.mosorinski;MultipleActiveResultSets=True;Application Name=EntityFramework";
    SqlConnection con = new SqlConnection(ConnectionString);
    SqlCommand cmd = new SqlCommand("SELECT p.ProjectName, p.Description, c.ClientName FROM Project AS p INNER JOIN Client AS c ON p.CustomerID = c.ClientID", con);
    if (con.State == ConnectionState.Closed)
    {
        con.Open();
    }
    DataTable dt = new DataTable();
    SqlDataAdapter adp = new SqlDataAdapter(cmd);
    adp.Fill(dt);
    PagedDataSource pgitems = new PagedDataSource();
    DataView dv = new DataView(dt);
    pgitems.DataSource = dv;
    pgitems.AllowPaging = true;
    pgitems.PageSize = 5;
    pgitems.CurrentPageIndex = PageNumber;

    if (pgitems.PageCount > 1)
    {
        rptPagingProject.Visible = true;
        ArrayList pages = new ArrayList();
        for (int i = 0; i < pgitems.PageCount; i++)
            pages.Add((i + 1).ToString());
        rptPagingProject.DataSource = pages;
        rptPagingProject.DataBind();
    }
    else
    {
        rptPagingProject.Visible = false;
    }
    projectRepeter.DataSource = pgitems;
    projectRepeter.DataBind();
}

public int PageNumber
{
    get
    {
        if (ViewState["PageNumber"] != null)
            return Convert.ToInt32(ViewState["PageNumber"]);
        else
            return 0;
    }
    set
    {
        ViewState["PageNumber"] = value;
    }
}

protected void rptPaging_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    PageNumber = Convert.ToInt32(e.CommandArgument) - 1;
    BindRepeater();
}

}

nemo_87
  • 4,523
  • 16
  • 56
  • 102
  • I guess you can not do this way. If you want to use pagination with repeater control, then http://www.webblogsforyou.com/custom-datapagerrepeater-asp-net-repeater-control-with-datapager-example/ is the good example to do the same. – immayankmodi Nov 25 '14 at 12:33
  • Are you sure you have rptPagingProject on both pages and are you sure pgitems.PageCount > 1 on both pages? – paparazzo Nov 25 '14 at 13:41
  • @Blam On the one that is working PageCount is > 1, cause I have more than 5 results, on the one that's not working is equal to one cause there is less than five results. And yes rtpPagingProject is on both pages... I don't get it. I've tried the other example from the link in comment below from MM Tac and it's giving me the same. NullException when I try to access DataSource property... – nemo_87 Nov 25 '14 at 13:51
  • @MMTac I've just tried example you've shared with me, and it's giving me the same result. Exception cause Repeater gives null... – nemo_87 Nov 25 '14 at 13:51
  • Then on the other page it is not working fine. Take that out of the question as it has nothing to do with the problem. – paparazzo Nov 25 '14 at 14:15
  • possible duplicate of [Can't access control ID in code behind](http://stackoverflow.com/questions/1316757/cant-access-control-id-in-code-behind) – paparazzo Nov 25 '14 at 14:20
  • @nemo_87, It seems to be something changed in your page settings. Try all of those code with creating in another new sample page. – immayankmodi Nov 25 '14 at 14:47
  • @MMTac I solved problem by totally deleting page and creating new one. Now it's working... I guess that it was something about page settings :) – nemo_87 Nov 25 '14 at 15:20

0 Answers0