Oct 5 2006

Filter Collection of Objects

Category: ASP.NET 1.x | ASP.NET 2.0 - GeneralBil@l @ 06:04

I have been developing an API for an application of mine at work. The API returns a set of objects in the form of Collections. I needed a way to filter the collection based on some values I provide.

I have passed by this blog post:

http://codebetter.com/blogs/brendan.tompkins/archive/2004/09/02/24116.aspx

Brendan creates something known as CollectionView, that supports both Windows and Web collections.

I took that code, and did some modifications on it:

  1. I removed the IBindingList interface
  2. I removed all methods from the IList and ICollection implementation that might allow change of data in teh input Collection.
  3. I removed the sorting feature

I made the collection a normal collection that takes as input a collection, and then allows the developer to filter data inside.

In addition, I made the CollectionView capable of filtering not only on the direct properties of the classes inside the IList, but also multilevel properties as follows:

Currently, you can filter on simple data-types properties:

public string/int/double (etc ...) PropertyName
{
   get {}
   set {}
}

My CollectionView allows you to do filtering even on:

public Person MyPerson
{
    get {}
    set {}
}

Where Person is a class, so you can filter a collection of objects, where each object might have a property of type another object, i.e. you can filter a collection based on a property in the multilevel properties of the objects in the Collection.

An example illustrates how to use the new ApplyFilter method:

Suppose, a class Person has a property of type Address, where Address is another class that has a property called City as follows:

 

 public class Person
 {
  //members
  private string firstName=null;
  private string lastName=null;
  private Address address = null;
  
  //Public constructor
  public Person(string FirstName,string LastName, Address address)
  {
   this.firstName =FirstName;
   this.lastName=LastName;
   this.address = address;
  }

  #region Properties for member access
  public string FirstName
  {
   get
   {
    return firstName;
   }
   set
   {
    firstName=value;
   }
  }

  public string LastName
  {
   get
   {
    return lastName;
   }
   set
   {
    lastName=value;
   }
  }

  public Address Address
  {
   get
   {
    return this.address;
   }
   set
   {
    this.address=value;
   }
  }

  #endregion
 }
}


 public class Address
 {
  private string city;

  public string City
  {
   get
   {
    return this.city;
   }
   set
   {
    this.city = value;
   }
  }

  public Address(string City)
  {
   this.city = City;
  }
 }

Now you create a collection of Person class as follows:

PersonCollection pCol = new PersonCollection();
Person p1 = new Person("Bilal", "Haidar", new Address("Beirut"));
Person p2 = new Person("Lara", "Keyrouz", new Address("Beirut"));
Person p3 = new Person("Wessam", "Zeidan", new Address("Zahle"));
Person p4 = new Person("Ziad", "Hanna", new Address("Beirut"));
Person p5 = new Person("Alaa", "Said", new Address("Saida"));

Now, you want to get all persons in Beirut, which is a property called City inside object Address, where Address is a property inside Person class, so you can see the hierarchy:

Person
   --------> Address
                       --------> City

What you do is use the new ApplyFilter method as follows:

CollectionView cv = new CollectionView(pCol);
cv.ApplyFilter("Address","City", new Object[] {"Beirut"},FilterOperand.Equals);

The first parameter in the ApplyFilter is the Property, the second is the SubProperty, then an array of objects to provide the data in where to search, then an operand of equals.

You can even search for direct and simple properties as follows:

CollectionView cv = new CollectionView(pCol);
cv.ApplyFilter("FirstName",null, new Object[] {"Bilal"},FilterOperand.Equals);

I have also added a new Filter Option called "InRange", this way, you can search for a set of values as follows:

CollectionView cv = new CollectionView(pCol);
cv.ApplyFilter("FirstName",null, new Object[] {"Bilal", "Wessam"},FilterOperand.InRange);

This way, it will bring you person records whose FirstName are in a set of values (Bilal and Wessam).

The additions are:

  1. Support for multilevel property filter
  2. A new Filter Operan called InRange has been added
  3. Data to be filtered according to has been changed to an array of Objects

You can download the CollectionView.cs file and use it in your applications.

I will be enhancing it with sort option, maybe more Filter Operands too!!

Hope you liked this post!!

Regards

 

Tags: ,

Aug 26 2006

xGrid on CodePlex

Category: ASP.NET 2.0 - GeneralBil@l @ 06:23

Hello,

I have decided to place the xGrid on CodePlex because of the huge number of comments and feedback I am receiving every day on the xGrid.

The xGrid needs some enhancements and improvements by adding additional features.

Hope you all will be able to help me out in moving this xGrid to a new stage, where all developers can use it for free with the maximum features available!

Regards

Tags:

Aug 21 2006

Page Properties and SqlDataSource-ObjectDataSource-AccessDataSource

Category: ASP.NET 2.0 - GeneralBil@l @ 13:57

A nice tip given by Stephan Walther in his latest book, ASP.NET 2.0 Unleashed is how to use a page property as a parameter to either Insert, Delete, Update, or Select parameters.

As you know, we have a set of new parameter objects that can be used to supply data to Sql, Object, and Access DataSources. Among which is the ControlParameter.

ControlParameter usually uses a control on the page. Since a Page is also a control, then we can use the Page control as follows

<asp:ControlParameter Name="IPAddress" ControlID="__page"
                PropertyName="IPAddress" />

In this example, you can see that the ControlParamter uses the ControlID as "__page", where __page is the name of the Page control at run time. The propertyName here represents a property that you have created on the page code-behind as:

    Public ReadOnly Property IPAddress() As String
        Get
            Return Request.UserHostAddress
        End Get
    End Property

So, you are able to use the Page property simply as input for the ControlParameter.

Hope this helps,

Regards

Tags:

Aug 5 2006

Databinding Expressions in ASP.NET 1.1/2.0

Category: ASP.NET 2.0 - GeneralBil@l @ 07:47

A very common question on the ASP.NET forums is how to do expression databinding on the ASPX page.

The ASP.NET allows you to use 3 different binding expressions:

1- <% %>
You can use the above expression to execute some C#/VB.NET code as follows:

<% Response.Write("Hello World"); %>

Which will print out "Hello World" on the page.

2- <%# %>
This expression can be used to execute any property, collection, expression, or method result on the ASPX page.

  1. <%# CustomerID %> will print out the customer ID provided CustomerID is a public property/field.
  2. <asp:ListBox ... datasource='<%# studentArray %>' ... /> this will bind the ListBox to an array of students.
  3. Full Name: <%# (Student.FirstName + " " + Student.La   stName) %>
  4. Number of users online: <%# GetUserCount() %>

The main idea here is that, the expression within the <%# %> is only executed when Page.DataBind() is called.

3- <%= %>
This works the same as the above expression syntax with the difference that this syntax is executed on page load without the need to call Page.DataBind(). Similar to Response.Write().

hth,

Regards


Candidates for test 70-620 should have minimum one year of experience in the IT industry. They characteristically make available phone support at the tier-1 or tier-2 level in a broad range of environments, incorporating retail stores and the small organizations or enterprise environment. 1Y0-259 is a Citrix Presentation Server 4.5 administration test. SY0-101 is a CompTIA Security+ exam. Designing unified Cisco multilayer switched networks is the task which requires you to pass 642-812.

Tags:

Aug 1 2006

HttpResponse.ApplyAppPathModifier Method

Category: ASP.NET 2.0 - GeneralBil@l @ 08:50

A very interesting method I came over today:

HttpResponse.ApplyAppPathModifier

According to the MSDN documentation:

Adds a session ID to the virtual path if the session is using Cookieless session state and returns the combined path. If Cookieless session state is not used, ApplyAppPathModifier returns the original virtual path.

This is very cute!! If you are using cookieless session state, you can easily you this method, give it a virtual path, it will automatically append the SessionId to the URL without any need to do any manual effort!!!

It is also helpful, when you want to redirect from Http to Https or vicaversa, it helps you maintain the same session Id when moving!

This is a small example:

Response.Write(Response.ApplyAppPathModifier("demo/default.aspx"));

will generate something as:

/App/(S(avsbnbml2n1n5mi5rmfqnu65))/demo/default.aspx

Hope this helps!

Regards

Tags:

Jul 9 2006

SQL Server Provider Statistics

Category: ASP.NET 2.0 - GeneralBil@l @ 12:14

SQL Server Provider in .NET Framework 2.0 provides a way to generate per-connection reports for measuring performance through a property in the SqlConnection object called StatisticsEnabled.

This is a sample code of how to do so:

SqlConnection conn = new SqlConnection(connectionString);
conn.Open();
conn.StatisticsEnabled = true;

// Perform data access operations

IDictionary stats = conn.RetrieveStatistics();

foreach (DictionaryEntry e in stats)
{
        Response.Write("Key : " + e.Key + " Value : " + e.Value);       
}

conn.ResetStatistics();



Hope this helps!

Regards

Tags:

Jul 7 2006

Provider ToolKit Configuration Utility - Part 2

Category: ASP.NET 2.0 - GeneralBil@l @ 09:47

In a previous post, Provider ToolKit Configuration Utility, I published a small utility that helps you create all the needed files when you are building a new Service Provider Model.

In this post, I have upgraded that utility to allow you to create Providers not only related to Data Sources but rather to other APIs. Sometimes, we might need to create a provider that talks to a third-party API, which is in this case neither an Oracle, SQL Server, Microsoft Access, etc ...

In addition, the Namespace TextBox has been increaded in width to allow you to use long namespaces and at the same time be able to see it.

You can download the new version from the Files section above.

Hope this helps,

Regards

Tags:

Jun 27 2006

Store View State in a Persistent Medium, the Proper Way

Category: ASP.NET 2.0 - GeneralBil@l @ 01:13

I kindly invite you to check out my latest article on the ASPAlliance.com with the title:

Store View State in a Persistent Medium, the Proper Way

Hope you will enjoy it!

Regards

Tags:

Jun 5 2006

Membership Manager Control in ASP.NET 2.0

Category: ASP.NET 2.0 - GeneralBil@l @ 21:25

This is the tool we have long waited from the ASP.NET Team. However, this time it came from Quality Data Corporation, it is the Membership Manager Control.

As you know the ASP.NET Configuration tool that ships with VS 2005 can be used locally to manage the membership of a website! Once the website is hosted remotely there is no way to manage your users/roles/etc…

What the MMC does is that, it mimics the local ASP.NET Configuration Tool, it allows you to:

  • Add new users
  • Delete users
  • Update user records
  • Manage Roles
  • Search for your users by:
    • Email
    • UserName

    Above all it can be skinned so you can apply a skin to it and have a lovely interface! Every single piece of text that appears on the control can be changed and adapted to your preferences.

    I believe the control is still at its beginning. It still has lots of stuff to be added to become richer and richer, and therefore we can promise ourselves with such a great control to use in order to make managing our websites an easy thing to do.

    I advise you to check this control, download it for a trail version, use it, and then you know what you shall do ;)

    Special thanks to Brian Mishler for this great control!

    Membership Manager Control

     

    Regards

    Tags:

    Jun 4 2006

    Adding Templates within UserControl

    Category: ASP.NET 2.0 - GeneralBil@l @ 20:01

    http://90statehouse.com/forums/blogs/robertseder/archive/2006/06/04/201.aspxCheck out this important blog post that shows you how to add Template sections into your usercontrols.

    We have always believed that only properties are supported by UserControls, but now this has changed!

    Robert Seder discusses this idea in his blog post here.

    Tags: