Introduction:
In this article I will explain how to edit, update and delete user account details programmatically in asp.net membership.
Description:
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.
Design Code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Edit,update and delete User accounts in asp.net 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 }
</style>
</head>
<body>
<form id="form1" runat="server">
<div class="GridviewDiv">
<asp:GridView ID="gvDetails" runat="server" CssClass="Gridview" AutoGenerateColumns="false"
AutoGenerateDeleteButton="true" AutoGenerateEditButton="true"
onrowcancelingedit="gvDetails_RowCancelingEdit"
onrowdeleting="gvDetails_RowDeleting" onrowediting="gvDetails_RowEditing"
onrowupdating="gvDetails_RowUpdating">
<HeaderStyle BackColor="#df5015" />
<Columns>
<asp:BoundField DataField="UserName" HeaderText="UserName" ReadOnly="true"/>
<asp:BoundField DataField="Email" HeaderText="Email" />
</Columns>
</asp:GridView>
<asp:Label ID="lblResult" runat="server" Font-Bold="true"/>
</div>
</form>
</body>
</html>
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)
{
BindGridview();
}
}
// This Method is used to bind gridview
protected void BindGridview()
{
gvDetails.DataSource = Membership.GetAllUsers();
gvDetails.DataBind();
}
// This event is used to cancel the edit mode
protected void gvDetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvDetails.EditIndex = -1;
BindGridview();
}
// This event is used to make our girdivew editable
protected void gvDetails_RowEditing(object sender, GridViewEditEventArgs e)
{
gvDetails.EditIndex = e.NewEditIndex;
BindGridview();
}
// 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;
Membership.DeleteUser(username);
lblResult.Text = string.Format("{0} Details deleted Successfully", username);
lblResult.ForeColor = Color.Green;
BindGridview();
}
// 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;
Membership.UpdateUser(user);
lblResult.Text = string.Format("{0} Details updated Successfully", username);
lblResult.ForeColor = Color.Green;
}
gvDetails.EditIndex = -1;
BindGridview();
}
Here don’t forgot to set database connection settings in web.config
First set the
connectionstring like this
<connectionStrings>
<add name="dbconnection" connectionString="Data Source=SureshDasari;Initial Catalog=AspMembership;Integrated Security=true" providerName="System.Data.SqlClient"/>
</connectionStrings>
After that write the following code in
system.web section
<membership>
<providers>
<clear/>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="dbconnection" applicationName="SampleApplication"/>
</providers>
</membership>
<profile>
<providers>
<clear/>
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="dbconnection" applicationName="SampleApplication"/>
</providers>
</profile>
<roleManager enabled="false">
<providers>
<clear/>
<add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="dbconnection" applicationName="SampleApplication"/>
</providers>
</roleManager>
Now run your application and check your output
Demo