ASP .NET 2.0 Profile Object - Part II
CoderSource.net
ASP .NET 2.0 Profile Object - Part II - Article by azamsharp
Level: BeginnerType: Article
Rating: Page: 2 of 2

Date: 1/20/2006 12:00:00 AM

Environment:

Complex Types:

You can even use Profile object with the Complex Types which includes Entity Classes.

Let's first make a very simple entity class.

Here is a very simple class:

[Serializable]

public class User

{

   /* PRIVATE FIELDS */

   private string _firstName;

   private string _lastName;

   /* PUBLIC PROPERTIES */

   public string FirstName

   {

      get

      {

         if (!String.IsNullOrEmpty(_firstName))

              return _firstName;

        else return null;

       }

       set { _firstName = value; }

    }

   public string LastName

   {

      get

      {

           if (!String.IsNullOrEmpty(_lastName))

              return _lastName;

           else return null;

       }

       set { _lastName = value; }

   }

   public User()

   {

   }

}

 

When using Profile object with the entity class you have to mark the class Serializable.

Now, let's see the settings in the web.config file.

<anonymousIdentification enabled="true"/>

<profile defaultProvider="MyProfileProvider">

<providers>

<add name="MyProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ConnectionString"/>

</providers>

<properties>

<add name="User" type="User" serializeAs="Binary" allowAnonymous="true"/>

</properties>

</profile>

The name attribute simply represents the name of the Profile. The serializaAs attribute represents that what will be the format of the serialized object. In this case I have defined the format as Binary but you can also choose XML, String or ProviderSpecific.

Now, let's see how you can use the Profile object.

Profile.User.FirstName = "Andy";

Profile.User.LastName = "Johnson";

As you can see it is very similar to the approach that we were already using. One important question that we need to ask our self is that when is the Profile object updated when we are using an entity class.

You might be thinking that since we can any type to be used for the properties then we can also use WebControls.Image types. Let's see this in action:

<anonymousIdentification enabled="true"/>

<profile defaultProvider="MyProfileProvider">

<providers>

<add name="MyProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ConnectionString"/>

</providers>

<properties>

<add name="User" type="

System.Web.UI.WebControls.Image

" serializeAs="Binary" allowAnonymous="true"/>

</properties>

</profile>

When you used the Image type in your code and assign it anything it will throw an exception. The reason is because Image class is not serializable and for the Profile properties to work the class must be serializable. You can off course save the image path as a string and later retrieve the string using the path. 

When is the Profile Object Updated?

The data is updated in the database whenever we assign something to the Profile Object and when the request is completed. The data will only be updated in the database after the request has been completed.

Migrating Anonymous Profiles:

The last thing that I will discuss in this article is migrating the anonymous profiles to authenticated profiles. When ever the user comes to the website he always has his anonymous profile in which the username is created using GUID. You can consider a situation that a user visits the shopping cart application and after all the shopping he is asked for the username and password. This means that all that time the user was shopping using anonymous authentication. You can easily migrate user's anonymous Profile by using the following code in Global.asax file.

void Profile_MigrateAnonymous(Object s, ProfileMigrateEventArgs e)

{

ProfileCommon anonProfile = Profile.GetProfile(e.AnonymousID);

Profile.Name = anonProfile.Name;

Profile.PicturePath = anonProfile.PicturePath;

}

In the next article we will see how to make a Custom Profile Provider.

I hope you liked the article, happy programming!

1 2

You Can Rate this Article, if you are Logged In      
 

More Links from CoderSource.net:

 
Refer to a Friend:

Your Details:

Name:     e-mail:

Friend Details:

Name:    e-mail:    


MENU
Home
MFC 
C++
.Net
WIN32
Programming
Forum
My Articles
Add to Google
Add to My Yahoo!
Welcome to Codersource.Net Login | Register | Faq  

SEARCH
Google
 

NOTES:


Thanks for visiting our CoderSource.net. This site will be improved with more articles. Interested visitors can also submit their articles through the Submit Article link.Your article will also be published after due consideration by the editor. 

© Copyright 2003. All rights on content reserved by CoderSource.net. Contact    About Us