[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Refresh sitemap in cache

Posted on 2009-02-17
4
Medium Priority
?
2,512 Views
Last Modified: 2012-05-06
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?
0
Comment
Question by:bjorkn
  • 2
  • 2
4 Comments
 
LVL 3

Expert Comment

by:Mahone7
ID: 23659204
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
 

Author Comment

by:bjorkn
ID: 23659295
I'm using  MS-ACCESS database,  I thought this worked only for  SQL-databases
0
 
LVL 3

Assisted Solution

by:Mahone7
Mahone7 earned 1000 total points
ID: 23659467
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
 

Accepted Solution

by:
bjorkn earned 0 total points
ID: 23668655
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses
Course of the Month19 days, 17 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question