jump to navigation

Using profile in ASP.Net applications October 5, 2010

Posted by fofo in asp.net, Visual Studio 2005, Visual Studio 2008, Visual Studio 2010.

We all want when we visit a website to have some unique treatment (personalisation). Developers on the other side must devise mechanisms to remember our personal settings and preferences. This makes it more likely for us to return. ASP.Net developers must track users and save somehow their preferences between visits.

There are many ways to do that. Some are easy and some more complex. A lot of people seem not to know the profile feature that is in our disposal from ASP.NET 2.0.

With Profile we store user specific data that is accessible anywhere in our ASP.Net application.

We define properties using the web.config file. We use standard XML code to do that. ASP.NET reads the schema defined in web.config and automatically generates a class that is accessible from the Profile property on a page.

I am going to show you a small example of the Profile property.

I will be using VS 2010 Ultimate edition and C# to create a simple asp.net application.

1) Launch Visual Studio 2010/2008/2005.Express edition will suffice.

2) Create a web site with an appropriate name.

3) In the default.aspx add four TextBox controls and a button. Leave the default names.The markup should look like this

Name: <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
Age:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
Url:<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br />
Comments: <asp:TextBox ID="TextBox4" runat="server" TextMode=MultiLine>
</asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="Save to Profile" 
        onclick="Button1_Click" />

4) Inside the web.config, in the <system.web> section add these lines of code

          <add name="Name"/>
          <add name="Age"/>
          <add name="Url"/>
          <add name="Comments"/>

5) In the Button1_Click event handling routine type

 Profile.Name = TextBox1.Text;
 Profile.Age = TextBox2.Text;
 Profile.Url = TextBox3.Text;
 Profile.Comments = TextBox4.Text;

6) Run your application and fill in the textbox values that will eventually be stored in the Profile.

7) Add another page in your site. Leave the default name. Add 4 Labels in the new page. The markup should look like this

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label><br />
<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label><br />
<asp:Label ID="Label4" runat="server" Text="Label"></asp:Label><br />

8) In the Page_Load event handling routine, type

 Label1.Text = Profile.Name ;
 Label2.Text=Profile.Age ;

9) Run your application( Set the Default.aspx as the start page ). Fill in the texboxes and click the button. Close the web browser.

10) Run the default2.aspx page. You will see the values from the Profile object appearing in the label controls.

You can use the Profile object to hard code values in the web.config that are important to your site. Do not forget that you can encrypt your site.

Imagine if you have a website and it has 100 pages. If the admin’s email changes then you need to update all 100 pages with the new admin’s email. I have seen that happening in one web site where

  • It did not use a master page.
  • The admin’s email was not retrieved from the database.

Email me if you need the source code.

Hope it helps!!!



1. zvi - May 23, 2011

when I tried to do it i get the error –
“The name ‘Profile’ does not exist in the current context”
sombody told me to change in the web.comnfig properties Bulit action to “Complie” and Copy to output to “Copy always”.
Now I have the error –
“A namespace cannot directly contain members such as fields or methods ” at the web.config file

what I need to do?

2. Sean - May 25, 2011

I have the exact same problem. Anyone with a solution?

3. Sean - May 25, 2011

OK, the Profile object only works with Web-Site projects not Web-Application projects!

4. plastic surgery financing australia - June 12, 2013

Whats up! I just wish to give an enormous thumbs up
for the great information you could have here on
this post. I will probably be coming again to your weblog for extra soon.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: