How to use ASP.NET Membership class to retrieve users from the store in VB.NET

If you want to retrieve a single user and a list of users through the Membership class from the membership store, you can create a simple page with a GridView control for binding the users to the grid, as follows:
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head runat=”server”>
<title>Display Users Page</title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<asp:GridView ID=”UsersGridView” runat=”server” DataKeyNames=”UserName” AutoGenerateColumns=”False”>
<Columns>
<asp:BoundField DataField=”UserName” HeaderText=”Username” />
<asp:BoundField DataField=”Email” HeaderText=”Email” />
<asp:BoundField DataField=”CreationDate” HeaderText=”Creation Date” />
<asp:CommandField ShowSelectButton=”True” />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
In the markup the GridView defines the UserName field as DataKeyName and this enables you to access the UserName value of the currently selected user directly through the grid’s SelectedValue property. With this page in place, you can now add the following code to the Page_Load event procedure for loading the users from the membership store and binding them to the grid:
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.Security
Public Class MembershipUsers
Inherits System.Web.UI.Page
Dim _MyUsers As MembershipUserCollection
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
_MyUsers = Membership.GetAllUsers()
UsersGridView.DataSource = _MyUsers
If Not IsPostBack Then
UsersGridView.DataBind()
End If
End Sub
End Class
The next picture shows the application in action:
The custom user management application in action in VB.NET

The custom user management application in action in VB.NET

The Membership class includes a GetAllUsers method, which returns an instance of type MembershipUserCollection. You can use this collection just like any other collection. Every entry contains all the properties of a single user. If you want to display the details of a selected user, you just need to add a couple of controls for displaying the contents of the selected user in the previously created page, as follows:
<form id=”form1″ runat=”server”>
<div>
<asp:GridView ID=”UsersGridView” runat=”server” DataKeyNames=”UserName”
AutoGenerateColumns=”False”
onselectedindexchanged=”UsersGridView_SelectedIndexChanged”>
<Columns>
<asp:BoundField DataField=”UserName” HeaderText=”Username” />
<asp:BoundField DataField=”Email” HeaderText=”Email” />
<asp:BoundField DataField=”CreationDate” HeaderText=”Creation Date” />
<asp:CommandField ShowSelectButton=”True” />
</Columns>
</asp:GridView>
Selected user: <br />
<table border=”1″ bordercolor=”cyan”>
<td>User Name:
<td><asp:Label ID=”UsernameLabel” runat=”server” />
<td>Email:
<td><asp:TextBox ID=”EmailText” runat=”server” />
<td>Last Login Date:
<td><asp:Label ID=”LastLoginLabel” runat=”server” />
<td>Comment:
<td><asp:TextBox ID=”CommentTextBox” runat=”server” TextMode=”multiline” />
<td><asp:CheckBox ID=”IsApprovedCheck” runat=”server” Text=”Approved” />
<td><asp:CheckBox ID=”IsLockedOutCheck” runat=”Server” Text=”Locked Out” />
</table>
</div>
</form>
You can then handle the SelectedIndexChanged event of the previously added GridView control for filling these fields with the appropriate values, as follows:
Protected Sub UsersGridView_SelectedIndexChanged(sender As Object, e As EventArgs) Handles UsersGridView.SelectedIndexChanged
Dim Current As MembershipUser
If UsersGridView.SelectedIndex >= 0 Then
Current = _MyUsers(CStr(UsersGridView.SelectedValue))
UsernameLabel.Text = Current.UserName
LastLoginLabel.Text = Current.LastLoginDate.ToShortDateString()
EmailText.Text = Current.Email
CommentTextBox.Text = Current.Comment
IsApprovedCheck.Checked = Current.IsApproved
IsLockedOutCheck.Checked = Current.IsLockedOut
End If
End Sub
The next picture presents the new code in action:
Handling the SelectedIndexChanged event in the custom user management application in VB.NET

Handling the SelectedIndexChanged event in the custom user management application in VB.NET