Friday, 30 October 2009

Here's a quick way to add English counties to your next sign-up form

In the never-ending quest to speed up development and reduce the chance of user error I have packaged up a data source which can be used to populate your controls with a complete list of English counties.

Why would you use this?

If you are creating an English focused website and you need to collect personal information such as names and addresses then you can not only save time by data binding from the list, you can also improve the quality of your data because you eliminate the chance of misspellings or incorrect data.

EnglandCountyDataSource-DropDownList

The code included in this article contains a DataObject class which is a bindable object that populates controls with a list of all the counties in England. The class can be used codelessly with a DropDownList and an ObjectDataSource. You can also use your normal development techniques such as required selection validators and the AppendDataBoundItems attribute.

The DataObject code - EnglandCountyDataSource.cs

This file is a straightforward class which is decorated with the DataObject attribute. It simply provides a bindable list of counties.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;

namespace rtp.DataSource
{
    /// <summary>
    /// A data object containing a list of Englands postal counties as defined at
    /// http://en.wikipedia.org/wiki/Postal_counties_of_the_United_Kingdom
    /// </summary>
    [DataObject(true)]
    public class EnglandCountyDataSource
    {
        private List<string> CountyList;

        public EnglandCountyDataSource()
        {
            CountyList = new List<string>();

            PopulateCountyList();
        }

        [DataObjectMethod(DataObjectMethodType.Select, true)]
        public List<string> SelectCountyList()
        {
            return CountyList;
        }

        private void PopulateCountyList()
        {
            CountyList.Add("Avon");
            CountyList.Add("Bedfordshire");
            CountyList.Add("Berkshire");
            CountyList.Add("Buckinghamshire");
            CountyList.Add("Cambridgeshire");
            CountyList.Add("Cheshire");
            CountyList.Add("Cleveland");
            CountyList.Add("Cornwall");
            CountyList.Add("County Durham");
            CountyList.Add("Cumbria");
            CountyList.Add("Derbyshire");
            CountyList.Add("Devon");
            CountyList.Add("Dorset");
            CountyList.Add("East Sussex");
            CountyList.Add("Essex");
            CountyList.Add("Gloucestershire");
            CountyList.Add("Hampshire");
            CountyList.Add("Herefordshire");
            CountyList.Add("Hertfordshire");
            CountyList.Add("Isle of Wight");
            CountyList.Add("Kent");
            CountyList.Add("Lancashire");
            CountyList.Add("Leicestershire");
            CountyList.Add("Lincolnshire");
            CountyList.Add("London");
            CountyList.Add("Merseyside");
            CountyList.Add("Middlesex");
            CountyList.Add("Norfolk");
            CountyList.Add("North Humberside");
            CountyList.Add("North Yorkshire");
            CountyList.Add("Northamptonshire");
            CountyList.Add("Northumberland");
            CountyList.Add("Nottinghamshire");
            CountyList.Add("Oxfordshire");
            CountyList.Add("Shropshire");
            CountyList.Add("Somerset");
            CountyList.Add("South Humberside");
            CountyList.Add("South Yorkshire");
            CountyList.Add("Staffordshire");
            CountyList.Add("Suffolk");
            CountyList.Add("Surrey");
            CountyList.Add("Tyne and Wear");
            CountyList.Add("Warwickshire");
            CountyList.Add("West Midlands");
            CountyList.Add("West Sussex");
            CountyList.Add("West Yorkshire");
            CountyList.Add("Wiltshire");
            CountyList.Add("Worcestershire");
        }
    }
}

Usage Example 1 - Simple data binding

This is a simple example to show how a DropDownList is bound to a DataObject called EnglandCountyDataSource providing a list of all the counties in England.

<p>
    <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="EnglandCountyDataSource">
    </asp:DropDownList>
</p>
<asp:ObjectDataSource ID="EnglandCountyDataSource" runat="server" SelectMethod="SelectCountyList"
    TypeName="rtp.DataSource.EnglandCountyDataSource"></asp:ObjectDataSource>

Usage Example 2 - Providing an initial value

This example shows how to provide a default selection text to the data source

<asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="EnglandCountyDataSource"
    AppendDataBoundItems="True">
    <asp:ListItem>Please select a county</asp:ListItem>
</asp:DropDownList>
<asp:ObjectDataSource ID="EnglandCountyDataSource" runat="server" SelectMethod="SelectCountyList"
    TypeName="rtp.DataSource.EnglandCountyDataSource"></asp:ObjectDataSource>

Using it in your own project

To use this data object in your own project simply copy the c# code which is in the following location of the attached code sample:

~/App_Code/EnglandCountyDataSource.cs

Then bind it to your object using the methods demonstrated in usage examples 1 and 2.

Databinding - Step By Step

When you come to databinding its really straightforward - the only caveat is that by default the data source configuration wizard will want to bind the data field and data value to "Length". To bind to the actual county name just wipe both textboxes and leave them blank.

1 - First drag a DropDownList onto your page. Click the smart tag and click "Choose Data Source".

EnglandCountyDataSource-DataBinding1

2 - In the Select a data source pick <New data source…>

EnglandCountyDataSource-DataBinding2 

3 - Choose object to create a new ObjectDataSource. Click OK.

EnglandCountyDataSource-DataBinding3

4 - Choose the EnglandCountyDataSource from the business object drop down

EnglandCountyDataSource-DataBinding4

5 - The methods will be pre-populated for you. Just click Finish.

 EnglandCountyDataSource-DataBinding5

6 - The default data display field and data value fields will be set to Length, simply wipe both fields clean and press OK.

EnglandCountyDataSource-DataBinding6 EnglandCountyDataSource-DataBinding7

Other formats

I have included the data in three other formats so that you can integrate the information into your workflow with the minimum of hassle. The formats are xml, <asp:ListItem> and a sql insert file.

The code for the generator tool that I used to generate these formats is included. It shows how you can programmatically operate on the data object.

The code can be found in the GenerateFormattedCountyList.aspx file in the download.

Possible expansion

This data object is focused on what providing exactly what it set out to do. You may want to add in an "Other" or "Outside England" option to the end of the data objects internal list.

The data object uses a List<string> structure so you can just duplicate the last Add() line to insert your own.

If you want to do it at the top of your list then you can use the same technique as in usage example 2 to append data bound items.

Download

    This download includes all of the files discussed in this article including an xml file, sql file and txt ListItem file, the formatted list generator, the data object itself and the example page (default.aspx).

    Further Reading

    kick it Shout it vote it on WebDevVote.com

    2 comments:

    Pzycoman said...

    Your list isnt correct - there is no postal area of "Greater London" - its simply "London" :)

    rtpHarry said...

    Thanks Pzycoman I have updated the list to reflect the true postal counties of England.

    It didn't occur to me that they would be different than the normal counties but Wikipedia provided a list which is hopefully accurate!