Tuesday, September 19, 2017

ASPX - ZipCountyLookup.aspx 

//NOTE: This sample is great for an ASP/ASPX website page, where the code pages (and session) were combined during a 3 year upgrade of 3000 ASP pages... which explains some of the "loosely coupled ends" demonstrated below... The result of this code page essentially builds a dynamic list of counties, to be placed into another web control...

**** ZipCountyLookup.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class ZipCountyLookup_ZipCountyLookup : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
try
{
string zipCode = Request["ZipCode"];
using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["dbDatabaseConnection"].ConnectionString))
{
cn.Open();
SqlCommand cmd = new SqlCommand("select szCounty from dbDatabaseName..tblZipcodes     where szZip_Code = @szZip_Code", cn);
cmd.Parameters.Add(new SqlParameter("szZip_Code", zipCode));
SqlDataReader dr = cmd.ExecuteReader();

Response.Write("<counties>");
int i = 0;
while (dr.Read())
{
Response.Write("<county cid=\"" + i + "\">" + dr["szCounty"] + "</county>");
i++;
}
Response.Write("</counties>");
dr.Close();
Response.End();
}
}
catch (Exception)
{
Response.End();
}
    }

}

----------------------------------------------------------------------------------------------------------
**** ZipCountyLookup.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ZipCountyLookup.aspx.cs" Inherits="ZipCountyLookup_ZipCountyLookup" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>ZipcodeLookups</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
         {intentionally blank...}
    </div>
    </form>
</body>
</html>

No comments:

Post a Comment