Click here to Skip to main content
15,881,812 members
Articles / All Topics

Using ASP.NET Profile Feature in a Web Application Project

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
27 Nov 2011CPOL2 min read 21.1K   4   1
Using ASP.NET Profile Feature in a Web Application Project

I decided to take advantage of the Profile feature of .NET to store some personal data per user. Shouldn't be a big deal, right? After all, you just call the appropriate assembly and then, you should be able to invoke Profile.Save() and like magic, the data is now in your ASP database on the Profiles table.

This is when I came across a huge set of posts and threads about how complex and confusing it can be to implement the Profile feature if you have a Web Application Project.

Among the confusing, credit should be due to Lee Dumond and Scott Gu as usual for posting some awesome examples in their blogs.

By following Lee's blog, below is my implementation of the same technique to be able to use Profiles in my Web Application Project. I thought it would be a good idea to post it, in order to give you a second example.

Step 1

Create a class named ProfileInfo which exposes the properties that I want to save in my profile, in this case, let's work with two FirstName and LastName.

C#
namespace Project1.Account
{
    [Serializable]
    public class ProfileInfo
    {
        public string FirstName { get; set; } 
        public string LastName { get; set; } 
    }
}

Step 2

Create a second class named wProfile, this class is inherited from the ProfileBase class and will be used to expose the profile functionality on other pages in the site.

C#
using System.Web;
using System.Web.Profile;
namespace Project1.Account
{
    public class wProfile : ProfileBase
    {
 
        public ProfileInfo ProfileInfo
        { 
            get { return (ProfileInfo) GetPropertyValue("ProfileInfo"); }       
        } 
 
        public static wProfile GetProfile() 
        { 
            return (wProfile) HttpContext.Current.Profile; 
        } 
 
        public static wProfile GetProfile(string userName) 
        { 
            return (wProfile) Create(userName); 
        }  
    }
}

Step 3

Modify your Web.config file to implement a SQLProfileProvider. Notice the "inherits" on the defaultProvider definition. Also notice that the connection ApplicationServices was previously defined.

XML
<profile defaultProvider="AspNetSqlProfileProvider" inherits="Project1.Account.wProfile"> 
  <providers>
    <clear/>
    <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" 
	connectionStringName="ApplicationServices" applicationName="/wMyApp"/>
  </providers>
</profile>

Step 4

Implement the logic in my ASPX pages:

C#
protected void Page_Load(object sender, EventArgs e)
{
    wProfile lProfile = wProfile.GetProfile(Membership.GetUser().UserName);
    Label1.Text = "Welcome, " + lProfile.ProfileInfo.FirstName + " " + 
	lProfile.ProfileInfo.LastName;
}


Happy coding.

Hope this helps,
Will


<meta name="keywords" content="Windows 7 Open Application in Admin Mode, C#, T-SQL, Microsoft SQL Server, general Windows 7 issues, Windows 7, Application Admin Mode, Running Stored Procedures WCF Service, iPhone suggestions and recommendations, interesting coding techniques and factoids and personal expertise in writing LINQ statements, working with ListViews, Content Panels, methods, functions and other general software development topics, ASP.NET, C#, Microsoft SQL Server 2008, Microsoft SQL Server 2008, SSRS, SSRS 2008, LINQ, Windows 7, iPhone, ListViews, Webparts, Content, Panels, Stored procedures, SP, T-SQL, SELECT, UPDATE, INSERT, DELETE, XML, App.Config, Web.Config, Application Settings, HTML, Javascript, Exporting XML Content StreamWriter, DotNetZip Example">

This article was originally posted at http://feeds.feedburner.com/blogspot/NyVKAz

License

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow to Groupping in Asp.net Profile for web application... Pin
shriji.111125-Jun-13 19:18
shriji.111125-Jun-13 19:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.