Reputation: 2410
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.
Upvotes: 2
Views: 2976
Reputation: 1450
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
Upvotes: 3