Profiles in Asp.net 2.0 by azamsharp

Introduction:

We all remember the Sessions and Application variables in Asp.net 1.1. For those who are not familiar with Sessions and Application variables here is a quick recap.

Session Variables:

Session variables are used to maintain a session for a single user. Each session hold a certain information about a user. The questions is why do we need to store sessions and if we even do why can’t we simply store them in cookies. Well, cookies are client side and sessions are server side. And you might also know that cookies are not enabled on all the browsers. Anyway back to sessions, so we can use sessions to store a user information and in that way we will know that this user’s session has been created and we don’t have to create it again and hence we can migrate his shopping cart maybe add some items in the card or insert some more information into its session variables.
Creating sessions is also very easy so lets see how we can create a simple session which contains the value from a textbox.

Session["UserName"] = TextBox1.Text;

You can also insert collections in the session variables like this:

System.Collections.ArrayList aList = new System.Collections.ArrayList();
aList.Add("SharpControls");
aList.Add("RazorControls");
aList.Add("PlainControls");
Session["Controls"] = aList;

Application Variables:

The Application Variables are used to hold the information which is for the whole application. This can be a simple web based user counter which keep tracks of how many users visited the website. The syntax is pretty much the same as the Session variables except for the Application keyword.

Application["Counter"] = 1;

Most of the time all the Application variable information is put into the Global.asax file.

New things in Profiles:

Profile object in the Asp.net 2.0 is quite similar to the Session variables in the Asp.net 1.1 with one exception that profile stays consistent and is not lost when the user leaves the website. If you remember Session variables is lost if you leave the particular website and than if you visit the website again a new session had to be created.
If you remember that when we used the Session variables in Asp.net 1.1 a unique Session ID was generated for every user. This ID was generated automatically. Did you ever tried printing out the Session ID. Well if you have not it looks something like this of-course it will be different for each computer and each session that’s why its unique.

Response.Write(Session.SessionID);  // This will print           roahoq55lpkgn055qwcwod45

Let’s see some examples that how we can use profile object in our Asp.net web applications:
First in order to create unique id for the anonymous users we need to add a tag in the web.config file.

<anonymousIdentification enabled="true" />

This tag means that we will create a Profile ID for the anonymous users.
New thing to note about the Profile tag is that you cannot make the variables on the fly. I mean you cannot do something like this:
Sample2.aspx: We just simply put the Name from the TextBox in the profile object

Profile["Name"] = txtName.Text;
Response.Redirect("Sample2_1.aspx");

Sample2_1.aspx: This page recieved the Name from the Sample2.aspx page and prints the result

if (!Page.IsPostBack)
{
	if (Profile["Name"] != null)
	{
		string name = Profile["Name"].ToString();
		Response.Write(name);
	}
}

This will result in an error saying that it was not able to recognize the Name variable. For this to work we need to define the Profile variables and what better place to do this than in the web.config file.
Let’s see how we can pass values from one page to another using the Profile object.

<profile>
</properties>
</profile>allowAnonymous="true" /><addname="Name"defaultValue="Tom"<properties>

As we see that we have introduced some new tags in the profile class. All the tags are placed inside the profile tag which also contains the properties tag. You can define any number of profile variables in the add tag. In this example we are just using the “Name” tag. Now let’s see how we can access the values of the Profile variables that we have just set in the web.config file.
Our “Sample2.aspx” page contains a simple TextBox and the button. When the button is clicked the values are transferred to another page Sample2_1.aspx page.


Now let’s see the button click code:

void Button1_Click(object sender, EventArgs e)
{
	Profile["Name"] = txtName.Text;
	Response.Redirect("Sample2_1.aspx");
}

We simply assigns the value of the TextBox to the Profile variable called “Name” and than redirected the page to the Sample2_1.aspx page where we can now display the value.

Sample2_1.aspx:
if (!Page.IsPostBack)
{
	if (Profile["FirstName"] != null)
	{
		string name = Profile["FirstName"].ToString();
		Response.Write(name);
	}
}

On this page we simply retrieve the value from the Profile object and prints out the value.
Let’s see if we can transfer an arrayList from one page to another by putting it in the profile object.

Sample3.aspx
void Button1_Click(object sender, EventArgs e)
{
System.Collections.ArrayList aList = new System.Collections.ArrayList();
aList.Add("Azam");
aList.Add("John");
aList.Add("Saif");
Profile["Name"] = (object) aList;  // You may not need the (object) conversion since this is an implicit conversion
Response.Redirect("Sample3_1.aspx");
}

In the above code we just added few items in the arrayList object and than put that arrayList in the Profile object. And finally we redirected the to Sample3_1.aspx page.

Sample3_1.aspx
if (!Page.IsPostBack)
{
	if (Profile["Name"] != null)
	{
		System.Collections.ArrayList myList = (System.Collections.ArrayList)Profile["Name"];
		for (int i = 0; i < myList.Count; i++)
		{
			Response.Write(myList[i].ToString());
		}
	}
}

In this code sample we just iterate through the list and print all the values contained in the arrayList.
If you run this code you will get an error saying that the Profile object is not compatible with the type. For this to work you need to make a small change in the web.config file.

</profile></properties>allowAnonymous="true" /><addname="Name"type="System.Collections.ArrayList"defaultValue="??"<properties>

Now if you see the web.config file above you can see that I have defined a type attribute which has the “System.Collections.ArrayList” type.
Now if you run your page it will work fine and display the results on the Sample3_1.aspx page.
There is much more you can do with profiles like sending classes information by serializing them and inheriting from profiles and using role based profiles.
Be sure to check all the code samples that I have written.
happy coding !

Attachments:

Project Files: ProfilesinAsp2.zip