Refresh sitemap in cache

I'm building a small CMS-system where the source for the sitemap is stored in an access-database.  I'm using an acces-sitemapprovider and I would like to refresh the sitemap in cache after changes have been made to the database.   Can anyone help me with that?
bjorknAsked:
Who is Participating?
 
bjorknAuthor Commented:
I'm still having problems with this

I update the treeview,  update the database and then call refresh,  but the sitemap don't get refreshed

Here is the code where I call the refresh   and the accesssitemapprovider

Can you see what I'm doing wrong.
   private void UpdateNodes()
    {
        _connect = GetConnectionString();
        OleDbConnection connection = new OleDbConnection(_connect);
 
        //OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;DataSource=" + Server.MapPath("app_data/sitemap.mdb"));
        connection.Open();
        IList<RadTreeNode> allNodes = RadTreeView1.GetAllNodes();
        for (int i = 0; i < allNodes.Count; i++)
        {
            RadTreeNode node = (RadTreeNode)allNodes[i];
            int parentId = node.ParentNode == null ? -1 : allNodes.IndexOf(node.ParentNode);
            command = new OleDbCommand("INSERT into sitemap([nodeid],parentnodeid,[name],url) values (@ID,@parentId,@Text,@url)", connection);
            command.Parameters.AddWithValue("nodeid", i.ToString());
            if (parentId > -1)
                command.Parameters.AddWithValue("parentnodeid", parentId);
            else
                command.Parameters.AddWithValue("parentnodeid", System.DBNull.Value);
            command.Parameters.AddWithValue("parentnodeid", node.Text);
            command.Parameters.AddWithValue("url", node.NavigateUrl);
            command.ExecuteNonQuery();
        }
        connection.Close();
        //update sitemap
        AccessSiteMapProvider siteMapProvider = (AccessSiteMapProvider)SiteMap.Provider;
 
 
        if (siteMapProvider != null)
        {
 
            siteMapProvider.Refresh();
 
        }
        //AccessSiteMapProvider sitemap = new AccessSiteMapProvider();  virkar ekki
        //SiteMapNode s = sitemap.RootNode;
    }
 
 
 
-----------------------------------------------------------------------
 
 
And here is the AccessSitemapprovider:
 
 
 
namespace Samples.AspNet.CS.Controls
{
 
    using System;
    using System.Collections;
    using System.Collections.Specialized;
    using System.Data;
    using System.Data.OleDb;
    using System.Security.Permissions;
    using System.Web;
 
    /// An extremely simple AccessSiteMapProvider that only supports a
    /// site map node hierarchy 1 level deep.
    /// 
 
   
 
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
    public class AccessSiteMapProvider : StaticSiteMapProvider
    {
 
 
        public interface IRefreshable
        {
 
            void Refresh();
 
 
        }
 
 
        private SiteMapNode rootNode = null;
        private OleDbConnection accessConnection = null;
 
        // This string is case sensitive.
        private string AccessConnectionStringName = "connectionString";
 
        // Implement a default constructor.
        public AccessSiteMapProvider() { }
 
        // Some basic state to help track the initialization state of the provider.
        private bool initialized = false;
        public virtual bool IsInitialized
        {
            get
            {
                return initialized;
            }
        }
        // Return the root node of the current site map.
        public override SiteMapNode RootNode
        {
            get
            {
                SiteMapNode temp = null;
                temp = BuildSiteMap();
                return temp;
            }
        }
        protected override SiteMapNode GetRootNodeCore()
        {
            return RootNode;
        }
        // Initialize is used to initialize the properties and any state that the
        // AccessProvider holds, but is not used to build the site map.
        // The site map is built when the BuildSiteMap method is called.
        public override void Initialize(string name, NameValueCollection attributes)
        {
            if (IsInitialized)
                return;
 
            base.Initialize(name, attributes);
 
            // Create and test the connection to the Microsoft Access database.
 
            // Retrieve the Value of the Access connection string from the
            // attributes NameValueCollection.
            string connectionString = attributes[AccessConnectionStringName];
 
            if (null == connectionString || connectionString.Length == 0)
                throw new Exception("The connection string was not found.");
            else
                accessConnection = new OleDbConnection(connectionString);
 
            initialized = true;
        }
 
        ///
        /// SiteMapProvider and StaticSiteMapProvider methods that this derived class must override.
        ///
        // Clean up any collections or other state that an instance of this may hold.
        protected override void Clear()
        {
            lock (this)
            {
                rootNode = null;
                base.Clear();
            }
        }
 
        // Build an in-memory representation from persistent
        // storage, and return the root node of the site map.
        public override SiteMapNode BuildSiteMap()
        {
 
            // Since the SiteMap class is static, make sure that it is
            // not modified while the site map is built.
            lock (this)
            {
 
                // If there is no initialization, this method is being
                // called out of order.
                if (!IsInitialized)
                {
                    throw new Exception("BuildSiteMap called incorrectly.");
                }
 
                // If there is no root node, then there is no site map.
                if (null == rootNode)
                {
                    // Start with a clean slate
                    Clear();
 
                    // Select the root node of the site map from Microsoft Access.
                    int rootNodeId = -1;
 
                    if (accessConnection.State == ConnectionState.Closed)
                        accessConnection.Open();
                    OleDbCommand rootNodeCommand =
                        new OleDbCommand("SELECT nodeid, url, name FROM SiteMap WHERE parentnodeid IS NULL",
                                         accessConnection);
                    OleDbDataReader rootNodeReader = rootNodeCommand.ExecuteReader();
 
                    if (rootNodeReader.HasRows)
                    {
                        rootNodeReader.Read();
                        rootNodeId = rootNodeReader.GetInt32(0);
                        // Create a SiteMapNode that references the current StaticSiteMapProvider.
                        rootNode = new SiteMapNode(this,
                                                     rootNodeId.ToString(),
                                                     rootNodeReader.GetString(1),
                                                     rootNodeReader.GetString(2));
 
                    }
                    else return null;
 
                    rootNodeReader.Close();
                    AddChildren(rootNodeId,rootNode);
 
                    
                    accessConnection.Close();
                }
                return rootNode;
            }
        }
 
 
        private void AddChildren(int rootNodeId, SiteMapNode rootChildNode)
        {
 
                    // Select the child nodes of the root node.
                    OleDbCommand childNodesCommand =
                        new OleDbCommand("SELECT nodeid, url, name FROM SiteMap WHERE parentnodeid = ?",
                                         accessConnection);
                    OleDbParameter rootParam = new OleDbParameter("parentid", OleDbType.Integer);
                    rootParam.Value = rootNodeId;
                    childNodesCommand.Parameters.Add(rootParam);
 
                    OleDbDataReader childNodesReader = childNodesCommand.ExecuteReader();
 
                    if (childNodesReader.HasRows)
                    {
 
                        SiteMapNode childNode = null;
                        
                        while (childNodesReader.Read())
                        {
                            rootNodeId = childNodesReader.GetInt32(0);
                            childNode = new SiteMapNode(this,
                                                         childNodesReader.GetInt32(0).ToString(),
                                                         childNodesReader.GetString(1),
                                                         childNodesReader.GetString(2));
 
                            // Use the SiteMapNode AddNode method to add
                            // the SiteMapNode to the ChildNodes collection.
                            AddNode(childNode, rootChildNode);
                               // Check for children in this node.
			                AddChildren(rootNodeId,childNode);
                            
                        }
                        childNodesReader.Close();
                    }
	             
                    
 
}
        public void Refresh()
        {
            Clear();
            //nodes.Clear();
            rootNode = null;
        } 
 
 
 
        }
    }

Open in new window

0
 
Mahone7Commented:
You Can See this to use sqlCacheDependency , in this way u can refresh the site map if the underlying database changes

http://msdn.microsoft.com/en-us/magazine/cc163657.aspx
0
 
bjorknAuthor Commented:
I'm using  MS-ACCESS database,  I thought this worked only for  SQL-databases
0
 
Mahone7Commented:
implement an IRefreshable interface will be a good choice

this interface contain this code

public interface IRefreshable

{

void Refresh();

}


you can implements it with this code for example

public void Refresh()
{
Clear();
nodes.Clear();
root = null;
}

and when you need to refresh it just call the method refresh()

you can see this to more information : http://www.dotneteer.com/Weblog/post/2006/02/An-alternative-way-to-refresh-SiteMap-with-SQLSiteMapProvider.aspx

this works for all sitemap providers

cya
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.