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
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="UserProfile.aspx.cs" Inherits="UserProfile" %>
- <!DOCTYPE html>
- <html
- xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- Hello :
- <asp:Label ID="Label1" runat="server" Font-Bold="True" Text="Label"></asp:Label>
- <br />
- Last visted on:
- <asp:Label ID="Label2" runat="server" Font-Bold="True" Text="Label"></asp:Label>
- <br />
- Birth Place is :
- <asp:Label ID="Label3" runat="server" Font-Bold="True" Text=""></asp:Label>
- </div>
- <br />
- <br />
- Enter your name:
- <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
- Enter Your Birth Place
- <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
- <asp:Button ID="Button1" runat="server" Text="Save" OnClick="Button1_Click" />
- </form>
- </body>
- </html>
Aspx.cs
- using System;
- using System.Data;
- using System.Configuration;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- using System.Drawing;
- public partial class UserProfile: System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- if (IsPostBack == false)
- {
- string name = Profile.Name;
- DateTime lastVisited = Profile.VisitedOn;
- string birthPlace;
-
- if (name == string.Empty)
- {
-
- Label1.Text = "Guest";
- } else {
-
- Label1.Text = name;
- }
- if (lastVisited.ToString() == "1/1/0001 12:00:00 AM")
- {
- Label2.Text = "Never";
- }
- else
- {
- Label2.Text = lastVisited.ToString();
- }
- }
- }
- protected void Page_UnLoad(object sender, EventArgs e)
- {
- Profile.VisitedOn = DateTime.Now;
-
- Profile.Save();
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- Profile.Name = TextBox1.Text;
- Label1.Text = TextBox1.Text;
- Label3.Text = TextBox2.Text;
- Profile.Save();
- }
- }
Web.config
- <?xml version="1.0"?>
- <configuration>
- <connectionStrings>
- <add name="connectionFirst" connectionString="Data Source=.;Initial Catalog=DataBaseName;Integrated Security=True" providerName="System.Data.SqlClient"/>
- </connectionStrings>
- <system.web>
- <compilation debug="true"/>
- <authentication mode="Windows"/>
- <anonymousIdentification enabled="true"/>
- <profile>
- <providers>
- <add name="UserProfile" connectionStringName="connectionFirst" applicationName="/" type="System.Web.Profile.SqlProfileProvider"/>
- </providers>
- <properties>
- <add name="Name" allowAnonymous="true"/>
- <add name="VisitedOn" type="System.DateTime" allowAnonymous="true"/>
- </properties>
- </profile>
- </system.web>
- </configuration>