SqlMembershipProvider.UpdateUser Method (MembershipUser)

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Updates information about a user in the SQL Server membership database.

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

public override void UpdateUser(
	MembershipUser user
)

Parameters

user
Type: System.Web.Security.MembershipUser

A MembershipUser object that represents the user to update and the updated information for the user.

Exception Condition
System.ArgumentNullException

user is null.

- or -

The UserName property of user is null.

- or -

The Email property of user is null and RequiresUniqueEmail is set to true.

System.ArgumentException

The UserName property of user is an empty string (""), contains a comma, or is longer than 256 characters.

- or -

The Email property of user is longer than 256 characters.

- or -

The Email property of user is an empty string and RequiresUniqueEmail is set to true.

System.Configuration.Provider.ProviderException

The UserName property of user was not found in the database.

- or -

The Email property of user was equal to an existing e-mail address in the database and RequiresUniqueEmail is set to true.

- or -

The user update failed.

This method is called by the Membership class to update user information for a user in the SQL Server database specified in the ASP.NET application's configuration file (Web.config). The Email, Comment, IsApproved, LastLoginDate, and LastActivityDate property values are updated for the specified membership user.

The maximum length for the UserName property is 256 characters. The maximum length for the Email property is 256 characters.

The password for a membership user cannot be updated using the UpdateUser method. To update the password for a membership user, use the ChangePassword method of the MembershipUser class.

The following code example updates the e-mail address for a user.

System_CAPS_noteNote

This example uses the Membership class to call the SqlMembershipProvider specified as the defaultProvider in the Web.config file. If you need to access the default provider as the type SqlMembershipProvider, you can cast the Provider property of the Membership class. To access other configured providers as a specific provider type, you can access them by their configured name with the Providers property of the Membership class and cast them as the specific provider type.

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Security" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

MembershipUser u;

public void Page_Load(object sender, EventArgs args)
{
  u = Membership.GetUser(User.Identity.Name);

  if (!IsPostBack)
  {
    EmailTextBox.Text = u.Email; 
  }
}

public void UpdateEmailButton_OnClick(object sender, EventArgs args)
{
  try
  {
    u.Email = EmailTextBox.Text;

    Membership.UpdateUser(u);

    Msg.Text = "User e-mail updated.";
  }
  catch (System.Configuration.Provider.ProviderException e)
  {
    Msg.Text = e.Message;
  }
}

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Sample: Update User E-Mail</title>
</head>
<body>

<form id="form1" runat="server">
  <h3>Update E-Mail Address for <%=User.Identity.Name%></h3>

  <asp:Label id="Msg" ForeColor="maroon" runat="server" /><br />

  <table cellpadding="3" border="0">
    <tr>
      <td>E-mail Address:</td>
      <td><asp:TextBox id="EmailTextBox" MaxLength="128" Columns="30" runat="server" /></td>
      <td><asp:RequiredFieldValidator id="EmailRequiredValidator" runat="server"
                                    ControlToValidate="EmailTextBox" ForeColor="red"
                                    Display="Static" ErrorMessage="Required" /></td>
    </tr>
    <tr>
      <td></td>
      <td><asp:Button id="UpdateEmailButton" 
                      Text="Update E-mail" 
                      OnClick="UpdateEmailButton_OnClick" 
                      runat="server" /></td>
    </tr>
  </table>
</form>

</body>
</html>

.NET Framework
Available since 2.0
Return to top
Show: