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

Published Saturday, April 12, 2008 9:46 PM by anas
Filed under: ,

Comments

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

Monday, April 14, 2008 3:37 AM by sergiotarrillo

I prefer this solution: weblogs.asp.net/.../writing-a-custom-asp-net-profile-class.aspx.

Saludos,

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

Wednesday, February 18, 2009 8:17 AM by Amir

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

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

Wednesday, March 4, 2009 5:50 AM by venkateswara rao Morampudy

Thanx for u r support keep it up

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

Friday, March 6, 2009 6:23 AM by venkat

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

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

Friday, March 6, 2009 7:10 AM by anas

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");

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

Thursday, June 24, 2010 8:03 AM by s3dra

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

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

Wednesday, September 21, 2011 12:32 PM by chris9876

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 ?

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

Saturday, February 4, 2012 2:17 AM by Raja Lakshman

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

      <SettingsAllowAnonymous(False)> _

      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

      <SettingsAllowAnonymous(False)> _

      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:

  <profile inherits="MIDAS.MIDASProfileManager.UserProfile" enabled="true" defaultProvider="MyAspNetSqlProfileProvider" >

    <providers>

      <add name="MyAspNetSqlProfileProvider"

           connectionStringName="aspnetdbConnectionString"

              applicationName="MIDAS"

              type="System.Web.Profile.SqlProfileProvider" />

    </providers>

  </profile>

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

''' <summary>

''' 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.

''' </summary>

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

''' <summary>

''' Contains my site's global variables.

''' </summary>

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"))

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

Tuesday, February 21, 2012 12:45 AM by Warren

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

 <add name="City" type="string"/>

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