2

I am taking some code that I have used for a nested listview before and trying to make it work with a nested Repeater but I am getting an error.

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

.aspx

    <asp:Repeater ID="reMainNav" runat="server">
    <HeaderTemplate><ul></HeaderTemplate>
    <ItemTemplate><li><%# DataBinder.Eval(Container.DataItem, "name")%>

        <asp:Repeater ID="reSubNav" runat="server">
            <HeaderTemplate>
                <ul>
            </HeaderTemplate>
            <ItemTemplate>
                <li>
                    <%# DataBinder.Eval(Container.DataItem, "name")%>
                </li>
            </ItemTemplate>
            <FooterTemplate>
                </ul>
            </FooterTemplate>
        </asp:Repeater>

    </li></ItemTemplate>
    <FooterTemplate></ul></FooterTemplate>
</asp:Repeater>

.vb

    Protected Sub reMainNav_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles reMainNav.ItemDataBound

    Dim rowView As System.Data.DataRowView
    rowView = CType(e.Item.DataItem, System.Data.DataRowView)

    'database connection from web.config file
    Dim synergySQL As SqlConnection = New SqlConnection()
    synergySQL.ConnectionString = ConfigurationManager.ConnectionStrings("connSynergy").ConnectionString()

    'check if personal section complete
    Dim cmdSubNav As SqlCommand = New SqlCommand()
    cmdSubNav.Connection = synergySQL
    cmdSubNav.CommandText = "SELECT * FROM [subNavigation] WHERE [parentId] = " & rowView("id") & " ORDER BY [orderNo]"
    cmdSubNav.CommandType = CommandType.Text
    'data adapter
    Dim daSubNav As SqlDataAdapter = New SqlDataAdapter
    daSubNav.SelectCommand = cmdSubNav
    'data set
    Dim dsSubNav As DataSet = New DataSet()
    daSubNav.Fill(dsSubNav, "SubNav")

    Dim iSchedule As Integer
    iSchedule = dsSubNav.Tables(0).Rows.Count

    Dim reSubNav As Repeater = CType(e.Item.FindControl("reSubNav"), Repeater)
    reSubNav.DataSource = dsSubNav
    reSubNav.DataBind()

    synergySQL.Close()

End Sub

Do i have to do something different for a repeater?

Thanks for any help.

J.

1
  • Debug the code . Handle Exception for more detail on the error..!! Commented Jul 28, 2011 at 11:17

1 Answer 1

3

Its header row for first time so you are unable to get the inner repeater control. Apply check for item type as below.

Protected Sub reMainNav_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles reMainNav.ItemDataBound

 If e.Item.ItemType == ListItemType.Item OrElse e.Item.ItemType == ListItemType.AlternatingItem Then

    Dim rowView As System.Data.DataRowView
    rowView = CType(e.Item.DataItem, System.Data.DataRowView)

    'database connection from web.config file
    Dim synergySQL As SqlConnection = New SqlConnection()
    synergySQL.ConnectionString = ConfigurationManager.ConnectionStrings("connSynergy").ConnectionString()

    'check if personal section complete
    Dim cmdSubNav As SqlCommand = New SqlCommand()
    cmdSubNav.Connection = synergySQL
    cmdSubNav.CommandText = "SELECT * FROM [subNavigation] WHERE [parentId] = " & rowView("id") & " ORDER BY [orderNo]"
    cmdSubNav.CommandType = CommandType.Text
    'data adapter
    Dim daSubNav As SqlDataAdapter = New SqlDataAdapter
    daSubNav.SelectCommand = cmdSubNav
    'data set
    Dim dsSubNav As DataSet = New DataSet()
    daSubNav.Fill(dsSubNav, "SubNav")

    Dim iSchedule As Integer
    iSchedule = dsSubNav.Tables(0).Rows.Count

    Dim reSubNav As Repeater = CType(e.Item.FindControl("reSubNav"), Repeater)
    reSubNav.DataSource = dsSubNav
    reSubNav.DataBind()

    synergySQL.Close()

End If

End Sub
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Umar, i was trying something similar but couldn't get syntax correct.
How would I solve this: stackoverflow.com/questions/26431759/…. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.