User Profile State Management in ASP.NET

Introduction

User Profile is a mechanism of state management .user profile feature associates information with an individual user and stores the information in a persistent format. User Profiles allow you to manage user information without requiring you to create and maintain your own database. Suppose if a user enter a address while purchasing a product . here user profile stores the all address value. And if same user same browser visit again to purchase product he not need to enter address again here we fill address from user profile.

Here I try to describe with code .

Aspx Page

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="UserProfile.aspx.cs" Inherits="UserProfile" %>  
  2. <!DOCTYPE html>  
  3. <html  
  4.     xmlns="http://www.w3.org/1999/xhtml">  
  5.     <head runat="server">  
  6.         <title></title>  
  7.     </head>  
  8.     <body>  
  9.         <form id="form1" runat="server">  
  10.             <div>  
  11. Hello :           
  12.                 <asp:Label ID="Label1" runat="server" Font-Bold="True" Text="Label"></asp:Label>  
  13.                 <br />  
  14. Last visted on:    
  15.                 <asp:Label ID="Label2" runat="server" Font-Bold="True" Text="Label"></asp:Label>  
  16.                 <br />  
  17. Birth Place is :  
  18.                 <asp:Label ID="Label3" runat="server" Font-Bold="True" Text=""></asp:Label>  
  19.             </div>  
  20.             <br />  
  21.             <br />  
  22. Enter your name:     
  23.             <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>   
  24. Enter Your Birth Place   
  25.             <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>   
  26.             <asp:Button ID="Button1" runat="server" Text="Save" OnClick="Button1_Click" />  
  27.         </form>  
  28.     </body>  
  29. </html>  
Aspx.cs
  1. using System;  
  2. using System.Data;  
  3. using System.Configuration;  
  4. using System.Web;  
  5. using System.Web.Security;  
  6. using System.Web.UI;  
  7. using System.Web.UI.WebControls;  
  8. using System.Web.UI.WebControls.WebParts;  
  9. using System.Web.UI.HtmlControls;  
  10. using System.Drawing;  
  11. public partial class UserProfile: System.Web.UI.Page  
  12. {  
  13.     protected void Page_Load(object sender, EventArgs e)   
  14.     {  
  15.         if (IsPostBack == false)   
  16.         {  
  17.             string name = Profile.Name;  
  18.             DateTime lastVisited = Profile.VisitedOn;  
  19.             string birthPlace;  
  20.   
  21.             if (name == string.Empty)  
  22.             {  
  23.                 //User has not specified his name  
  24.                 Label1.Text = "Guest";  
  25.             } else {  
  26.                 //returning user, show his name  
  27.                 Label1.Text = name;  
  28.             }  
  29.             if (lastVisited.ToString() == "1/1/0001 12:00:00 AM")  
  30.             {  
  31.                 Label2.Text = "Never";  
  32.             }   
  33.             else   
  34.             {  
  35.                 Label2.Text = lastVisited.ToString();  
  36.             }  
  37.         }  
  38.     }  
  39.     protected void Page_UnLoad(object sender, EventArgs e)   
  40.     {  
  41.         Profile.VisitedOn = DateTime.Now;  
  42.   
  43.         Profile.Save();  
  44.     }  
  45.     protected void Button1_Click(object sender, EventArgs e)  
  46.     {  
  47.         Profile.Name = TextBox1.Text;  
  48.         Label1.Text = TextBox1.Text;  
  49.         Label3.Text = TextBox2.Text;  
  50.         Profile.Save();  
  51.     }  
  52. }
Web.config
  1. <?xml version="1.0"?>  
  2. <configuration>  
  3.     <connectionStrings>  
  4.         <add name="connectionFirst" connectionString="Data Source=.;Initial Catalog=DataBaseName;Integrated Security=True" providerName="System.Data.SqlClient"/>  
  5.     </connectionStrings>  
  6.     <system.web>  
  7.         <compilation debug="true"/>  
  8.         <authentication mode="Windows"/>  
  9.         <anonymousIdentification enabled="true"/>  
  10.         <profile>  
  11.             <providers>  
  12.                 <add name="UserProfile" connectionStringName="connectionFirst" applicationName="/" type="System.Web.Profile.SqlProfileProvider"/>  
  13.             </providers>  
  14.             <properties>  
  15.                 <add name="Name" allowAnonymous="true"/>  
  16.                 <add name="VisitedOn" type="System.DateTime" allowAnonymous="true"/>  
  17.             </properties>  
  18.         </profile>  
  19.     </system.web>  
  20. </configuration>
Ebook Download
View all
Learn
View all