SqlRoleProvider.DeleteRole Method
Removes a role from the role database.
Assembly: System.Web (in System.Web.dll)
'Declaration Public Overrides Function DeleteRole ( _ roleName As String, _ throwOnPopulatedRole As Boolean _ ) As Boolean
Parameters
- roleName
- Type: System.String
The name of the role to delete.
- throwOnPopulatedRole
- Type: System.Boolean
If true, throws an exception if roleName has one or more members.
| Exception | Condition |
|---|---|
| System.ArgumentNullException | roleName is Nothing (Nothing in Visual Basic). |
| System.ArgumentException | roleName is an empty string or contains a comma. -or- roleName is longer than 256 characters. |
| System.Configuration.Provider.ProviderException | roleName has one or more members and throwOnPopulatedRole is true. -or- An unknown error occurred while communicating with the database. |
The DeleteRole method is called by the Roles class to delete a role from the SQL Server database specified in the ASP.NET application's configuration file (Web.config). When a role is deleted, the list of users associated with that role is also deleted from the database. The user information in the database is not affected.
If throwOnPopulatedRole is true, then an exception will be thrown and the role will not be deleted if the role identified by the roleName parameter has one or more members. If throwOnPopulatedRole is false, then the role will be deleted whether it is empty or not.
The following example deletes a role from the database. For an example of a Web.config file that enables role management, see SqlRoleProvider.
<%@ Page Language="VB" %> <%@ 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"> Dim rolesArray() As String Public Sub Page_Load(ByVal sender As Object, ByVal args As EventArgs) If Not IsPostBack Then ' Bind roles to ListBox. rolesArray = Roles.GetAllRoles() RolesListBox.DataSource = rolesArray RolesListBox.DataBind() End If End Sub Public Sub DeleteRole_OnClick(ByVal sender As Object, ByVal args As EventArgs) Dim delRole As String Try delRole = RolesListBox.SelectedItem.Value Roles.DeleteRole(delRole) Msg.Text = "Role '" & Server.HtmlEncode(delRole) & "' deleted." ' Re-bind roles to ListBox. rolesArray = Roles.GetAllRoles() RolesListBox.DataSource = rolesArray RolesListBox.DataBind() Catch Msg.Text = "Role '" & Server.HtmlEncode(delRole) & "' <u>not</u> deleted." End Try End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>Sample: Delete Role</title> </head> <body> <form runat="server" id="PageForm"> <h3> Delete Role</h3> <asp:Label ID="Msg" ForeColor="maroon" runat="server" /><br /> <table border="0"> <tr> <td valign="top"> Delete Role:</td> <td valign="top"> <asp:ListBox ID="RolesListBox" runat="server" Rows="8" /></td> <td valign="top"> <asp:Button Text="Delete Role" ID="DeleteRoleButton" runat="server" OnClick="DeleteRole_OnClick" /></td> </tr> </table> </form> </body> </html>
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.