2011-07-28 38 views
2

我正在採取一些代碼,我已經用於嵌套列表視圖之前,並試圖使其與嵌套Repeater工作,但我得到一個錯誤。嵌套中繼器錯誤:對象引用未設置爲對象的實例

System.NullReferenceException:未將對象引用設置爲對象的實例。

的.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 

做我必須做一些事情的中繼器有什麼不同?

感謝您的任何幫助。

J.

+0

調試代碼。處理異常的錯誤的更多細節..! – Muse

回答

3

其標題列,第一次,所以你無法得到內心Repeater控件。應用檢查項目類型如下。

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 
+0

非常感謝Umar,我在嘗試類似的東西,但無法獲得正確的語法。 – JBoom

+0

我將如何解決此問題:http://stackoverflow.com/questions/26431759/how-to-populate-a-asp-repeater-from-calling-a-sql-stored-procedure-in-c-sharp。謝謝 – SearchForKnowledge

相關問題