Click here to Skip to main content
Click here to Skip to main content

SearchableSqlProfileProvider - The Searchable SQL Profile Provider

, 23 Feb 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

Introduction

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.

Background

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">
  <providers>
      <clear/>
      <add name="mySearchableSqlProfileProvider" 
        type="SearchableSqlProfileProvider" 
        connectionStringName="myDbConnection"/>
  </providers>
  <properties>
      <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"/>
  </properties>
</profile>

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 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[
         "PropertyName"].Provider).FindProfilesByPropertyValue(
         ProfileCommon.Properties["PropertyName"], 
         SearchableSqlProfileProvider.SearchOperator.Equal, 
         "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
}

Finally

The demo application SearchableSqlProfileProvider_Demo.zip will show you everything, download it, and run it to see the code in action.

License

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

Share

About the Author

Walid Abdulrazik
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 PinmemberMember 26309086-Mar-09 10: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 PinmemberWalid Abdulrazik10-Mar-09 3:34 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 23 Feb 2009
Article Copyright 2009 by Walid Abdulrazik
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid