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: 1 of 2

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

Environment:

Introduction:

In the last article you learned the basics of the Profile object which is introduced in ASP.NET 2.0.

In this article we will learn some of the more cool features of the Profile objects which includes Groups, Complex Types and migrating anonymous to authenticated user.

Using Groups with Profile:

In the last article I showed you that how you can make properties in the web.config file and use those defined properties in your code behind. The properties that we defined in the last article were common to all and hence every user exposes the same properties. In a multi user and multi role environment you may need to expose some properties only for a group of users. Profile object let's you distinguish users based on their groups.

Creating Groups:

Let's see how we can create groups. The only thing you need to do is to add the settings in the configuration file. Take a look at the settings below which makes two groups Students and Faculty.

<anonymousIdentification enabled="true"/>

<profile defaultProvider="MyProfileProvider">

<providers>

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

</providers>

<properties>

<group name="Student">

<add name="ID" allowAnonymous="true" type="System.Int32" />

<add name="Name" allowAnonymous ="true"/>

<add name="Major" allowAnonymous ="true"/>

</group>

<group name="Faculty">

<add name="ID" allowAnonymous="true" type="System.Int32"/>

<add name="Name" allowAnonymous="true"/>

<add name="CourseList" allowAnonymous="true"  type="System.Collections.ArrayList" />

</group>

</properties>

</profile>

The Student group exposes different properties than the Staff group. One more important thing to note is the property name CourseList which is of ArrayList type. You can use any datatype within the properties section whose base class allows serialization. I will talk about this later in this article in a little detail.

Now, let's see how we can use the Groups that we have defined in the web.config file.

Profile.Student.Name = "Azamsharp";

Profile.Faculty.Name = "Dr Smith";

See how easy it is and it is pretty much the same that we have already done in the first article. Once again we get the full intellisense support when using groups in the Profile object.

You can also easily populate the ArrayList of the Faculty member with come dummy courses. Take a look at the code below to see how we populate the ArrayList of a Faculty member.

Profile.Faculty.Name = "Dr Smith";

Profile.Faculty.CourseList.Add("Math");

Profile.Faculty.CourseList.Add("English");

Profile.Faculty.CourseList.Add("Accounting");

And you can easily retrieve the values in the other page using the following piece of code:

string name = Profile.Faculty.Name;

System.Collections.ArrayList courseList = Profile.Faculty.CourseList;

Also, remember that the next time you add in the Profile object it won't be empty but it will have 3 records which you inserted previously.

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