Friday, February 8, 2008

ASP.Net Profile

Profile is a ASP.Net 2.0 State Management option.

The greatest drawback with other state management options like Query String, Session Variable, Cookie and ViewState is lack of intellisense, which is the cause for hard to track bugs.
For example if you define a QueryString called Name in Default.aspx and refer to the same as Nam in Welcome.aspx, you won't get an error message instead a new QueryString variable called Nam will be created so obviously unpredicted result.

Profile supports intellisense.

Profile is strongly typed and you can specify default value.


Start out be creating Profile propertis in web.config












Make sure you run the application before using the profile properties.

You will get intellisense of Profile properties.

//Default.aspx
protected void btnSubmit_Click(object sender, EventArgs e)
{
Profile.Name = txtName.Text;
Profile.Blog = txtBlog.Text;
Profile.Rate = 400 + 100;
Response.Redirect("Welcome.aspx");
}


Here note that we are not assigning value to Specialization Profile property and we can apply arithmetic operations on Rate Profile property since its type is Int32.

We are accessing the value in Welcome.aspx

//Welcome.aspx
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(String.Format("{0} 's blog is {1} specializing in {2} hourly rate is {3} INR", Profile.Name, Profile.Blog, Profile.Specialization, Profile.Rate ));
}

Group of Personalization Properties

lt;profile>

<properties>
<group name ="Person">
<add name ="Name"/>
<add name ="Location"/>
</group>
</properties>
</profile>

//Default.aspx
protected void btnSubmit_Click(object sender, EventArgs e)
{
Profile.Person.Name = "Shalvin";
Profile.Person.Location = "Kochi";
Response.Redirect("Welcome.aspx");

}

//Welcome.aspx
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(String.Format("I am {0} my location is {1}", Profile.Person.Name, Profile.Person.Location));
}


Using Sql Server Instance Database

>aspnet_reqsql -E -S .\shalvin -d ShalvinProfile -A p



<connectionStrings>
<add name="ShalvinProfileConnectionString"
connectionString="Data Source=.\shalvin;Initial Catalog=ShalvinProfile;Integrated Security=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>

<system.web>

<profile>
<providers>
<clear/>
<add name="AspNetSqlProfileProvider"
type="System.Web.Profile.SqlProfileProvider"
connectionStringName="ShalvinProfileConnectionString"/>
</providers>
<properties>
<add name="Name"/>
</properties>
</profile>
</system.web>







Related Blog

Query String Asp .Net : Working with multiple values

Session Asp .Net : Storing DataSet

No comments:

Post a Comment