The Differences in Profile between Web Application Projects (WAP) and website

The profile services is a very helpful and easy way to add custom properties for your users which is  not contained in the Standard MembershipUser Properties...

for example , you may need to add the Marital status , Date of Birth, Address.... all of these are custom properties that you may need them while developing your projects ...

If you are familiar with Profile .. you will know that the first thing that must done when working with profiles is to set the Profile properties in web.config File ,

for example ,you can add  the Date of Birth, address, Marital status for the user profile as follows,

<profile>
  <properties >
    <add name="DateOfBirth" type="DateTime"/>
    <add name="Address" type="String"/>
    <add name="MaritalStatus" type="String"/>
  </properties>
</profile>

after saving the file , and working under a website project ,

you will notice that if you  typed profile in the page code behind , you will see the properties  generated for you as in the picture below:

this happened because the Visual studio created a new class called ProfileCommon Thats inherits ProfileBase , and adds the new properties to it ..

Note that visual studio will always update that class when you change the Profile properties in web.config ...

Now , if you are working with web application projects , you will notice that adding the Profile properties to web.config will not add any properties to Profile object in the code behind of the page.... this is because Visual studio doesn't generate a profileCommon class ...

instead you need to access the properties using  ProfielBase.GetPropertyValue(PropertyName)

for example , to access the DateOfBirth property , you need to use this code

Dim DOB As DateTime = CDate(HttpContext.Current.Profile.GetPropertyValue("DateOfBirth"))

In this post , I talked about the Differences in Profile  between the normal website and WAP projects  , Note that there is a lot of other differences between them, for example ,

when working with resource files , the website will provide a strong typing for resources properties which is also handled by Visual studio ...

 

Regards,

Anas Ghanem

9 Comments

  • I prefer this solution: http://weblogs.asp.net/jgalloway/archive/2008/01/19/writing-a-custom-asp-net-profile-class.aspx.

    Saludos,

  • How can i use asp:ProfileParameter in a ObjectDtaSource in my WAP. Are there any notices? I use vs2008,SP1.

  • Thanx for u r support keep it up

  • How to Access the Profile Properties in Global.asax file which is in WAP

  • Hi Venkat ,
    you can access the profile properties as i mentioned through the HttpContext class:

    For example , to access to DateOfBirth property in Authenticate_Request :
    DateTime DOB =(DateTime)HttpContext.Current.Profile.GetPropertyValue("DateOfBirth");

  • I m sorry but i have added my profile tag in my project but still i m unable to get profile properties in code behind fiel page....please can u help me for ths

  • Instead of writing a class for a userProfile, can't you just instantiate an object of type ProfileBase and use the Create method ? I assume this will be aware of the properties in the web.config file and allow you to assign and use the Save method. If you create your own class, even if you inherit from ProfileBase, it doesn't seem like the inherited methods are going to be aware of the properties you code, or will it ?

  • A custom ASP.Net UserProfile class in VB.Net:

    UserProfile CLASS:

    Namespace MIDASProfileManager

    Public Class UserProfile

    Inherits ProfileBase

    Public Shared Function GetUserProfile(username As String) As UserProfile

    Return TryCast(Create(username), UserProfile)

    End Function

    Public Shared Function GetUserProfile() As UserProfile

    Return TryCast(Create(Membership.GetUser().UserName), UserProfile)

    End Function

    _

    Public Property Theme() As System.String

    Get

    Return DirectCast(MyBase.Item("Theme"), String)

    End Get

    Set(value As System.String)

    MyBase.Item("Theme") = value

    End Set

    End Property

    _

    Public Property ThemeSelectedIndex() As Integer

    Get

    Return DirectCast(MyBase.Item("ThemeSelectedIndex"), Integer)

    End Get

    Set(value As Integer)

    MyBase.Item("ThemeSelectedIndex") = value

    End Set

    End Property

    End Class

    End Namespace

    Web.Config:













    In Global.asax Session_Start:

    Dim Profile As UserProfile = UserProfile.GetUserProfile(SV.Username)

    SV.ddTheme = Profile.Theme

    SV.ddThemeSelectedIndex = Profile.ThemeSelectedIndex

    My SV Class:

    Imports MIDAS.MIDASProfileManager

    '''

    ''' Session variables class - provides a facade to the ASP.Net Session Object.

    ''' All access to Session variables must be through this class.

    ''' Create an instance, populate and store in Session object for each session.

    '''

    Public NotInheritable Class SV

    Public Shared ReadOnly Property Username As String

    Get

    Return HttpContext.Current.User.Identity.Name

    End Get

    End Property

    Public Shared Property ddTheme As String

    Get

    If HttpContext.Current.Session("ddTheme") Is Nothing Then

    Return GV.DefaultTheme

    Else

    Return CStr(HttpContext.Current.Session("ddTheme"))

    End If

    End Get

    Set(ByVal value As String)

    If CStr(HttpContext.Current.Session("ddTheme")) = value Then

    Return

    End If

    ' TODO: To change to also store in profile

    HttpContext.Current.Session("ddTheme") = value

    Dim Profile As UserProfile = UserProfile.GetUserProfile(SV.Username)

    Profile.Theme = value

    Profile.Save()

    End Set

    End Property

    Public Shared Property ddThemeSelectedIndex As Integer

    Get

    If HttpContext.Current.Session("ddThemeSelectedIndex") Is Nothing Then

    Return GV.DefaultThemeSelectedIndex

    Else

    Return CInt(HttpContext.Current.Session("ddThemeSelectedIndex"))

    End If

    End Get

    Set(ByVal value As Integer)

    If CInt(HttpContext.Current.Session("ddThemeSelectedIndex")) = value Then

    Return

    End If

    ' TODO: To change to also store in profile

    HttpContext.Current.Session("ddThemeSelectedIndex") = value

    Dim Profile As UserProfile = UserProfile.GetUserProfile(SV.Username)

    Profile.ThemeSelectedIndex = value

    Profile.Save()

    End Set

    End Property

    End Class

    My GV Class:

    Imports System.Data

    Imports System.Linq

    Imports System.Web

    Imports Microsoft.Practices.EnterpriseLibrary.Logging

    '''

    ''' Contains my site's global variables.

    '''

    Public NotInheritable Class GV

    Private Sub New()

    End Sub

    Public Shared Property DefaultTheme() As String

    Public Shared Property DefaultThemeSelectedIndex() As Integer

    End Class

    Global.asax Application_Start has:

    GV.DefaultTheme = System.Configuration.ConfigurationManager.AppSettings("DefaultTheme")

    GV.DefaultThemeSelectedIndex = CInt(System.Configuration.ConfigurationManager.AppSettings("DefaultThemeSelectedIndex"))

  • using System.Web.Profile;
    using System.Configuration;


    namespace liquide.Account
    {
    public partial class Settings : System.Web.UI.Page
    {
    MembershipUser current = Membership.GetUser();
    protected void Page_Load(object sender, EventArgs e)
    {
    CityLabel.Text = HttpContext.Current.Profile.GetPropertyValue("City").ToString();

    }

    protected void UserCitiesDropDownList_SelectedIndexChanged(object sender, EventArgs e)
    {
    //LoginView1.FindControl("")

    HttpContext.Current.Profile.SetPropertyValue("City", CitiesDropDownList.SelectedValue.ToString());
    HttpContext.Current.Profile.Save();
    }
    }
    }

    and




    But Still Cant Set or Get.? the table in the database remains empty

Comments have been disabled for this content.