In the previous section we demonstrated how to define simple properties in the profile section of the web.config file. Sometimes when developing a web application, we may need to group a set of properties within the Profile object. For instance, one might be developing a web application for a school, where the application needs to store a student’s details in a Profile object, in addition to storing other Profile properties. The way to do so is to place the student’s details in a group section in the profile configuration section. In Listing 3 below, we have a profile configuration section that includes a group section named Student, which defines the student details of FirstName, LastName, and StudentId.
Listing 3. Group Profile Property Configuration
<configuration>
<system.web>
<authentication mode="forms" />
<anonymousIdentification enabled="true" />
<profile enabled="true">
<properties>
<group name="Student">
<add name="FirstName" allowAnonymous="true" />
<add name="LastName" allowAnonymous="true" />
<add name="StudentId" allowAnonymous="true" type="Int32" />
</group>
</properties>
</profile>
</system.web>
</configuration>
Listing 3 defines a group section named Student and specifies its three properties. The first two, FirstName and LastName, do not have a defined type, which means that they are of the default type of string. The third property, which is StudentId, has a type of Int32 representing the student's ID number.
Once the group profile configuration section is defined, we can start accessing the Profile object’s Student property from the server-side code, as shown in the code below:
Profile.Student.StudentId = 9898;
Profile.Student.FirstName = "Johny";
Profile.Student.LastName = "White";
The page in Listing 4 shows how to use a Profile group object to persist a student’s details. This page displays a form to view and modify the values of the Profile's Student group property (see Figure 2).
Figure 2. Group Profile Properties

Listing 4. GroupProperties.aspx (C#)
<%@ Page Language="C#" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
StudentIDTextbox.Text = Profile.Student.StudentId.ToString();
FirstNameTextbox.Text = Profile.Student.FirstName;
LastNameTextbox.Text = Profile.Student.LastName;
}
}
protected void Modify_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(StudentIDTextbox.Text))
{
int IntId;
Profile.Student.StudentId =
(int.TryParse(StudentIDTextbox.Text, out IntId)) ? IntId : 0;
}
if (!string.IsNullOrEmpty(FirstNameTextbox.Text))
Profile.Student.FirstName = FirstNameTextbox.Text;
if (!string.IsNullOrEmpty(LastNameTextbox.Text))
Profile.Student.LastName = LastNameTextbox.Text;
}
protected void Page_PreRender(object sender, EventArgs e)
{
StudentIDLabel.Text = Profile.Student.StudentId.ToString();
FirstNameLabel.Text = Profile.Student.FirstName;
LastNameLabel.Text = Profile.Student.LastName;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Group Profile Properties</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>
Group Profile Properties</h1>
<h3>
Current Properties</h3>
<p>
Student ID : <asp:Label ID="StudentIDLabel" runat="server" /><br />
First Name : <asp:Label ID="FirstNameLabel" runat="server" /><br />
Last Name : <asp:Label ID="LastNameLabel" runat="server" /><br />
</p>
<hr />
<h3>
Modify Properties</h3>
<p>
Student ID : <asp:TextBox ID="StudentIDTextbox" runat="server" /><br />
First Name : <asp:TextBox ID="FirstNameTextbox" runat="server" /><br />
Last Name : <asp:TextBox ID="LastNameTextbox" runat="server" /><br />
</p>
<p>
<asp:Button ID="Modify" OnClick="Modify_Click" Text="Modify Student Profile"
runat="server" />
</p>
</div>
</form>
</body>
</html>
Once the Modify Student Profile button is clicked, the Profile's Student group properties are updated with the new values entered.
One thing to mention is that we cannot nest one group section within another group section. However, we can have as many group sections as we want in the Profile configuration section.