CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Jeff Lynch [MVP]

Everything E-Commerce!

Commerce Server 2007: Development Tip #5 - Using the RegionCodeDataSet

I'm going to repost this series of "development tips" related to Commerce Server 2007 to help other folks get started with their development work. All of these "tips" come directly from what I've learned (the hard way) over the past eight months and are intended to save you time and effort. All code samples are now based upon the RTM version and have been tested in our production environment.

The Commerce Server Team has spent a lot of time and effort making the site developer's life a whole lot easier. Sometimes it's through really big changes like the new Inventory subsystem and sometimes it's through the "little things" like the RegionCodeDataSet.

Tip #5 - Using the RegionCodeDataSet

One of the really interesting little things I stumbled upon during my CS2007 explorations was the RegionCodeDataSet which contains two DataTables, one for Country Codes and another for State or Province Codes.

On my site I've setup a simple form for a user to add a new Address profile as shown below. Since our site is used globally, I need to make sure the Address profile is filled out as completely and correctly as possible. As you can see, I begin with a DropDownList of countries which is bound to the RegionCodeDataSet.CountryCodes DataTable.

I've also bound another DropDownList control to the RegionCodeDataSet.StateCodes DataTable using the Page_Load code shown below.

protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!Page.IsPostBack)
            {
                // Get the RegionCodeDataSet
                dsRegions = addressHelper.GetRegionCodes();
 
                // Get the CountryCodesDataTable
                dtCountryCodes = dsRegions.CountryCodes;
                CountryCodeDropDownList.DataSource = dtCountryCodes;
                CountryCodeDropDownList.DataBind();
 
                // Allow AutoPostBacks
                CountryCodeDropDownList.AutoPostBack = true;
 
                // Set the DropDownList's initial value
                CountryCodeDropDownList.SelectedValue = "US";
 
                // Get the DropDownList's SelectedIndex
                int iRow = CountryCodeDropDownList.SelectedIndex;
 
                // Get the DataRow array
                DataRow[] drStates = dtCountryCodes.Rows[iRow].GetChildRows("States");
 
                // If the "Country" has "States"
                if (drStates.Length > 0)
                {
                    RegionCodeDropDownList.DataSource = drStates;
                    RegionCodeDropDownList.DataBind();
                    RegionCodeDropDownList.Enabled = true;
                    RegionCodeLabel.Enabled = true;
 
                CountryCodeDropDownList.Focus();
            }
        }
        catch (Exception ex)
        {
            // Show the Error Label and set it's text
            ErrorLabel.Visible = true;
            ErrorLabel.Text = ex.Message;
 
            // Hide the Panel control
            AddressPanel.Visible = false;
        }
    }
 
 

The Page_Load method calls a simple AddressHelper.GetRegionCodes() method shown here.

public RegionCodeDataSet GetRegionCodes()
    {
        // New RegionCodeDataSet
        RegionCodeDataSet dsRegions = new RegionCodeDataSet();
 
        // Get the TypedDataSet from the OrderContext
        dsRegions = CommerceContext.Current.OrderSystem.GetRegionCodes();
 
        // Check to see if relation exists
        if (dsRegions.Relations.Count < 1)
        {
            // Add a relation to each table in the Typed DataSet
            dsRegions.Relations.Add("States", dsRegions.CountryCodes.Columns["Code"], dsRegions.StateCodes.Columns["CountryCode"]);
        }
 
        return dsRegions;
    }

When the user selects another country such as the "United Kingdom" which doesn't have any States or Provinces, the DropDownList isn't data bound and is disabled as shown below.

 

I then use the DropDownList control's OnSelectedIndexChanged event to call the method shown below which returns the "States" or "Provinces" depending upon the CountryCode selected by the user.

protected void GetStateCodes(object sender, EventArgs e)
    {
        try
        {
            dsRegions = addressHelper.GetRegionCodes();
            dtCountryCodes = dsRegions.CountryCodes;
 
            // Get the DropDownList's SelectedIndex
            int iRow = CountryCodeDropDownList.SelectedIndex;
 
            // Get the DataRow array
            DataRow[] drStates = dtCountryCodes.Rows[iRow].GetChildRows("States");
 
            // If the "Country" has "States"
            if (drStates.Length > 0)
            {
                RegionCodeDropDownList.DataSource = drStates;
                RegionCodeDropDownList.DataBind();
                RegionCodeDropDownList.Enabled = true;
                RegionCodeLabel.Enabled = true;
            }
            else
            {
                // Clear the DropDownList's Items
                RegionCodeDropDownList.Items.Clear();
                RegionCodeDropDownList.Enabled = false
                RegionCodeLabel.Enabled = false;
            }
        }
        catch (Exception ex)
        {
            // Show the Error Label and set it's text
            ErrorLabel.Visible = true;
            ErrorLabel.Text = ex.Message;
        }
    }

The final step to wrap this code with a new Atlas UpdatePanel control so only the TextBox and DropDownList controls are re-rendered during the post-back.

When this is put all together, it creates a really nice experience for the user and ensures that the Address profile created will be complete and correct (at least as far as the country and states/provinces are concerned).

Special thanks to Joe Wasson [MSFT] for his help and guidance in pointing me in the right direction!

Technorati Tags:


Published Jul 12 2006, 09:42 PM by jlynch
Filed under:

Comments

David Hayden said:

I don't do anything with Commerce Server, but the fact that you are writing code and writing it in c# gets me all tingly inside. Keep up the good posts.

I do hope you don't comment every line of code in your applications, however, unless you get paid per line that is :)
# May 3, 2006 12:25 AM

jlynch said:

Dave,

When you reach middle age, have four daughters and can't remember your wife's birthday you'll need to comment your code like I do (LOL).

Actually, I like to do this for demo code to make it easier to follow.
# May 3, 2006 11:54 AM

Jeff Lynch [MVP] said:

What do I do if the StarterSite doesn't fit my scenario?

# January 4, 2007 8:28 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!

This Blog

Syndication

News