Click here to Skip to main content
15,886,362 members
Articles / Web Development / ASP.NET

Adding 'Google Suggest' functionality to an ASP.NET application

Rate me:
Please Sign up or sign in to vote.
4.76/5 (39 votes)
1 Dec 20055 min read 199K   4.4K   121  
Shows how to add 'Google Suggest' functionality to an ASP.NET application.
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using System.Configuration;
using System.Data.OleDb;


namespace ASB
{
	public class GetAutoSuggestData : System.Web.UI.Page
	{
		//Generate menu and return it
		private void Page_Load(object sender, System.EventArgs e)
		{
			string sKeyword		=Request.QueryString["Keyword"];
			string sTextBoxID	=Request.QueryString["TextBoxID"];
			string sDivID		=Request.QueryString["DivID"];
			string sDataType	=Request.QueryString["DataType"];

			//Get menu item labels and values
			ArrayList aMenuItems=LoadMenuItems(sDataType, sKeyword);
						
			ASBMenuItem oMenuItem;
			string sHtml;

			//Output menu items to the web page
			for (int nCount=0; nCount < aMenuItems.Count; nCount++)
			{
				oMenuItem=(ASBMenuItem)aMenuItems[nCount];
				sHtml=oMenuItem.GenHtml(nCount+1, sTextBoxID, sDivID);

				Response.Write(sHtml + "\n\r");
			}
		}


		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new System.EventHandler(this.Page_Load);
		}
		#endregion



		/// <summary>Get all cities that contain specified keyword</summary>
		/// <param name="sKeyword">Keyword to use in a query</param>
		/// <returns></returns>
		private ArrayList LoadCities(string sKeyword)
		{
			ArrayList aOut=new ArrayList();

			string sConnString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\databases\\AutoSuggestBox_Demo.mdb;User Id=admin;Password=;";
			OleDbConnection oCn=new OleDbConnection(sConnString);

			string sSql="SELECT TOP 10 tblCity.Name as CityName, " +
				"tblCity.Code as CityCode, " +
				"tblCountry.Name as CountryName, " +
				"tblState.Name as StateName " + 
				" FROM (tblCity INNER JOIN tblCountry ON tblCity.CountryID=tblCountry.ID) " +
				"		LEFT OUTER JOIN tblState ON tblCity.StateID=tblState.ID " + 
				" WHERE (tblCity.Name LIKE '" + sKeyword.Replace("'", "''") + "%') " +
				" ORDER BY tblCity.Name";
		
			OleDbCommand oCmd = new OleDbCommand(sSql, oCn);
			oCn.Open();

			OleDbDataReader oReader = oCmd.ExecuteReader();

			string sCity;
			string sCityCode;
			string sState;
			string sCountry;
			
			string sLabel;

			ASBMenuItem oMenuItem;

			while (oReader.Read())
			{
				//Build label using City, Country & State
				sCity		=oReader.GetString(0);
				sCityCode	=oReader.GetString(1);
				sCountry	=oReader.GetString(2);

				if (oReader.GetValue(3)==System.DBNull.Value)
					sState="";
				else
					sState=oReader.GetString(3);
				

				sLabel=sCity;
                
				//Append either city or country
				if (sState != "")
					sLabel+=", " + sState;
				else
					sLabel+=", " + sCountry;
				
				oMenuItem=new ASBMenuItem();
				oMenuItem.Label=sLabel;
				oMenuItem.Value=sCityCode;
				
				aOut.Add(oMenuItem);
			} 

			oReader.Close();
			oCn.Close();
		
			return aOut;
		}



		/// <summary>Load array of ASBMenuItems for a specific data type</summary>
		/// <param name="sDataType"></param>
		/// <param name="sKeyword"></param>
		/// <returns></returns>
		private ArrayList LoadMenuItems(string sDataType, string sKeyword) 
		{
			ArrayList aMenuItems;

			switch(sDataType)
			{
				case "City":
					aMenuItems=LoadCities(sKeyword);
					break;
				default:
					throw new Exception("GetAutoSuggestData  Type '" + sDataType + "' is not supported.");
			}


			return aMenuItems;
		}	

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions