German Wear Discount Shop - Click Here Write for Dotnet-friends and earn for your each submission [Dot]Net-Friends
Skip Navigation Links
Home
Latest
Fast Code
Articles
Tutorials
Online Resources
Forums
Login   | Hi, Guest


Profiling in ASP .NET 2.0

Written by admin on Dec 26, 2006
How Create Profiles in ASP .NET 2.0

Introducation:

ASP .NET 2.0 Made life easy by launching built-in Membership. Ther are some situations when web developers need more information to keep in their Datbase. Profiling is the Best solution for that. Here we will look on how to create and access profiling.

Explanation:

Ist thing which is must to do is telling the Web Application that what extra information we need to keep in our Database. These informations come in the  Web.config

 

For example we want to add information about "Age" and "Gender" of our users. We will Add these lines of code to our Web.config.

 

<profile enabled="true">
<properties>
<add name="Age" allowAnonymous="true" type="System.Int16"/>
<add name="Gender" allowAnonymous="true"/>
properties>
profile>  

Now we will see how to retrive and add information in our Page ( .aspx )

 

Accessing Logged-In User Profile:

<%@ Page Language="C#"  %>
<script runat="server">  
  
protected void Page_Load(object sender, EventArgs e)
  {
    
if (!IsPostBack)
    {
      gTextBox.Text = Profile.Gender;
      ageTextBox.Text = Profile.Age.ToString();
    }
  }

  
protected void updateProfileButton_Click(object sender,  EventArgs e)
  {
    Profile.Gender = gTextBox.Text;
    Profile.Age =
Int16.Parse(ageTextBox.Text);
    Profile.Save();  //this line is must to save the Data
  }
  
script>

<
asp:Content ID="Content1" ContentPlaceHolderID="main" runat="Server">
   <asp:TextBox runat="server" ID="gTextBox" />
  <asp:TextBox runat="server" ID="ageTextBox" /> 
   <asp:Button runat="server" ID="updateProfileButton" Text="Save Preferences"
        OnClick="updateProfileButton_Click" />
asp:Content >
  

Accessing User Profile Which is not Logged-in:

 

<%@ Page Language="C#"  %>
<script runat="server">  
ProfileCommon Prof = Profile.GetProfile(UserName);
//UserName Can be accessed through QureyString, SessionState or other any suitble method

 protected void Page_Load(object sender, EventArgs e)
  {
   
if (!IsPostBack)
    {
      gTextBox.Text = Prof.Gender;
      ageTextBox.Text = Prof.Age.ToString();
    }
  }

 
protected void updateProfileButton_Click(object sender,  EventArgs e)
  {
    Prof.Gender = gTextBox.Text;
    Prof.Age =
Int16.Parse(ageTextBox.Text);
    Prof.Save();   // this  is must to save the Data
  }
  
script>

<
asp:Content ID="Content1" ContentPlaceHolderID="main" runat="Server">
   <asp:TextBox runat="server" ID="gTextBox" />
  <asp:TextBox runat="server" ID="ageTextBox" /> 
   <asp:Button runat="server" ID="updateProfileButton" Text="Save Preferences"
        OnClick="updateProfileButton_Click" />
asp:Content >
  

How to Create an Ananymous User Profile:

 

protected void btnSave_Click(object sender, EventArgs e) {
  ProfileCommon userProfile =
        (ProfileCommon) ProfileCommon.Create(txtUserName1.Text, false);
      userProfile.Street = txtStreet.Text;
      userProfile.Save();
}

 

But one thing have to be keep in mind that this Ananymous Profile must be unique and different then what your Users "UserName" else you will distroy some one profile or new users can delete your Data.

 

You noticed that default Property of a Profile Field is "string" otherwise one have to define one explicitly. In Our case we Defined "Age" as an "Integer"

 

To create Proile in the Registration Page see Create a User Profile in Regisration Page.

Visitors/Readers Comments



hi..thanks...the codes helps me very much!

Comments By:ct

at: 25/11/2007 21:18:38 UTC
This is nice and very helpful. Thank you

Comments By:ASP:NET

at: 28/02/2008 15:27:45 UTC



Add your Comments

Name:
 
Message:
Note: For faster response please use Forums >> for your questions instead of the comments area! (Admin)