Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Consuming the Yahoo! Local Search Service in a C# WinForms Application

1.00/5 (1 vote)
18 Jan 2008CPOL4 min read 1   479  
An article on consuming the Yahoo! Local Search Service in a C# WinForms Application
Image 1

Introduction

Yahoo Web Services are made available to developers through Yahoo’s Developer Network; a variety of services are available, but this article is specifically interested in using Yahoo’s Local Search Service. This service will return a list of businesses based upon a zip code based search for a business name or product. The service returns the following information:

  • Title (the name of the business)
  • Street Address
  • City
  • State
  • Phone Number
  • Latitude and Longitude
  • The distance from the center of the zip code supplied by the user
  • URLs for maps and details about the business
  • URL of the business web site (if available)

This article will demonstrate the basics of submitting Local Search Service request, displaying the results, and accessing the available links returned in the search results. Naturally there are a number of ways in which we could present the search results and this demonstration application only illustrates one such possibility. For more information regarding the program, refer directly to the Yahoo Developer Network website located here.

The service may be accessed up to 50,000 times a day; participation in the program is free and is contingent upon signing up for the Yahoo! Developers Network and obtaining an application ID from Yahoo!

When visiting the Yahoo! site, take a look at the different programs available and take a look at some of the many services made available through Yahoo.

Image 2
Figure 1: The demonstration application running.

Image 3
Figure 2: Providing Context Menu Access to the Map, Business, and Business Details URLs.

Image 4
Figure 3: Displaying the Map to the Business.

Image 5
Figure 4: Displaying the Business Website.

Image 6
Figure 5: Displaying the Yahoo! Business Details.

Getting Started

In order to get started, unzip the included project and open the solution in the Visual Studio 2005 environment. In the solution explorer, you should note these files:

Image 7
Figure 6: Solution Explorer.

The Main Form (frmSearch.cs)

The main form (frmSearch.cs) is the only form contained in the application; all of the application specific code required to access the service, return the results of a local search, and to display those results in a datagridview control or as a Web page are included in this class.

The code is pretty simple, if you'd care to open the code view up in the IDE, you will see that the code file begins as follows:

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;

The imports are primarily per the default configuration for a Windows application; the System.Net library import is the only departure from the default.

Following the imports, the namespace and class are defined and a default constructor added. Note that in the constructor; the zip code text box is populated using a stored user setting. This code and all remaining code is annotated to describe the purpose of each section of code.

C#
namespace YahooLocalSearch
{
    /// <summary>
    /// Consume the Yahoo! Local Search Service
    /// in a C# WinForms application
    /// </summary>
    public partial class frmSearch : Form
    {

        /// <summary>
        /// default constructor
        /// </summary>
        public frmSearch()
        {
            InitializeComponent();

            // the user's zip code likely won't change so
            // set the zip code to user last entered zip code
            txtZipCode.Text = Properties.Settings.Default.MyZipCode;
        }

The next section of code is the button click event handler for the search button. When this button is clicked, the method will verify that the user has entered a search term and zip code. It will store the current zip code into the user setting (in case the user changed it) and then it will call a separate method called LocalSearch which accepts two arguments: the title of the business or product name, and the zip code.

C#
/// <summary>
/// Search for matches based upon user supplied
/// business/product name and zip code
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSearch_Click(object sender, EventArgs e)
{
    // validate that search terms were entered
    // by making sure that the business/product
    // and the zip code textboxes contain something
    if (txtBusinessOrProduct.Text != string.Empty ||
        txtZipCode.Text != string.Empty)
    {
        // save last entered zip code in user setting
        // so that the next time the application is used
        // we can retrieve this zip code and reuse it
        Properties.Settings.Default.MyZipCode = txtZipCode.Text;
        Properties.Settings.Default.Save();

        // call the search with the product and zip code arguments
        // this function will populate the datagrid with the
        // values returned from the product search
        LocalSearch(txtBusinessOrProduct.Text, txtZipCode.Text);
    }
    else
    {
        // tell the user that the search requested is invalid
        // if they failed to fill in both the product/business and
        // zip code textboxes
        MessageBox.Show("You must supply both a zip code and product
        or business name.", "Search Invalid");
    }
}

The next section of the code is used to execute the local search. The first part of the code stores the business or product name into a string variable and then trims the zip code string to prevent a zip+4 entry; the textbox that accepts the zip code is limited to only five characters in length so this is not really necessary. An HTTP Web request is then formatted to execute the search against the Yahoo! Local Search service; this request uses the supplied business or product name along with the supplied zip code to limit the number of results returned. The service limits the results to a maximum of 20 entries.

The derived response stream is used to populate a dataset; the dataset’s second table is then bound to the datagridview. The second table contains the results of the search; a total of three tables are returned by the service and contain information regarding the business rating and the total number of hits returned by the search.

C#
/// <summary>
/// Conduct a search for products or business names
/// within a specified zip code using the Yahoo!
/// Local Search Service
/// </summary>
/// <param name="business"></param>
/// <param name="zipCode"></param>
private void LocalSearch(string business, string zipCode)
{
    try
    {
        // business or product name
        string biz = business;

        // take only the zip code (not zip + 4)
        string zip = zipCode.Substring(0, 5);

        // format the web request using
        // the zip code and business/product name
        // passed in as arguments
        // NOTE:  The appid should be replaced with the
        // appid supplied by Yahoo! when the developer
        // signs up with the free developer network
        System.Net.HttpWebRequest request
            = System.Net.WebRequest.Create
            ("http://local.yahooapis.com/LocalSearchService/V2/localSearch?
            appid=YahooDemo&query=" + biz + "&zip=" + zip + "&results=20") as
            System.Net.HttpWebRequest;

        // get http web response for the web request
        using (System.Net.HttpWebResponse response =
        request.GetResponse() as System.Net.HttpWebResponse)
        {
            // populate the dataset using the
            // response stream (in XML)
            DataSet dsSearch = new DataSet();
            dsSearch.ReadXml(response.GetResponseStream());

            // populate the datagridview control using the
            // second table (which contains the actual search
            // results - in all three tables are returned
            dataGridView1.DataSource = dsSearch.Tables[1];
            dataGridView1.Refresh();
        }
    }
    catch (Exception ex)
    {
        // describe any encountered error if the
        // local search should fail for any
        // reason
        MessageBox.Show(ex.Message, "Error");
    }
}

The final three methods contained in the class are used to open up the business, map, and details links provided in the results obtained for any specific business.

C#
/// <summary>
/// Open up a browser window navigating to the
/// Map URL supplied with the search results
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void mapURLToolStripMenuItem_Click(object sender,
    EventArgs e)
{
    try
    {
        // get the cell value for the map URL
        string url =
            dataGridView1.SelectedRows[0].Cells[10].Value.ToString();

        // open the URL into a browser window
        System.Diagnostics.Process.Start(url);
    }
    catch
    {
        // display an error message to the user
        // if the URL does not exist or was invalid
        MessageBox.Show("Invalid URL", "URL Error");
    }
}

/// <summary>
/// Open up a browser window navigating to the business
/// details URL returned with the search results
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void detailsToolStripMenuItem_Click(object sender,
    EventArgs e)
{
    try
    {
        // get the cell value for the details URL
        string url =
            dataGridView1.SelectedRows[0].Cells[8].Value.ToString();

        // open the URL into a browser window
        System.Diagnostics.Process.Start(url);
    }
    catch
    {
        // display an error message to the user
        // if the URL does not exist or was invalid
        MessageBox.Show("Invalid URL", "URL Error");
    }
}

/// <summary>
/// Open up a browser window navigating to the business
/// owned website (if available).
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void businessURLToolStripMenuItem_Click(object sender,
    EventArgs e)
{
    try
    {
        // get the cell value for the business URL
        string url =
            dataGridView1.SelectedRows[0].Cells[11].Value.ToString();

        // open the URL into a browser window
        System.Diagnostics.Process.Start(url);
    }
    catch
    {
        // display an error message to the user
        // if the URL does not exist or was invalid
        MessageBox.Show("Invalid URL", "URL Error");
    }
}

Summary

This application was provided as an example of how one might take advantage of the Yahoo! Local Search service in a C# WinForms application. The service returns information about the business. It also returns a collection of links that may be used to display a map of the business, the business web site, and Yahoo! collected information pertaining to the business.

License

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