About Me

My photo
Hello, I'm a technology geek looking to upgrade my programming skills. These are some of the things I'm learning along my journey.

Sunday, June 24, 2012

Creating and Storing Profiles in ASP.NET - Simplified

A good web site will remember the visitor even he/she does not log in to the site. There are lots of possible things to remember such as color scheme, shipping address, etc. The visitor is more likely to return in the future when personal preferences are stored and retrieved upon revisiting.

Let me start with a very basic example so you can get the picture without the clutter.

Start a new blank web site in Visual Studio 2010. Click on "Website" on top of screen. Select "ASP.NET configuration". Click on the "Security" tab. Under "Users" click on "Select Authentication Type".Select "From the Internet". Click on "Done". Under "Users" click on "Create user". Fill in "Create User" boxes and click "Create User". Click on "Back". Close browser program. Close VS 2010. Reopen VS 2010. The “App_Data” folder should be visible in the "Solution Explorer" with the "ASPNETDB.MDF" file in the folder. The “Database Explorer” should have the "ASPNETDB.MDF" file. Open up the "ASPNETDB.MDF" file in the "Database Explorer".
Open the "Tables" folder to find the "aspnet_Profile" table. Right click on the "aspnet_Profile" table and select "Show Table Data". All the cells should be null. In summary, Visual Studio has autogenerated a SQL Server database with the necessary table, fields and stored procedures to make it easy for you to work with profiles.

Click to enlarge: 



Modify the “web.config” file to accept anonymous users:


Add four properties in the “profile” section with the anonymous attribute set to true:
example:


Add some text boxes, labels and drop down controls to test your program.

Click to enlarge.




Initial the text controls with the existing entries in the Profile table.
example:
if (Profile.Name != "")
           {
               LabelName.Text = Profile.Name;
               LabelName.Visible = true;
               lblWelcome.Visible = true;
           }
           if (Profile.Age != 0)
           {
               LabelAge.Text = Profile.Age.ToString();
               LabelAge.Visible = true;
               lblAge.Visible = true;
           }
           if (Profile.City != "")
           {
               LabelCity.Text = Profile.City;
               LabelCity.Visible = true;
               lblCity.Visible = true;
           }
Create an event handlers for your submit button, drop-down list, etc.
example:
protected void SubmitButton_Click(object sender, EventArgs e)
    {
       Profile.Name = TextBoxName.Text;
       Profile.Age = Int16.Parse(TextBoxAge.Text);
       Profile.City = TextBoxCity.Text;
       LabelName.Text = Profile.Name;
       LabelName.Visible = true;
       lblWelcome.Visible = true;
       LabelAge.Text = Profile.Age.ToString();
       LabelAge.Visible = true;
       lblAge.Visible = true;
       LabelCity.Text = Profile.City;
       lblCity.Visible = true;
       LabelCity.Visible = true;
       Profile.Color = "none";
       lblColorSelectionText.Visible = true;
       cboColor.Visible = true;
       
    }
protected void cboColor_SelectedIndexChanged(object sender, EventArgs e)
    {
       lblColorText.Visible = true;
       switch (cboColor.SelectedItem.ToString())
       {
           case "red":
               lblColor.Visible = true;
               Profile.Color = "Red";
               lblColor.Text = Profile.Color;
               break;
           case "blue":
               lblColor.Visible = true;
               Profile.Color = "Blue";
               lblColor.Text = Profile.Color;
               break;
           case "green":
               lblColor.Visible = true;
               Profile.Color = "Green";
               lblColor.Text = Profile.Color;
               break;
       }
You can store objects of any type using profiles. The profile feature provides a generic storage feature that allows you to define and maintain almost any kind of data while still making the data available in a type-safe manner which means your intellisense works. This is in contrast to the session object which doesn’t have this advantage.

Now run the program and check the table data of the "aspnet_Profile" table. You should see entries in the “PropertyNames” and “PropertyValuesString” fields.

If you run the program a second time with different profile data, you see that the first row in the "aspnet_Profile" table has been overwritten. The reason for this is that you are the same user to the ASP.NET server so it doesn’t add a new row for you. If you want to simulate a different user, just copy the web address at the top of the browser and open a different browser with the copied address pasted new browser. Example: You started your program for the first time in Firefox. Start the program for the second time in “Chrome”. Once you do this the table should have a second row with data written to it.

You can download the complete program here.

Click here for a more thorough explanation of profiles.

No comments:

Post a Comment