Using the Profile object:
Okay, now you are all ready to use the Profile object.
I have implemented this code in the button click event.
| Profile["Name"] = "Bill";
Profile["PicturePath"] = @"C:\Bill.jpg";
|
As, you can see that you can use the Profile object in a similar way that you used Session. The Profile properties must be the same as it is defined in Web.config file or else it will throw a runtime error. The above code will run fine but it's not strongly typed which means that you need to remember that you use "Name" and not "FirstName" and also you don't get intellisense support when you typing in Visual Studio.NET 2005.
You can easily write those lines like this:
| Profile.Name = "Bill William";
Profile.PicturePath = @"C:\Bill.jpg";
|
This way now you have intellisense support and there is less chance of making mistakes.
Where is the Profile Information Saved?
The Profile information is saved in the database aspnet_Profile. Simply, go to the SQL SERVER Enterprise Manager and open the database in which you created the Profile tables and open the table aspnet_Profile.

As, you can see that the Profile is saved in the table aspnet_Profile. The PropertyValuesString field contains the information about the properties which you have assigned. The first column is a UserId column which is a GUID.
When is the data written back to the aspnet_Profile table?
This is a very important and a question. It's very important to know that when is the trip to database will be made and how can I avoid it. When you are using simple properties which we are using right now then each time in a request you assign something to the property it goes back to the database and writes the new value for the property to the database.
I have made request word bold since its very important. This means that if we are assigning 10 values to the Profile object in one request then it will make one trip to the database and not ten.
Retrieving the Profile on the other page:
Retrieving the Profile on the other pages is also quite simply. Take a look at the code below which shows how you can retrieve the values from the Profile objects.
| protected void Page_Load(object sender, EventArgs e)
{
string userName = Profile.UserName;
string name = Profile.Name;
string picPath = Profile.PicturePath;
}
|
Easy isn't it. UserName is a predefined property that comes with the Profile object. If you look in the UserName you will see that its a 16 byte Hexadecimal number. This means that your user is currently not authenticated and GUID UserName represents its anonymous ID.
Authenticating a User:
Everyone is assigned an anonymous ID when they first come to the website. You can transfer the anonymous ID of the user to the new ID with simple settings in the Global.asax file.
It's always a good idea to authenticate the user in Application_AuthenticateRequest method of the Global.asax file. Let's see why we need to do it.
Let's say you write some code like this:
| protected void Button1_Click(object sender, EventArgs e)
{
// Here you have authenticated the user
FormsAuthentication.SetAuthCookie("Bill", false);
string userName = Profile.UserName;
Profile.Name = "Bill William";
Profile.PicturePath = @"C:\Bill.jpg";
Response.Redirect("MyPage.aspx");
}
|
The userName will still contain the anonymous GUID because you requested the webpage using anonymous ID. When you reach MyPage.aspx the user is authenticated and will contain "Bill" as the userName.
| protected void Page_Load(object sender, EventArgs e)
{
// gets the userName
string userName = Profile.UserName;
string name = Profile.Name;
string picPath = Profile.PicturePath;
}
|
So, you must be wondering that what's wrong with that since I finally got the correct ID. But go to the database check out the entries in the aspnet_Profile table. It will have two entries one for the anonymous user and one authenticated user that's pretty confusing right. Therefore whenever you want to authenticate a user do the authentication in the Application_BeginRequest method of the Global.asax.
In this article you saw the insights of the Profile object. In the next article we will see how you can use Groups and Complex types with the Profile object.
I hope you liked the article, happy programming!