aspdotnet-suresh offers articles and tutorials,csharp dot net, articles and tutorials,VB.NET Articles,Gridview articles,code examples of 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies


In this article I will explain how to create new roles and delete roles in membership.


In previous post I explained clearly how to install membership database schema in SQL Server  and create users programmatically in membership. Now I will explain how to create new roles and delete existing roles in membership.

Now open visual studio and create new web application and write following code in your aspx page 

<html xmlns="">
<head runat="server">
<title>Create and Delete Roles in Membership</title>
<style type="text/css">
.GridviewDiv {font-size: 100%; font-family: 'Lucida Grande', 'Lucida Sans Unicode', Verdana, Arial, Helevetica, sans-serif; color: #303933;}
Table.Gridview{border:solid 1px #df5015;}
.Gridview th{color:#FFFFFF;border-right-color:#abb079;border-bottom-color:#abb079;padding:0.5em 0.5em 0.5em 0.5em;text-align:center}
.Gridview td{border-bottom-color:#f0f2da;border-right-color:#f0f2da;padding:0.5em 0.5em 0.5em 0.5em;}
.Gridview tr{color: Black; background-color: White; text-align:left}
:link,:visited { color: #DF4F13; text-decoration:none }
<form id="form1" runat="server">
<div class="GridviewDiv">
<table align="center">
<td> <b>Enter RoleName:</b> </td>
<td><asp:TextBox ID="txtRole" runat="server"/></td>
<td><asp:Button ID="btnCreate" runat="server" Text="Create Role" onclick="btnCreate_Click" /></td>
<td colspan="2"><asp:Label ID="lblResult" runat="server" Font-Bold="true"/></td>
<td colspan="2">
<asp:GridView ID="gvRoles" runat="server" CssClass="Gridview" AutoGenerateColumns="false" AutoGenerateDeleteButton="true" onrowdeleting="gvRoles_RowDeleting">
<HeaderStyle BackColor="#df5015" />
<asp:TemplateField HeaderText="Role Name">
<asp:Label ID="lblRole" runat="server" Text='<%#Container.DataItem %>'/>
Now add the following namespaces in code behind

C# Code

using System;
using System.Drawing;
using System.Web.Security;
using System.Web.UI.WebControls;
After that write the following 

protected void Page_Load(object sender, EventArgs e)
// This Method is used to bind roles
protected void BindRolesDetails()
gvRoles.DataSource = Roles.GetAllRoles();
// This Button Click event is used to Create new Role
protected void btnCreate_Click(object sender, EventArgs e)
string roleName = txtRole.Text.Trim();
lblResult.Text = roleName + " Role Created Successfully";
lblResult.ForeColor = Color.Green;
txtRole.Text = string.Empty;
txtRole.Text = string.Empty;
lblResult.Text = roleName + " Role already exists";
lblResult.ForeColor = Color.Red;
// This RowDeleting event is used to delete Roles
protected void gvRoles_RowDeleting(object sender, GridViewDeleteEventArgs e)
Label lableRole = (Label)gvRoles.Rows[e.RowIndex].FindControl("lblRole");
lblResult.ForeColor = Color.Green;
lblResult.Text = lableRole.Text + " Role Deleted Successfully";
If you observe above code I used different methods those are  

Roles.GetAllRoles()- This method is used to get all the roles in membership database

Roles.CreateRole(roleName) – This method is used to create new role

Roles.DeleteRole(rolename,false)- This method is used to delete role based on rolename and parameter false is used to check rolename associated with any user or not


Imports System.Drawing
Imports System.Web.Security
Imports System.Web.UI.WebControls

Partial Class VBRoles
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
End If
End Sub
' This Method is used to bind roles
Protected Sub BindRolesDetails()
gvRoles.DataSource = Roles.GetAllRoles()
End Sub
' This Button Click event is used to Create new Role
Protected Sub btnCreate_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim roleName As String = txtRole.Text.Trim()
If Not Roles.RoleExists(roleName) Then
lblResult.Text = roleName & " Role Created Successfully"
lblResult.ForeColor = Color.Green
txtRole.Text = String.Empty
txtRole.Text = String.Empty
lblResult.Text = roleName & " Role already exists"
lblResult.ForeColor = Color.Red
End If
End Sub
' This RowDeleting event is used to delete Roles
Protected Sub gvRoles_RowDeleting(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs)
Dim lableRole As Label = DirectCast(gvRoles.Rows(e.RowIndex).FindControl("lblRole"), Label)
Roles.DeleteRole(lableRole.Text, False)
lblResult.ForeColor = Color.Green
lblResult.Text = lableRole.Text & " Role Deleted Successfully"
End Sub
End Class
Now set database connection settings in web.config 

First set the connectionstring like this 

<add name="dbconnection" connectionString="Data Source=SureshDasari;Initial Catalog=AspMembership;Integrated Security=true" providerName="System.Data.SqlClient"/>
After that write the following code in system.web section

<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="dbconnection" applicationName="SampleApplication"/>
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="dbconnection" applicationName="SampleApplication"/>
<roleManager enabled="true">
<add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="dbconnection" applicationName="SampleApplication"/>
In our web.config don’t forgot to set <roleManager enabled="true"> because this will enable us to work with roles concept in membership


If you enjoyed this post, please support the blog below. It's FREE!

Get the latest,, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email


Srilakshmi said...

It's very Useful for me.Good Material

Anonymous said...



Anonymous said...

thank you

Anonymous said...

i have used your code many times that is very useful to me

Anonymous said...

I am having a problem understanding Forms Authentication. This is a public internet site and all users will have access to the main part of the site. However there is a subdirectory that is restricted to certain users. I know that a user is valid because they will enter a user name and password
If user name and password match then
Set this users role to student registred.

Timotech said...

how can i resolve the problem: Cannot insert the value NULL into column 'RoleId', table 'Revenue.dbo.aspnet_Roles'; column does not allow nulls. INSERT fails. The statement has been terminated.

Timotech said...

This is the resolution: alter table aspnet_Roles alter column RoleId add ROWGUIDCOL

Give your Valuable Comments

Other Related Posts

© 2010-2012 All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.