Click here to Skip to main content
15,663,239 members
Articles / Web Development / ASP.NET
Posted 23 Feb 2009


26 bookmarked

SearchableSqlProfileProvider - The Searchable SQL Profile Provider

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
23 Feb 2009CPOL3 min read
An extension to the Microsoft SqlProfileProvider, to provide a search interface. This article is based on Shawn Wildermuth's - "a.k.a. ADO Guy" - work.


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="mySearchableSqlProfileProvider" 
      <add name="Title" type="System.Int32" defaultValue="0" />
      <add name="Name" type="System.String" defaultValue="" />
      <add name="Gender" type="System.Int32" 
        defaultValue="0" allowAnonymous="true"/>

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 Title, Name, 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 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[
         "Search Criteria");

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 will show you everything, download it, and run it to see the code in action.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer Viserware Technologies
Egypt Egypt
A physician, specialized in plastic surgery, with passion for computer programming, mostly database, windows and web applications.

Comments and Discussions

GeneralException has been thrown by the target of an invocation Pin
Member 26309086-Mar-09 9:08
Member 26309086-Mar-09 9:08 
I am trying to get this to work and I am getting the following Error (Any IDeas?):


Configuration Error
Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.

Parser Error Message: Exception has been thrown by the target of an invocation.

Source Error:

Line 40: <providers>
Line 41: <clear/>
Line 42: <add name="mySearchableSqlProfileProvider" type="SearchableSqlProfileProvider" connectionStringName="DB"/>
Line 43: </providers>
Line 44: <properties>
GeneralRe: Exception has been thrown by the target of an invocation Pin
Walid Abdulrazik10-Mar-09 2:34
Walid Abdulrazik10-Mar-09 2:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.