0

Hi,
I'm trying to populate an asp:menu from a database on my master page and keep getting the error:
Object reference not set to an instance of an object

Here's my code on the master page:

<asp:Label runat="server" ID="errLabel" />
<asp:Menu ID="Categories" runat="server" CssClass="menu" EnableViewState="false" Orientation="Vertical" DataSourceID="categoryListSrc"></asp:Menu>
<asp:XmlDataSource ID="categoryListSrc" runat="server" TransformFile="~/TransformXSLT.xslt" XPath="MenuItems/MenuItem"></asp:XmlDataSource>

And here's the code behind:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim ds As New DataSet()
        Dim sqlStr As String
        Dim oMySqlConn As MySqlConnection = New MySqlConnection()
        oMySqlConn.ConnectionString = "User ID=myid;" & _
                                     "Password=mypass;" & _
                                     "Host=localhost;" & _
                                     "Port=3306;" & _
                                     "Database=mydb;" & _
                                     "Pooling=true;" & _
                                     "Min Pool Size=0;" & _
                                     "Max Pool Size=100;" & _
                                     "Connection Lifetime=0"

        sqlStr = "SELECT * FROM tbl_category"
        Dim dbcomm As New MySqlCommand(sqlStr, oMySqlConn)
        'dbcomm.Connection.Open()

        dbcomm = oMySqlConn.CreateCommand
        dbcomm.CommandText = sqlStr

        Dim da As New MySqlDataAdapter
        Try

            da.SelectCommand = dbcomm
            da.Fill(ds)
            da.Dispose()

            ds.DataSetName = "Menus"
            ds.Tables(0).TableName = "Categories"

            Dim parentColumn As DataColumn = ds.Tables("Categories").Columns("id")
            Dim childColumn As DataColumn = ds.Tables("Categories").Columns("parent")

            Dim relation As DataRelation
            relation = New DataRelation("ParentChild", parentColumn, childColumn)

            relation.Nested = True
            ds.Relations.Add(relation)

            categoryListSrc.Data = ds.GetXml()

        Catch ex As Exception
            errLabel.Text = ex.Message.ToString
        End Try

    End Sub

I think I'm missing something totally obvious but I can't see it.

3
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by dnanetwork
0

Fixed it. There was an error in the data giving a circular reference and the

ds.GetXML

call was outputting the error message rather than the XML code resulting in the error as it tried to bind a non-existant datasource to the menu.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.