2

I'm working on my first ever ASP.Net project, and I seem to have been thrown in the deep end right away. All of my site files are stored in a database, so I'm using a VirtualPathProvider to access them. That part works well, but then I needed a site map that would dynamically pick up all the files since the user will add/delete files as they go. To do that, I created a class that inherits StaticSiteMapProvider, created a SiteMapDataSource on my Master Page, and a TreeView to use it. For the most part, this works. When you load the page you see the TreeView and it contains all of the nodes it is supposed to. However, every now and then the TreeView is completely blank -- no nodes at all. I even copied the TreeView into a content page. The content page TreeView always works, even when the MasterPage doesn't. (Also I didn't know a control on a content page could use a data source on a master page until I forgot to copy the data source!)

Trying to debug that, I put a throw exception in my BuildSiteMap() function in the SiteMapProvider just to prove to myself that the code was actually running. What I found was that the exception was hit every single time. However, the stack trace wasn't the same every time.

Most of the time, this is what I got:

[Exception: Test]
    ADEM.clsSiteMap.BuildSiteMap() in c:\inetpub\wwwroot\App_Code\clsSiteMap.vb:49
    System.Web.StaticSiteMapProvider.GetChildNodes(SiteMapNode node) +54
    System.Web.SiteMapNode.get_ChildNodes() +27
    System.Web.UI.WebControls.SiteMapDataSource.GetNodes(SiteMapNode node) +52
    System.Web.UI.WebControls.SiteMapDataSource.GetNodes() +329
    System.Web.UI.WebControls.SiteMapDataSource.GetTreeView(String viewPath) +35
    System.Web.UI.WebControls.SiteMapDataSource.GetHierarchialView(String viewPath) +32
    System.Web.UI.HierarchialDataSourceControl.System.Web.UI.IHierarchialDataSource.GetHierarchialView(String viewPath) +10
    System.Web.UI.WebControls.HierarchialDataBoundControl.GetData(String viewPath) +25
    System.Web.UI.WebControls.TreeView.DataBindNode(TreeNode node) +73
    System.Web.UI.WebControls.TreeView.PerformDataBinding() +120
    System.Web.UI.WebControls.HierarchicalDataBoundControl.PerformSelect() +85
    System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +73
    System.Web.UI.WebControls.TreeView.DataBind() +4
    System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +82
    System.Web.UI.WebControls.BaseDataBoundControl.OnPreRender(EventArgs e) +22
    System.Web.UI.WebControls.TreeView.OnPreRender(EventArgs e) +36
    System.Web.UI.Control.PreRenderRecursiveInternal() +80
    System.Web.UI.Control.PreRenderRecursiveInternal() +171
    System.Web.UI.Control.PreRenderRecursiveInternal() +171
    System.Web.UI.Control.PreRenderRecursiveInternal() +171
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +842

However, about as often as the TreeView would come up blank, I got this:

    [Exception: Test]
    ADEM.clsSiteMap.BuildSiteMap() in c:\inetpub\wwwroot\App_Code\clsSiteMap.vb:49
    System.Web.StaticSiteMapProvider.FindSiteMapNode(String rawUrl) +133
    System.Web.SiteMapProvider.FindSiteMapNode(HttpContext context) +54
    System.Web.SiteMapProvider.get_CurrentNode() +35
    System.Web.UI.WebControls.TreeView.DataBindNode(TreeNode node) +219
    System.Web.UI.WebControls.TreeView.PerformDataBinding() +120
    System.Web.UI.WebControls.HierarchicalDataBoundControl.PerformSelect() +85
    System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +73
    System.Web.UI.WebControls.TreeView.DataBind() +4
    System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +82
    System.Web.UI.WebControls.BaseDataBoundControl.OnPreRender(EventArgs e) +22
    System.Web.UI.WebControls.TreeView.OnPreRender(EventArgs e) +36
    System.Web.UI.Control.PreRenderRecursiveInternal() +80
    System.Web.UI.Control.PreRenderRecursiveInternal() +171
    System.Web.UI.Control.PreRenderRecursiveInternal() +171
    System.Web.UI.Control.PreRenderRecursiveInternal() +171
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +842

I guess the useful bit of code would be the BuildSiteMap function:

Public Overrides Function BuildSiteMap() As System.Web.SiteMapNode
    Dim node As SiteMapNode = Nothing

    SyncLock Me
        node = TryCast(HttpRuntime.Cache("SiteMap"), SiteMapNode)
        If node Is Nothing Then
            MyBase.Clear()
            Throw New Exception("Test")
            node = New SiteMapNode(Me, "FO1", "default.cnt", "Home")

            AddNode(node)
            siteRoot = node
            AddFolders(node)
            AddFiles(node)
            HttpRuntime.Cache.Insert("SiteMap", node, New SiteMapCacheDependency())
        End If

        Return node
    End SyncLock
End Function

AddFolders() and AddFiles() just do more of the same, so for brevity's sake I'll leave them out unless someone thinks they are important.

I tried to post the code for the treeview here, but it seems the site doesn't really like that. I will say that I'm setting the DataSourceID property instead of assigning it at runtime. I tried it both ways, doesn't seem to make a difference.

I'm not really sure what else might be useful, so just let me know if you need any more details.

Now I'm left wondering if this could be a clue to my problem or if it's just a coincidence. If it's a clue, I'm too dense to figure it out. Does any of this make sense to anyone else?

3
  • If you have any source code that is pertinent to your error, it might helpful to post it also.
    – Jagd
    Sep 18, 2009 at 20:38
  • I added some code. This one has me so stumped I'm at a bit of a loss to even figure out what details might be useful, so please let me know if you can think of anything else that might help.
    – user175751
    Sep 21, 2009 at 14:14
  • I thought it might be something to do with the cache, so I took that out and had it build it every time, but that still didn't help.
    – user175751
    Sep 21, 2009 at 15:09

1 Answer 1

0

As it turns out, the problem was in a bit of code that I didn't think was worth posting. It seems that every example I've found does something like this:

 Protected Overrides Function GetRootNodeCore() As System.Web.SiteMapNode
     Return siteRoot
 End Function

It turns out that GetRootNodeCore() gets called pretty much right out of the gate, so sometimes siteRoot is nothing. So the solution is as follows:

Protected Overrides Function GetRootNodeCore() As System.Web.SiteMapNode
    If siteRoot Is Nothing Then
        SyncLock Me
            BuildSiteMap()
        End SyncLock
    End If

    Return siteRoot
End Function

I won't take credit for finding that on my own, someone pointed it out to me here: http://forums.asp.net/p/1473236/3420805.aspx#3420805

Your Answer

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