Capturing Browser Information Using the Profile...

With ASP.NET 2.0, the Profile component can be an important tool in capturing browser information for accessing your web sites...


By: Brian Mains Spacer Date: January 26, 2006Spacer Download Spacer Download the code.

With ASP.NET 2.0, the Profile component can be an important tool in capturing browser information for accessing your web sites, though the functionality is not directly available. However, using the profile option can catalog the information, then display it in an administrative page, showing the users how many people use a certain browser. Here is how it could work, though it isn’t the most efficient process for certain reasons that I'll explain later. Take, for example the following profile:

<profile>
<properties>
   <add name="BrowserType" type="System.String"/>
   <add name="FavoriteColor" type="System.String"/>
</properties>
</profile>

An entry in the database looks like this:

PropertyNames: Title:S:0:9:FavoriteColor:S:9:3:BrowserType:S:12:7:
PropertyValuesString: Test DataRedFirefox

In the PropertyNames column, the field contains the name of the property value, the variable type (S for string), the start index value for the string in the PropertyValuesString field, and the length of the text in the field. In the ASP.NET page, the information is logged:

if (!Page.IsPostBack)
{
   Response.Write("Setting profile browsertype property");
   Profile.BrowserType = Request.Browser.Browser;
}

The problem with being forced to use this approach is that I tried to use the Session_Start, but I had an error because I believe that the Profile class isn't available at the time. I couldn't put this code in a custom page class, which each page would inherit, because the Profile class isn't available, and the ProfileCommon class's Properties collection is readonly. I really wanted to a avoid checking this at page load, but I found no other option.

Alternatively, the Session could be used in an if statement as a master if statement, avoiding checking and setting the browser type for each and every page. In the administrative page, I have the following information:

protected void Page_Load(object sender, EventArgs e)
{
   SqlDataAdapter da = new SqlDataAdapter("SELECT PropertyNames, PropertyValuesString FROM aspnet_Profile", ConfigurationManager.ConnectionStrings["express"].ConnectionString);
   DataTable dt = new DataTable();
   da.Fill(dt);

  foreach (DataRow row in dt.Rows)
   {
     if (!row.IsNull(0) && !row.IsNull(1))
     {
       string value = row[0].ToString();
       string[] parts = value.Split(':');

       for(int i = 0; i < parts.Length; i++)
       {
         string part = parts[i];
         if (part == "BrowserType")
         {
           int start = int.Parse(parts[i + 2]);
           int length = int.Parse(parts[i + 3]);
           string text = row[1].ToString();

           string browser = text.Substring(start, length).ToLower();
           if (browser == "ie")
           {
             int count = int.Parse(lblIE.Text);
             count += 1;
             lblIE.Text = count.ToString();
           }
           else if (browser == "firefox")
           {
             int count = int.Parse(lblFirefox.Text);
             count += 1;
             lblFirefox.Text = count.ToString();
           }
           else
           {
             int count = int.Parse(lblMozilla.Text);
             count += 1;
             lblMozilla.Text = count.ToString();
           }
           return;
         }
       }
     }
   }
}

This code, in the administrative page, selects the profile property names and values, parses the name string concatenation to get the property name, the start index, and the length out of the property names. It increments the appropriate label value (default value is zero) with the statistics found from the query. From the integer values, you can do anything with it.

This may seem like a hack (and maybe it is), but the key is to query the aspnet_Profile table, and get the name/value strings, parse the strings apart to get the key, start, and length values, and get the value from the string. Then take this value and increment as necessary. The profile is set whenever a page loads in the browser. Whether you use this code in the default page or every page is up to your implementation.