Click here to Skip to main content
15,901,666 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I m working with web parts in asp.net and I want to save my personal setting into db(sqlerver)

how to use these methods or others which are present in sqlprovider
1) SavePersonalizationBlob
2) LoadPersonalizationBlobs



etc
Waiting and need help help it humble request :confused: :confused:
Posted
Comments
goradaranaresh 12-Aug-10 3:24am    
Hi, Are you storing personalization data to aspnetdb database? Is there any way to store to store it to other database?

1 solution

You need to create a Table in sql server with following field.

UserName	 Varchar(50)	PK, Unique identifier for User
Page	         Varchar(255)	PK, Path and File Name of the Page
Personalization	 Image		Personalization data


Then you have to create a custom personalization provider class which will implement PersonalizationProvider class.

You can use the following code, I have created the class which will consume WCF service, so I will remove those parts from the code and you can replace it with simple sql statemetns to read/write from sql server table that you have just created.


C#
using System;
using System.Configuration.Provider;
using System.Security.Permissions;
using System.Web;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Specialized;
using System.Security.Cryptography;
using System.Text;
using System.IO;
namespace YourApplicationName
{
    public class MyCustomPersonalizationProvider : PersonalizationProvider
    {
        private string m_ApplicationName;
        public override string ApplicationName
        {
            get { return m_ApplicationName; }
            set { m_ApplicationName = value; }
        }
        private string m_ConnectionStringName;
        public string ConnectionStringName
        {
            get { return m_ConnectionStringName; }
            set { m_ConnectionStringName = value; }
        }
        public override void Initialize(string name, NameValueCollection config)
        {
            // Verify that config isn't null
            if (config == null)
                throw new ArgumentNullException("config");
            // Assign the provider a default name if it doesn't have one
            if (String.IsNullOrEmpty(name))
                name = "MyCustomPersonalizationProvider";
            // Add a default "description" attribute to config if the
            // attribute doesn't exist or is empty
            if (string.IsNullOrEmpty(config["description"]))
            {
                config.Remove("description");
                config.Add("description", "My Custom Personalization Provider for Portal");
            }
            // Call the base class's Initialize method
            base.Initialize(name, config);
            if (string.IsNullOrEmpty(config["connectionStringName"]))
            {
                throw new ProviderException
                    ("ConnectionStringName property has not been specified");
            }
            else
            {
                m_ConnectionStringName = config["connectionStringName"];
                config.Remove("connectionStringName");
            }
            if (string.IsNullOrEmpty(config["applicationName"]))
            {
                throw new ProviderException
                    ("applicationName property has not been specified");
            }
            else
            {
                m_ApplicationName = config["applicationName"];
                config.Remove("applicationName");
            }
            // Throw an exception if unrecognized attributes remain
            if (config.Count > 0)
            {
                string attr = config.GetKey(0);
                if (!String.IsNullOrEmpty(attr))
                    throw new ProviderException
                        ("Unrecognized attribute: " + attr);
            }
        }
        protected override void LoadPersonalizationBlobs(WebPartManager webPartManager, string path, string userName, ref byte[] sharedDataBlob, ref byte[] userDataBlob)
        {
            // Load shared state
            sharedDataBlob = null;
            userDataBlob = null;
            object userBlobDataObject = null;
            try
            {
                    System.Data.DataTable dt = // read personaliztion data from the table "Where tablename.UserName = userName"
                    if (dt.Rows.Count > 0)
                        userBlobDataObject = dt.Rows[0].ItemArray[0];
                    if (userBlobDataObject != null && userBlobDataObject != DBNull.Value)
                        userDataBlob =
                            (byte[])userBlobDataObject;
            }
            catch (Exception ex)
            {
            // write to event log...
             //   MyErrorHandling.WriteEventLog(System.Diagnostics.EventLogEntryType.Error,
                    "Dashboard",
                    MyErrorHandling.FormatErrorMessage(userName,
                    "Custom Personalization Provider",
                    "LoadPersonalizationBlobs()", ex.ToString()), 2);
            }
        }
        protected override void ResetPersonalizationBlob(WebPartManager webPartManager, string path, string userName)
        {
            // Delete the specified personalization file
            try
            {
            // delete row or set the personalization = null from talbe, "where UserName = useName" (SQL Statement)
            }
            catch (Exception ex)
            {
            // write to event log
                //MyErrorHandling.WriteEventLog(System.Diagnostics.EventLogEntryType.Error,
                    "Dashboard",
                    MyErrorHandling.FormatErrorMessage(userName,
                    "Custom Personalization Provider",
                    "ResetPersonalizationBlobs()", ex.ToString()), 2);
            }
        }
        protected override void SavePersonalizationBlob(WebPartManager webPartManager, string path, string userName, byte[] dataBlob)
        {
            try
            {
                // save the dataBlob to the personalization column in the table, where UserName = userName. also save path.
                // here you need to check if the row is already there for the userName in the sql server table, if not insert new row otherwise update the existing values.
            }
            catch (Exception ex)
            {
            // write to event log
             //   MyErrorHandling.WriteEventLog(System.Diagnostics.EventLogEntryType.Error,
                    "Dashboard",
                    MyErrorHandling.FormatErrorMessage(userName,
                    "Custom Personalization Provider",
                    "SavePersonalizationBlobs()", ex.ToString()), 2);
            }
        }
        public override PersonalizationStateInfoCollection FindState(PersonalizationScope scope, PersonalizationStateQuery query, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotSupportedException();
        }
        public override int GetCountOfState(PersonalizationScope scope, PersonalizationStateQuery query)
        {
            throw new NotSupportedException();
        }
        public override int ResetState(PersonalizationScope scope, string[] paths, string[] usernames)
        {
            throw new NotSupportedException();
        }
        public override int ResetUserState(string path, DateTime userInactiveSinceDate)
        {
            throw new NotSupportedException();
        }
    }
}



now after this you have to configure the web.config to use the custom personalization provider instead of aspnetsqlprovider add the following block inside <system.web></system.web> in the web.config


XML
<webParts>
  <personalization defaultProvider="MyCustomPersonalizationProvider">
    <providers>
      <remove name="AspNetSqlProvider"/>
      <add connectionStringName="DashBoardConnection" name="MyCustomPersonalizationProvider" type="YourApplicationName.MyCustomPersonalizationProvider" applicationName="/"/>
    </providers>
    <authorization>
      <allow users="*" verbs="enterSharedScope"/>
    </authorization>
  </personalization>
</webParts>



If you code the sql statement correctly in the class where I have put the comments, everything will just work fine...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900