SetPropertyValue Method

ProfileBase.SetPropertyValue Method (String, Object)


Sets the value of a profile property.

Namespace:   System.Web.Profile
Assembly:  System.Web (in System.Web.dll)

public void SetPropertyValue(
	string propertyName,
	object propertyValue


Type: System.String

The name of the property to set.

Type: System.Object

The value to assign to the property.

Exception Condition

An attempt was made to set a property value on an anonymous profile where the property's allowAnonymous attribute is false.


There are no properties defined for the current profile.


The specified profile property name does not exist in the current profile.


The provider for the specified profile property did not recognize the specified property.


An attempt was made to set a value value on a property that was marked as read-only.


An attempt was made to assign a value to a property using an incompatible type.

ASP.NET uses the ProfileBase class to create the class used for the user profile. When an application that has the user profile enabled is started, ASP.NET creates a new class of type ProfileCommon, which inherits from the ProfileBase class. Strongly typed accessors are added to the ProfileCommon class for each property defined in the configuration section. The strongly typed accessors of the ProfileCommon class call the SetPropertyValue method to pass property values to the ProfileProvider to be stored at the data source.

You can use the SetPropertyValue method to assign property values in the user profile for your application by name. Values are untyped, and type checking will be done at run time, not compile time. For strongly typed access to profile property values, you can access the property by name as a member of the Profile property that is available on each page, for example, Profile.CustomerAddress.

The following code example shows an ASP.NET page that reads and sets the ZipCode property specified for the user profile. For an example of a Web.config file that specifies properties for the user profile, see the example provided for the ProfileBase class.

System_CAPS_security Security Note

This example contains a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview.

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<script runat="server">

public void Page_PreRender()
  if (Profile.ZipCode == null)
    PersonalizePanel.Visible = false;
    GetZipCodePanel.Visible = true;
    ZipCodeLabel.Text = Profile.ZipCode;

    // Get personalized information for zip code here.

    PersonalizePanel.Visible = true;
    GetZipCodePanel.Visible = false;

public void ChangeZipCode_OnClick(object sender, EventArgs args)
  ZipCodeTextBox.Text = Profile.ZipCode;
  Profile.ZipCode = null;

  PersonalizePanel.Visible = false;
  GetZipCodePanel.Visible = true;

public void EnterZipCode_OnClick(object sender, EventArgs args)
  Profile.ZipCode = ZipCodeTextBox.Text;

<html xmlns="" >
<title>Home Page</title>

<form id="form1" runat="server">
  <table border="1" cellpadding="2" cellspacing="2">
        <asp:Panel id="PersonalizePanel" runat="Server" Visible="False">
          Information for Zip Code: <asp:Label id="ZipCodeLabel" Runat="Server" /><br />
          <!-- Information for Zip Code here. -->
          <br />
          <asp:LinkButton id="ChangeZipCodeButton" Runat="Server" Text="Change Your Zip Code"
                          OnClick="ChangeZipCode_OnClick" />
        <asp:Panel id="GetZipCodePanel" runat="Server" Visible="False">
          You can personalize this page by entering your Zip Code: 
          <asp:TextBox id="ZipCodeTextBox" Columns="5" MaxLength="5" runat="Server" />
          <asp:LinkButton id="EnterZipCodeButton" Runat="Server" Text="Go"
                          OnClick="EnterZipCode_OnClick" />


.NET Framework
Available since 2.0
Return to top
© 2015 Microsoft