German Wear Discount Shop - Click Here Write for Dotnet-friends and earn for your each submission [Dot]Net-Friends
Skip Navigation Links
Home
Latest
Fast Code
Articles
Tutorials
Online Resources
Forums
Login   | Hi, Guest


Searching Users by their Profiles and Displying results in the Picture listing

Written by omerkamal on Jan 04, 2007
How to search users by their Profile in the latest version of ASP .NET?

Explanation:

.NET Framework 2.0 offered new Membership and Role Managment. Many hobby programmers are busy in their new web pages with Visual Web Developer and Visual Studio 2005.

Frankly speaking Microsoft won the hearts of hobby Programmers with free launching of Express Developing Tools (Visual C# Express, Visual Basic Express, VC++ Express and VWD Express). 

I saw some guys asking about  "Searching Users by their Profiles in ASP .NET 2.0" . It made me curious. I did a little research. I searched the web but could not find some help.

The First question which arises is what kind of  "Data Displayer" we can use to Display our results i.e best choice of the "Data Presenter". We want to display users in the Pictures view. My choice is a "DataList". Why? Because this "Data Presenter" have a speacial property called "RepeatColumns".

Now see our DataList in the .aspx Page:

BorderColor="Silver" HorizontalAlign="Center" RepeatDirection="Horizontal">

 


 

 

  

 


<%# Eval("Name") %> Picture

  

 
 

UserProfile.aspx is our Distanation Page where user will go to view the "Clicked user Profile". Where as "PicHandler.ashx" is our Picture Handler which server us to bring the picture from our DataBase. For more detailes how to save and retrive Pictures to DataBase see Saving and Retrieving Images From SQL Server Using ADO.NET 2.0 and to Display images in a gridview see Displaying an Image in a Gridview .

Ok, now we have basic of how to display a picture in a "DataList".  Now, will put four more controles on the Page.

  1. TextBox called "TextBoxSearch" . It will be use for keywords input.
  2. Button called "Buttonsearch" with Onclick event "ButtonSearch_Click".
  3. Label called "LabelNoResult" whose Fore Color is set to "crimson".
  4. A "RequiredFieldValidator" with Controle to validate "TextBoxSearch"

Now, Our page is ready. what we are going to do next is to bind the above DataList with a DatTable. This DataTable is return by our Function like this;

public DataTable SearchedUsersDataTable(string keyTosearch)
{
DataTable dt = new DataTable("Users");
dt.Columns.Add("Name", typeof(string));

MembershipUserCollection memUserCol = Membership.GetAllUsers();
ProfileCommon prof;
string searchKey = keyTosearch.ToLower();
foreach (MembershipUser Tuser in memUserCol)
{
   prof = Profile.GetProfile(Tuser.UserName);

    if (Tuser.UserName.ToLower().Contains(searchKey))
    {
      dt.Rows.Add(new Object[] { Tuser.UserName });
    }
   else if (prof.FirstName.ToLower().Contains(searchKey))
   {
      dt.Rows.Add(new Object[] { Tuser.UserName });
    }
    else if (prof.LastName.ToLower().Contains(searchKey))
    {
      dt.Rows.Add(new Object[] { Tuser.UserName });
    }
}
return dt;
}


We created a Custom DataTable with a column "Name". after that we filled it with the User Names. You notices we used just a simple Method of "string" Data type.  Here "keyTosearch" string comes from our "Button Click" event.

protected void ButtonSearch_Click(object sender, EventArgs e)
{  
  UserDataList.DataSource = SearchedUsersDataTable(TextBoxSearch.Text);
  UserDataList.DataBind();
}

We are binding our DataList with pulled DataTable.
Now, what about if some one is trying to search some"keywords" which return no "DataRow"? Just add the following code to the "Buttonsearched_Click" Event just after the DataList Binding. Now the code will look like this;

protected void ButtonSearch_Click(object sender, EventArgs e)
{  
    UserDataList.DataSource = SearchedUsersDataTable(TextBoxSearch.Text);
    UserDataList.DataBind();
if (UserDataList.Items.Count == 0)
{
     LabelNoResult.Visible = true;
     LabelNoResult.Text = "No User Found for your search key!";
}
else
     LabelNoResult.Visible = false;
}

Hope it help.

Visitors/Readers Comments
(for questions please use The Forum)



briiiin
Hey u got cool tuts, helping me alot, Ive searched everywhere for most stuff u have here and havent found, not even in asp.net.
Thnx

28/02/2007 20:49:18 UTC

koen

nice tut,

i used it but with one addition

i placed .ToLower after all KeyToSearch and in front of all .Contains entries, this fixes case sensative troubles

 

20/03/2007 09:48:23 UTC

omer kamal

Koen!

Nice idea. We are going to apply changing the tut.

20/03/2007 10:12:28 UTC

123Stop End

Nope, not there. It is weird.

Anyway i was wondering if u could elaborate further on the question(above) about how to display/list users who are online just like displaying new users, the link u provided above searches the users...

Im not sure exaclty where to put this code below and is KeyToSearch necessary as am not searchin? Sorry for the questions am new to asp.net. But appreciate your help and time.

if(usr.UserName.Contains(KeyToSearch) && usr.Isonline){

}

thnx

21/03/2007 19:53:17 UTC

omer kamal

What you want to do? Do you want to cerate a DataTable of the Users only?

22/03/2007 07:24:36 UTC

123Stop End
Yes exactly.

22/03/2007 10:31:20 UTC

omer kamal

This Article is exectly what you want

https://dotnet-friends.com/tutorials/asp/tutinaspb26b6f22-0ff0-48cf-8f96-0fd5d41bd620.aspx

If you dont want to sort the Table then return DataTable instead of DataView.


I mean like this:

Public static DataTable GetAllUsersDataTable()
    {
        MembershipUserCollection mc = Membership.GetAllUsers();
 
        DataTable dt = new DataTable("LastUsers");
        dt.Columns.Add("UserName", typeof(string));
        dt.Columns.Add("Rdate", typeof(DateTime));
        string name = string.Empty;
        foreach (MembershipUser st in mc)
        {
            name = st.UserName;
            dt.Rows.Add(new Object[] { name, st.CreationDate });
        }
        dt.AcceptChanges();
     
        return dt;
    }

22/03/2007 16:07:30 UTC

123Stop End
Hi, thnx it does the job. Is it possible to filter so it only displays Online Users?
thnx!

Your Question have been moved to the Forum.

28/03/2007 06:26:40 UTC




Add your Comments

Name:  
Message:
Note: For faster response please use Forums >> for your questions instead of the comments area! (Admin)