Introduction:
In this article I will explain how to edit, update and delete user account details programmatically in asp.net membership.
Description:
In previous post I explained clearly how to install asp.net membership database schema in SQL Server and create users using CreateUserWizard in asp.net membership and programmatically insert user details in asp.net membership. Now I will explain how to modify user account details in asp.net membership.
Here don’t forgot to set database connection settings in web.config
In this article I will explain how to edit, update and delete user account details programmatically in asp.net membership.
Description:
In previous post I explained clearly how to install asp.net membership database schema in SQL Server and create users using CreateUserWizard in asp.net membership and programmatically insert user details in asp.net membership. Now I will explain how to modify user account details in asp.net 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="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();
}
|
VB.NET Code
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
BindGridview()
End If
End Sub
' This Method is used to bind gridview
Protected Sub BindGridview()
gvDetails.DataSource = Membership.GetAllUsers()
gvDetails.DataBind()
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
BindGridview()
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
BindGridview()
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
Membership.DeleteUser(username)
lblResult.Text = String.Format("{0} Details deleted Successfully", username)
lblResult.ForeColor = Color.Green
BindGridview()
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
Membership.UpdateUser(user)
lblResult.Text = String.Format("{0} Details updated Successfully", username)
lblResult.ForeColor = Color.Green
End If
gvDetails.EditIndex = -1
BindGridview()
End Sub
End Class
|
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
|
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
9 comments :
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?
Hai! your website is really beautiful.ya i have learned valuable data about asp.net... that was very great.. why don't publish entity frame work.
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
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 , muqeet.522@gmail.com is my e mail if any one have the code please frwrd ...
thanx a lot.....this help me
hai please explain the statemanagement
I still can't edit, but I think it might enhance everyone’s understanding.
Suresh: Nice job - very helpful. Now can you help me add the "IsLockedOut" and "IsApproved" fields to your screen? ..............Phil Hoop Email is philhoop@mm.com Thanks
How to get Password From membership table to Update Security Question And Security Answer???
Note: Only a member of this blog may post a comment.