In a previous section we discussed one of the ways to use our custom object in the Profile object. In this final section of the article, we will show you how to create a custom object that inherits from the Profile base class which is ProfileBase class, then configure the Profile configuration section in the application’s configuration file (web.config) to inherit from that class.
As we know, at run time an instance of the ProfileCommon class, which inherits from the ProfileBase class, is dynamically created. Using the inherits property allows the automatically generated ProfileCommon class to be created by inheriting from a class specified within its value and not from the ProfileBase base class; this can be found in the System.Web.Profile namespace.
Listing 8 shows the code for the StudentRecordInherit class that inherits from the ProfileBase base class.
Listing 8. StudentRecordInherit Inheriting from ProfileBase
using System;
using System.Web.Profile;
public class StudentRecordInherit : ProfileBase
{
#region Private Fields
private int _StudentId;
private string _Fname;
private string _Lname;
#endregion
#region Property Section
public int StudentId
{
get { return _StudentId; }
set { _StudentId = value; }
}
public string FirstName
{
get { return _Fname; }
set { _Fname = value; }
}
public string LastName
{
get { return _Lname; }
set { _Lname = value; }
}
#endregion
}
Listing 9 presents the Profile configuration section. There is no need to add any property; only the inherits property should be assigned to a custom class inheriting from ProfileBase base class.
Listing 9. Profile configuration section using Inherits
<anonymousIdentification enabled="true"/>
<Profile enabled="true" inherits="StudentRecord" />
Listing 10 demonstrates the use of the Profile object when inheriting from a custom class. When using the inherits property, all the properties of the custom class would be properties of the Profile object, since the ProfileCommon is auto-generated from that custom class.
Figure 4. Testing Inherits Property
Listing 10. Testing Inherits Property
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Profile;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class TestInherits : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Profile.StudentId = 1;
Profile.FirstName = "Bilal";
Profile.LastName = "Haidar";
Response.Write("Student Id : " + Profile.StudentId + "<br />");
Response.Write("First Name : " + Profile.FirstName + "<br />");
Response.Write("Last Name : " + Profile.LastName + "<br />");
}
}
We have seen two ways in which we can use our custom objects as part of the Profile object. Logically speaking, it would be better to go with the second option, in which we create our custom object inheriting from the ProfileBase class. This would give us full control over the properties which are part of the ProfileCommon instance created at run time.