How to assign Profile values? How to assign Profile values? asp.net asp.net

How to assign Profile values?


I had the same problem today, and learned a lot.

There are two kinds of project in Visual Studio -- "Web Site Projects" and "Web Application Projects." For reasons which are a complete mystery to me, Web Application Projects cannot use Profile. directly... the strongly-typed class is not magically generated for you from the Web.config file, so you have to roll your own.

The sample code in MSDN assumes you are using a Web Site Project, and they tell you just to add a <profile> section to your Web.config and party on with Profile.property, but that doesn't work in Web Application Projects.

You have two choices to roll your own:

(1) Use the Web Profile Builder. This is a custom tool you add to Visual Studio which automatically generates the Profile object you need from your definition in Web.config.

I chose not to do this, because I didn't want my code to depend on this extra tool to compile, which could have caused problems for someone else down the line when they tried to build my code without realizing that they needed this tool.

(2) Make your own class that derives from ProfileBase to represent your custom profile. This is easier than it seems. Here's a very very simple example that adds a "FullName" string profile field:

In your web.config:

<profile defaultProvider="SqlProvider" inherits="YourNamespace.AccountProfile"><providers>     <clear />     <add name="SqlProvider"          type="System.Web.Profile.SqlProfileProvider"          connectionStringName="sqlServerMembership" /></providers></profile>

In a file called AccountProfile.cs:

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Profile;using System.Web.Security;namespace YourNamespace{    public class AccountProfile : ProfileBase    {        static public AccountProfile CurrentUser        {            get { return (AccountProfile)                         (ProfileBase.Create(Membership.GetUser().UserName)); }        }        public string FullName        {            get { return ((string)(base["FullName"])); }            set { base["FullName"] = value; Save(); }        }        // add additional properties here    }}

To set a profile value:

AccountProfile.CurrentUser.FullName = "Snoopy";

To get a profile value

string x = AccountProfile.CurrentUser.FullName;


Web Application Projects can still use the ProfileCommon object but only at runtime. The code for it is just not generated in the project itself but the class is generated by ASP.Net and is present at runtime.

The simplest way to get to object is to use a dynamic type as demonstrated below.

In the Web.config file declare the profile properties:

<profile ... <properties>   <add name="GivenName"/>   <add name="Surname"/> </properties>

Then to access the properties:

dynamic profile = ProfileBase.Create(Membership.GetUser().UserName);string s = profile.GivenName;profile.Surname = "Smith";

To save changes to profile properties:

profile.Save();

The above works fine if you are comfortable using dynamic types and don't mind the lack of compile-time checking and intellisense.

If you use this with ASP.Net MVC you have to do some additional work if you pass the dynamic profile object to your views since the HTML helper methods don't play well with "model" objects that are dynamic. You will have to assign profile properties to statically typed variables before passing them to HTML helper methods.

// model is of type dynamic and was passed in from the controller@Html.TextBox("Surname", model.Surname) <-- this breaks@{ string sn = model.Surname; }@Html.TextBox("Surname", sn); <-- will work

If you create a custom profile class, as Joel described above, ASP.Net will still generate the ProfileCommon class but it will inherit from your custom profile class. If you don't specify a custom profile class ProfileCommon will inherit from System.Web.Profile.ProfileBase.

If you create your own profile class make sure that you don't specify profile properties in the Web.config file that you've already declared in your custom profile class. If you do ASP.Net will give a compiler error when it tries to generate the ProfileCommon class.


Profile can be used in Web Application Projects too.The properties can be defined in Web.config at design time or programmatically. In Web.config:

<profile enabled="true" automaticSaveEnabled="true" defaultProvider="AspNetSqlProfileProvider">      <providers>        <clear/>        <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="TestRolesNProfiles"/>      </providers>      <properties>        <add name="FirstName"/>        <add name="LastName"/>        <add name ="Street"/>        <add name="Address2"/>        <add name="City"/>        <add name="ZIP"/>        <add name="HomePhone"/>        <add name="MobilePhone"/>        <add name="DOB"/>      </properties>    </profile>

or Programmatically, create the profile section by instantiating a ProfileSection and creating individual properties using ProfilePropertySettings and ProfilePropertySettingsColletion, all of which are in System.Web.Configuration Namespace. To use those properties of the profile, use System.Web.Profile.ProfileBase Objects. The profile properties cannot be accessed with profile. syntax as mentioned above, but can be easily done by instantiating a ProfileBase and using SetPropertyValue("PropertyName") and GetPropertyValue{"PropertyName") as follows:

ProfileBase curProfile = ProfileBase.Create("MyName");

or to access the profile of current user:

ProfileBase curProfile = ProfileBase.Create(System.Web.Security.Membership.GetUser().UserName);        curProfile.SetPropertyValue("FirstName", this.txtName.Text);        curProfile.SetPropertyValue("LastName", this.txtLname.Text);        curProfile.SetPropertyValue("Street", this.txtStreet.Text);        curProfile.SetPropertyValue("Address2", this.txtAdd2.Text);        curProfile.SetPropertyValue("ZIP", this.txtZip.Text);        curProfile.SetPropertyValue("MobilePhone", txtMphone.Text);        curProfile.SetPropertyValue("HomePhone", txtHphone.Text);        curProfile.SetPropertyValue("DOB", txtDob.Text);        curProfile.Save();