ASP.NET 2.0 provides a profile system where websites can store user specific information, but this profile system doesn't provide a means by which a website can search through these profiles to find users with specific profile criteria. Luckily enough, the ASP.NET 2.0 profile feature is built upon what's called the "Provider Model", which allows customization of the profile system's inner workings to fit custom website needs, and that's what we are going to toy with in this article.
This article is based upon Shawn Wildermuth's a.k.a "ADOGuy" code, titled "Creating custom ASP.NET 2.0 profile providers". There, he explains what the profile feature is, how to use it, what a "profile provider" is, and how it's useful to web developers. Also, in his article, he has provided an example of a custom profile provider with a great idea. All I did was just build over his work. I improved his provider, customized it more, tested it more, and fixed some bugs in it. Here is a link to his article: Creating custom ASP.NET 2.0 profile providers, by Shawn Wildermuth. I'm not going to explain all that he had to, over again. I'll just explain how to use the custom profile provider. Also, the code of the provider is heavily commented, so all of you are more than welcome to have a look, and all questions are welcomed.
Using the Code
Using the code is simple enough; you just put the file named "SearchableSqlProfileProvider.cs" in the "App_Code" folder of your project. Also, you get to modify the "web.config" file to tell the system that you are going to use a custom profile provider and also to define the profile properties, like this:
<profile enabled="true" defaultProvider="mySearchableSqlProfileProvider">
<add name="Title" type="System.Int32" defaultValue="0" />
<add name="Name" type="System.String" defaultValue="" />
<add name="Gender" type="System.Int32"
In this piece of "web.config" file, we have defined that we are going to use a profile provider named "
mySearchableSqlProfileProvider" of type "
SearchableSqlProfileProvider", with the database defined with the entry "
myDbConnection" in the "
<connectionStrings>" section of the "web.config" file. We also added three profile properties with the names
Gender; of course, you can change them to suit your needs.
The one thing missing here is the database against which the profile provider will work! You will find a script in the SearchableSqlProfileProvider.zip file, which you can use to build a database called "users", and work against it by providing a valid connection to it in the entry "
myDbConnection" in the "
<connectionStrings>" section of the "web.config" file. The script has been tested with Microsoft SQL Server 2005, and it works nicely. By the way, the demo application has a database file generated by the same script.
After doing these few steps, you can now use the profile provider as you use the default one, just that simple!
The Demo Application
To use the search method of the profile provider, you just do as follows:
ProfileInfoCollection pic = new ProfileInfoCollection();
pic = ((SearchableSqlProfileProvider)ProfileCommon.Properties[
What we do here is instantiate a
ProfileInfoCollection to receive the return value of the search method "
FindProfilesByPropertyValue", which takes three parameters: the first one is the profile property to search against, the second one is the search operator that will be discussed later, and the last parameter is the search criteria.
The Search Operator
The search operator is defined as follows:
public enum SearchOperator
Equal = 0,
NotEqual = 1,
Like = 2,
NotLike = 3,
LessThan = 4,
LessThanOEqual = 5,
GreaterThan = 6,
GreaterThanOrEqual = 7,
Contains = 8,
FreeText = 9
The demo application SearchableSqlProfileProvider_Demo.zip will show you everything, download it, and run it to see the code in action.