Finishing the Filter – More on Scope

Whilst on the subject of finishing off the system there is another feature that isn’t yet completed.That feature is the filter.

To make this work we need to add some code to the event handler of the Apply button and make a small change to the middle layer class clsAddressCollection.

Modifying the Middle Layer

The first problem is that the filter is currently hard coded into the function for the AddressList.

To fix this we need some mechanism for the presentation layer to send data to the middle layer. The obvious solution is to add a parameter to the function.

There is a problem though. The function for AddressList is defining a property not a method and properties in C# do not accept parameters. So how do we solve this?

Creating the FilterByPostCode Method

One solution is to create a new function defining a FilterByPostCode method like so...

The next question is what code needs to go in this function?

It is very similar to the code in AddressList…

This time rather than hard coding a blank string we are using the PostCode parameter.

We now need to make sure that the AddressList function and this new function are using the same database connection.

Modify the FilterByPostCode function like so…

This means that the dBConnection isn’t being created from scratch but an existing connection is being re-set.

Also AddressList like so…

This will create a lot of red underlining as the object is not being created.

The question is “where is the existing connection being created?”

Scope

As we saw last week we need to adjust the scope (where they are declared) of certain objects.

In the function Find the object dBConnection is defined inside that function which means it is only available to code within that function.

Any code outside of that function e.g. AddressList cannot get at this object which is what is causing the problem.

The simplest solution is to decvare dBConnection at the top of the class definition...

This means that the functions FilterByPostCode and AddressList now share the same object.

Modifying the Presentation Layer

The next step is to modify the DisplayAddress function in Default.aspx.

The first step is to add a parameter to the function so that we may send it the data we want to filter on...

The next step is that we need to invoke the Find method before we try to access the AddressList data...

Run the program to see if it works.

What you should see is something like the following error...

The problem is that we have now added a parameter to the definition of DisplayAddresses which means we must add parameters to any calls to this function.

Double click on the errors to be taken to the offending code.

For the load event add a blank string as the parameter so that we show all records...

Do the same for the Display All event handler...

We may as well also add the code for the Apply event handler...

Run the program to see if it works. Try applying a filter!

What you will see is that the program still crashes with the following error...

So what is the problem?

The problem is with the code for the Count property in clsAddressBook...

We have another scope related problem. Since Count is creating its own version of the object dBConnection it is not using the object we have created at the top of the class definition...

To fix this we need to make sure that the Count function is using the same object as Find and AddressList. We need modify the count function like so...

Test the program to see if we are there yet.

You should see that there may be one last minor issue.

When the list is populated it is not clearing the previous data...

To fix this add the following line of code to the DisplayAddresses function...

You should now have a working find function for your data.