Roles.DeleteRole Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Removes a role from the data source.
Overloads
DeleteRole(String, Boolean) |
Removes a role from the data source. |
DeleteRole(String) |
Removes a role from the data source. |
DeleteRole(String, Boolean)
Removes a role from the data source.
public:
static bool DeleteRole(System::String ^ roleName, bool throwOnPopulatedRole);
public static bool DeleteRole (string roleName, bool throwOnPopulatedRole);
static member DeleteRole : string * bool -> bool
Public Shared Function DeleteRole (roleName As String, throwOnPopulatedRole As Boolean) As Boolean
Parameters
- roleName
- String
The name of the role to delete.
- throwOnPopulatedRole
- Boolean
If true
, throws an exception if roleName
has one or more members.
Returns
true
if roleName
was deleted from the data source; otherwise; false
.
Exceptions
roleName
is null
.
roleName
is an empty string.
roleName
has one or more members and throwOnPopulatedRole
is true
.
-or-
Role management is not enabled.
Examples
The following code example deletes a role from the data source. For an example of a Web.config file that enables role management, see Roles.
<%@ 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">
string[] rolesArray;
public void Page_Load(object sender, EventArgs args)
{
if (!IsPostBack)
{
// Bind roles to ListBox.
rolesArray = Roles.GetAllRoles();
RolesListBox.DataSource = rolesArray;
RolesListBox.DataBind();
}
}
public void DeleteRole_OnClick(object sender, EventArgs args)
{
string delRole = "";
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.";
}
}
</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>
<%@ 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>
Remarks
The DeleteRole method removes a role name from the data source. DeleteRole calls the DeleteRole method of the default role provider to remove the specified role from the data source.
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.
See also
Applies to
DeleteRole(String)
Removes a role from the data source.
public:
static bool DeleteRole(System::String ^ roleName);
public static bool DeleteRole (string roleName);
static member DeleteRole : string -> bool
Public Shared Function DeleteRole (roleName As String) As Boolean
Parameters
- roleName
- String
The name of the role to delete.
Returns
true
if roleName
was deleted from the data source; otherwise, false
.
Exceptions
roleName
is null
.
roleName
is an empty string or contains a comma (,).
Examples
The following code example deletes a role from the data source. For an example of a Web.config file that enables role management, see Roles.
<%@ 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">
string[] rolesArray;
public void Page_Load(object sender, EventArgs args)
{
if (!IsPostBack)
{
// Bind roles to ListBox.
rolesArray = Roles.GetAllRoles();
RolesListBox.DataSource = rolesArray;
RolesListBox.DataBind();
}
}
public void DeleteRole_OnClick(object sender, EventArgs args)
{
string delRole = "";
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.";
}
}
</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>
<%@ 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>
Remarks
The DeleteRole method removes a role name from the data source. DeleteRole calls the DeleteRole method of the default role provider to remove the specified role from the data source.
If the role identified by the roleName
parameter has one or more members, then an exception will be thrown and the role will not be deleted.