1using Microsoft.ApplicationBlocks.Data;
2using System;
3using System.Configuration;
4using System.ComponentModel;
5using System.Data;
6using System.Data.SqlClient;
7using System.Web;
8using System.Web.UI;
9
10namespace ZipCodeDistance
11{
12	public class Default : System.Web.UI.Page
13	{
14		#region Web Form Designer generated code
15
16		protected System.Web.UI.WebControls.RadioButton rdoProduct;
17		protected System.Web.UI.WebControls.Repeater rptHouses;
18		protected System.Web.UI.WebControls.TextBox txtZipCode;
19		protected System.Web.UI.WebControls.DropDownList cboWithin;
20		protected System.Web.UI.WebControls.Button cmdSearch;
21		protected System.Web.UI.WebControls.Label lblMessage;
22		protected System.Web.UI.WebControls.Image Image1;
23
24		override protected void OnInit(EventArgs e)
25		{
26			//
27			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
28			//
29			InitializeComponent();
30			base.OnInit(e);
31		}
32		
33		/// <summary>
34		/// Required method for Designer support - do not modify
35		/// the contents of this method with the code editor.
36		/// </summary>
37		private void InitializeComponent()
38		{    
39			this.cmdSearch.Click += new System.EventHandler(this.cmdSearch_Click);
40			this.Load += new System.EventHandler(this.Page_Load);
41
42		}
43		#endregion
44		#region Page_Load
45		private void Page_Load(object sender, System.EventArgs e)
46		{
47			if (!IsPostBack)
48			{
49				//Hide results controls
50				rptHouses.Visible = false;
51				lblMessage.Text = "";
52				//Populate within values (real app would pull from DB)
53				cboWithin.Items.Add("Only this zip");
54				cboWithin.Items[cboWithin.Items.Count - 1].Value = "0";
55				cboWithin.Items.Add("5 miles");
56				cboWithin.Items[cboWithin.Items.Count - 1].Value = "5";
57				cboWithin.Items.Add("10 miles");
58				cboWithin.Items[cboWithin.Items.Count - 1].Value = "10";
59				cboWithin.Items.Add("25 miles");
60				cboWithin.Items[cboWithin.Items.Count - 1].Value = "25";
61				cboWithin.Items.Add("50 miles");
62				cboWithin.Items[cboWithin.Items.Count - 1].Value = "50";
63				cboWithin.Items.Add("100 miles");
64				cboWithin.Items[cboWithin.Items.Count - 1].Value = "100";
65				cboWithin.Items.Add("200 miles");
66				cboWithin.Items[cboWithin.Items.Count - 1].Value = "200";
67				cboWithin.Items.Add("No limit");
68				cboWithin.Items[cboWithin.Items.Count - 1].Value = "999";
69			}
70		}
71		#endregion
72		#region cmdSearch_Click
73		private void cmdSearch_Click(object sender, System.EventArgs e)
74		{
75			DataTable objDT = null;
76			try
77			{
78				//Query the database for houses
79				objDT = GetHousesBySearchCriteria();
80				//Any houses found?
81				if (objDT.Rows.Count == 0)
82				{
83					//None found - hide repeater, show message
84					rptHouses.Visible = false;
85					lblMessage.Text = "No results found";
86				}
87				else
88				{
89					//Houses found - show repeater, hide message
90					rptHouses.DataSource = objDT;
91					rptHouses.DataBind();
92					rptHouses.Visible = true;
93					lblMessage.Text = "";
94				}
95			}
96			catch (Exception ex)
97			{
98				//Add your own error handling here
99				lblMessage.Text = "Error: " + ex.ToString();
100			}
101			finally
102			{
103				//Release memory
104				objDT = null;
105			}
106		}
107		#endregion
108		#region GetHousesBySearchCriteria
109		public DataTable GetHousesBySearchCriteria()
110		{
111			//Use the Microsoft Data Application Blocks to query database
112			DataSet objDS = new DataSet();
113			SqlParameter[] arParms = new SqlParameter[2];
114			arParms[0] = new SqlParameter("@Zipcode", SqlDbType.Char);
115			arParms[0].Value = txtZipCode.Text;
116			arParms[1] = new SqlParameter("@Miles", SqlDbType.Decimal);
117			arParms[1].Value = Int16.Parse(cboWithin.SelectedItem.Value);
118			//Return a DataTable
119			return SqlHelper.ExecuteDataset(ConfigurationSettings.AppSettings["ConnectionString"],
120				CommandType.StoredProcedure, "spHouses_GetNearZipcode", arParms).Tables[0];
121		}
122		#endregion
123	}
124}