How to get all users in asp.net c#

Get membership all users name in asp.net
.Net framework Membership.GetAllUsers() method allow us to get a collection of all the users in the database. Membership class GetAllUsers() method exists in System.Web.Security namespace.

GetAllUesrs() method return value type is System.Web.Security.MembershipUserCollection. this return value is a MembershipUserCollection of MembershipUser objects representing all of the users in the database.

In the bellow example code, we get the collection of all membership users and data bind this collection with a ListBox server control. if the collection is large then this method degrade the performance of asp.net application.

Membership.GetAllUsers(Int32, Int32, Int32) overloaded method allow us to get a collection of all the users in the database in pages of data. this overloaded method provide paging support of membership users list.

The following asp.net c# example code demonstrate us how can we get all the membership users in database programmatically at run time in an asp.net application.
GetAllUsersExample.aspx

<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e) {
        ListBox1.DataSource = Membership.GetAllUsers();
        ListBox1.DataBind();
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>GetAllUsers method example: how to get user list in asp.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2>GetAllUsers Method Example</h2>
        <asp:Label ID="Label1" runat="server" Text="All Users" AssociatedControlID="ListBox1"></asp:Label>
        <asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Get All Users" OnClick="Button1_Click" />
    </div>
    </form>
</body>
</html>
Related asp.net example