State Management using ASP.NET Profiles

 

Generally we maintain state by using Cookies,  View state,  Session or Application variables in Asp.Net. Other than these methods we can do the state management by using Asp.Net Profiles.  Profiles are persistent objects that means asp.net profiles maintain state information even you leave the web site and visit next time.

 

We can easily create the profiles in asp.net by declaring the corresponding profile objects in web.config file as shown below.

 

<system.web>

 

    <profile>   

       <properties>

              <add name="Name"/>

             <add name="Job"/>

      </properties> 

    </profile>

 

  </system.web>

 

 As shown above we declare the two profile objects Name and Job. By default data type of profile objects is string.

 

Store values in Profile objects by just assigning the value to the Profile.ObjectName as shown below.

 

                Profile.Name = txtName.Text;

                Profile.Job = txtJob.Text;

 

We can easily reterieving the asp.net profile objects by using the Profile.ObjectName as shown below.

 

                  lblName.Text = Profile.Name;

                lblJob.Text = Profile.Job;

 

Even we can declare prodile object for specific data type as shown below.

 

 <system.web>

 

   <profile>

       <properties>

            <add name="Id" type="Int32" defaultValue="0" />

       </properties> 

    </profile>

 

  </system.web>

 

As shown above we declare the variable Id as Int data type. Read and assign Id profile object as shown below.

 

                  //save int type data only

                  Profile.Id = 100; //assign value to Id profile object Id

                 int id = Profile.Id; // read profile object Id value

         
                                                                                                                           AspnetProfiles.zip (3.08 kb)