SqlMembershipProvider.UpdateUser Method
Updates information about a user in the SQL Server membership database.
Assembly: System.Web (in System.Web.dll)
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.
Note: |
|---|
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>
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Note: