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

Edit,update,delete user account details in membership

Jan 3, 2012

In this article I will explain how to edit, update and delete user account details programmatically in membership.


In previous post I explained clearly how to install membership database schema in SQL Server  and create users using CreateUserWizard in membership and programmatically insert user details in membership. Now I will explain how to modify user account details in membership.

Here I am using above concepts to setup membership database in our SQL Server and insert user details in database.

After completion of database setup and user accounts creation open visual studio and create new web application after that open aspx page and write following code

<html xmlns="">
<head id="Head1" runat="server">
<title>Edit,update and delete User accounts 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">
<asp:GridView ID="gvDetails" runat="server" CssClass="Gridview" AutoGenerateColumns="false" 
AutoGenerateDeleteButton="true" AutoGenerateEditButton="true"
onrowdeleting="gvDetails_RowDeleting" onrowediting="gvDetails_RowEditing"
<HeaderStyle BackColor="#df5015" />
<asp:BoundField DataField="UserName" HeaderText="UserName" ReadOnly="true"/>
<asp:BoundField DataField="Email" HeaderText="Email" />
<asp:Label ID="lblResult" runat="server" Font-Bold="true"/>
Now open code behind file and add following namespaces

C# Code

using System;
using System.Drawing;
using System.Web.Security;
using System.Web.UI.WebControls;
After add namespaces write the following code 

private string username;
private string email;
protected void Page_Load(object sender, EventArgs e)
if (!IsPostBack)
// This Method is used to bind gridview
protected void BindGridview()
gvDetails.DataSource = Membership.GetAllUsers();
// This event is used to cancel the edit mode
protected void gvDetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
gvDetails.EditIndex = -1;
// This event is used to make our girdivew editable
protected void gvDetails_RowEditing(object sender, GridViewEditEventArgs e)
gvDetails.EditIndex = e.NewEditIndex;
// This event is used to delete our gridview records
protected void gvDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
username = gvDetails.Rows[e.RowIndex].Cells[1].Text;
lblResult.Text = string.Format("{0} Details deleted Successfully", username);
lblResult.ForeColor = Color.Green;
// This event is used to update gridview data
protected void gvDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
int index = gvDetails.EditIndex;
GridViewRow gvrow = gvDetails.Rows[index];
username = gvDetails.Rows[e.RowIndex].Cells[1].Text;
email = ((TextBox)gvrow.Cells[2].Controls[0]).Text;
MembershipUser user = Membership.GetUser(username);
if (user != null)
user.Email = email;
lblResult.Text = string.Format("{0} Details updated Successfully", username);
lblResult.ForeColor = Color.Green;
gvDetails.EditIndex = -1;


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

Partial Class Default
Inherits System.Web.UI.Page
Private username As String
Private email As String
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 gridview
Protected Sub BindGridview()
gvDetails.DataSource = Membership.GetAllUsers()
End Sub
' This event is used to cancel the edit mode
Protected Sub gvDetails_RowCancelingEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
gvDetails.EditIndex = -1
End Sub
' This event is used to make our girdivew editable
Protected Sub gvDetails_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
gvDetails.EditIndex = e.NewEditIndex
End Sub
' This event is used to delete our gridview records
Protected Sub gvDetails_RowDeleting(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs)
username = gvDetails.Rows(e.RowIndex).Cells(1).Text
lblResult.Text = String.Format("{0} Details deleted Successfully", username)
lblResult.ForeColor = Color.Green
End Sub
' This event is used to update gridview data
Protected Sub gvDetails_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
Dim index As Integer = gvDetails.EditIndex
Dim gvrow As GridViewRow = gvDetails.Rows(index)
username = gvDetails.Rows(e.RowIndex).Cells(1).Text
email = DirectCast(gvrow.Cells(2).Controls(0), TextBox).Text
Dim user As MembershipUser = Membership.GetUser(username)
If user IsNot Nothing Then
user.Email = email
lblResult.Text = String.Format("{0} Details updated Successfully", username)
lblResult.ForeColor = Color.Green
End If
gvDetails.EditIndex = -1
End Sub
End Class
Here don’t forgot to 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="false">
<add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="dbconnection" applicationName="SampleApplication"/>
Now run your application and check your output 


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


Ajay said...

Perfect!Thanks a lot...!! How can we sort users on alphabetically order like giving "ABCD to z" on top of the grid view and when we click on letter "B" all usersnames with B must display on gridview? and so on.. Can you please help with this?

Anonymous said...

Hai! your website is really beautiful.ya i have learned valuable data about that was very great.. why don't publish entity frame work.

Anonymous said...

It is working but if you have 500 users how can you manage paging. it gives error message when you allow paging. Thanks for your help

asif said...

How can i use this code and create Login for user and admin by there role??? can any one help me with links or code , is my e mail if any one have the code please frwrd ...

Anonymous said...

thanx a lot.....this help me

Unknown said...

hai please explain the statemanagement

hermes said...

I still can't edit, but I think it might enhance everyone’s understanding.

PhilHoop said...

Suresh: Nice job - very helpful. Now can you help me add the "IsLockedOut" and "IsApproved" fields to your screen? ..............Phil Hoop Email is Thanks

Unknown said...

How to get Password From membership table to Update Security Question And Security Answer???

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

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