sandeep sir

Saturday, 6 June 2015

USPS address validation in asp.net

USPS/ Endecia  address validation in asp.net.

Hi guys , Today i am going to show you  street level address validation  of USPS/Endecia. Since Endecia already have provided us very good services so i have used them. here is the sample code. 

Here i am taking three parameters as input.
1. Address(Street level).
2. City
3. State
Now on my web form i have taken three textboxes like below.
<form id="form1" runat="server">
         <div style="width:500px; margin-top:10px;">
            <h2>USPS/Endecia Address Validation</h2>
        </div>
        <hr />
        <table class="auto-style1">
            <tr>
                <td class="auto-style5">&nbsp;</td>
                <td class="auto-style5">&nbsp;</td>
                <td class="auto-style5">&nbsp;</td>
                <td class="auto-style5">&nbsp;</td>
                <td class="auto-style5">&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style5">&nbsp;</td>
                <td class="auto-style5">Enter Street address</td>
                <td class="auto-style5">
                    <asp:TextBox ID="tbxAddress" runat="server"></asp:TextBox>
                </td>
                <td class="auto-style5">&nbsp;</td>
                <td class="auto-style5">&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style5">&nbsp;</td>
                <td class="auto-style5">&nbsp;</td>
                <td class="auto-style5">&nbsp;</td>
                <td class="auto-style5">&nbsp;</td>
                <td class="auto-style5">&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style5">&nbsp;</td>
                <td class="auto-style5">Enter City</td>
                <td class="auto-style5">
                    <asp:TextBox ID="tbxCity" runat="server"></asp:TextBox>
                </td>
                <td class="auto-style5">&nbsp;</td>
                <td class="auto-style5">&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style5">&nbsp;</td>
                <td class="auto-style5">&nbsp;</td>
                <td class="auto-style5">&nbsp;</td>
                <td class="auto-style5">&nbsp;</td>
                <td class="auto-style5">&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style5">&nbsp;</td>
                <td class="auto-style5">Enter StateCode</td>
                <td class="auto-style5">
                    <asp:TextBox ID="tbxStateCode" runat="server"></asp:TextBox>
                </td>
                <td class="auto-style5">&nbsp;</td>
                <td class="auto-style5">&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style5">&nbsp;</td>
                <td class="auto-style5">&nbsp;</td>
                <td class="auto-style5">&nbsp;</td>
                <td class="auto-style5">&nbsp;</td>
                <td class="auto-style5">&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style5">&nbsp;</td>
                <td class="auto-style5">&nbsp;</td>
                <td class="auto-style5">
                    <asp:Button ID="btnValidateAddress" runat="server" Text="Validate Address" OnClick="btnValidateAddress_Click" />
                </td>
                <td class="auto-style5">&nbsp;</td>
                <td class="auto-style5">&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style5">&nbsp;</td>
                <td class="auto-style5">&nbsp;</td>
                <td class="auto-style5">&nbsp;</td>
                <td class="auto-style5">&nbsp;</td>
                <td class="auto-style5">&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style5">Address Validation Result:</td>
                <td class="auto-style5">&nbsp;</td>
                <td class="auto-style5">&nbsp;</td>
                <td class="auto-style5">&nbsp;</td>
                <td class="auto-style5">&nbsp;</td>
            </tr>
        </table>
        <div style="margin-top:5px; margin-bottom:5px;">
            <asp:Label ID="lblMsg" runat="server" Text=""></asp:Label>
        </div>
    <div style="margin-top:20px;">
    
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None">
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <asp:TemplateField HeaderText="Select Address">
                    <ItemTemplate>
                        <asp:CheckBox ID="chkboxSelect" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Address">
                    <ItemTemplate>
                        <asp:Label ID="lblAddress" runat="server" Text='<%#Eval("address") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="City">
                    <ItemTemplate>
                        <asp:Label ID="lblCity" runat="server" Text='<%#Eval("city") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>

                 <asp:TemplateField HeaderText="State">
                    <ItemTemplate>
                        <asp:Label ID="lblState" runat="server" Text='<%#Eval("state") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Zip5">
                    <ItemTemplate>
                        <asp:Label ID="lblZip5" runat="server" Text='<%#Eval("zip5") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Zip4">
                    <ItemTemplate>
                        <asp:Label ID="lblZip4" runat="server" Text='<%#Eval("zip4") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <EditRowStyle BackColor="#2461BF" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#EFF3FB" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#F5F7FB" />
            <SortedAscendingHeaderStyle BackColor="#6D95E1" />
            <SortedDescendingCellStyle BackColor="#E9EBEF" />
            <SortedDescendingHeaderStyle BackColor="#4870BE" />
        </asp:GridView>
    
    </div>
    </form>

Now in following code on button click.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Xml;
using System.IO;
using System.Text;

public partial class Blog : System.Web.UI.Page
{
    StringBuilder output = new StringBuilder();
    string strDAta;
    DataTable dt = new DataTable();
    List<string> allAddress = new List<string>();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnValidateAddress_Click(object sender, EventArgs e)
    {
        USPSAddressValidation();

    }
    public void USPSAddressValidation()
    {
        dt.Columns.Add("address");
        dt.Columns.Add("city");
        dt.Columns.Add("state");
        dt.Columns.Add("zip5");
        dt.Columns.Add("zip4");
        string strURL = "http://production.shippingapis.com/ShippingAPITest.dll?API=Verify&XML=<AddressValidateRequest%20USERID='801SPD007842'>" +
           "<Address><Address1></Address1><Address2>"+tbxAddress.Text+"</Address2><City>"+tbxCity.Text+"</City><State>"+tbxStateCode.Text+"</State> <Zip5></Zip5> <Zip4></Zip4> " +
           "</Address>" +
           "</AddressValidateRequest>";
        using (var webClient = new System.Net.WebClient())
        {
            var json = webClient.DownloadString(strURL);
            strDAta = json;// "<?xml version='1.0' encoding='UTF-8'?><AddressValidateResponse><Address><Address2>6406 IVY LN</Address2><City>GREENBELT</City><State>MD</State><Zip5>20770</Zip5><Zip4>1441</Zip4></Address></AddressValidateResponse>";// Convert.ToString(json);
        }
        using (XmlReader reader = XmlReader.Create(new StringReader(strDAta)))
        {
            XmlWriterSettings ws = new XmlWriterSettings();
            ws.Indent = true;
            using (XmlWriter writer = XmlWriter.Create(output, ws))
            {
                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                        case XmlNodeType.Element:
                            writer.WriteStartElement(reader.Name);
                            break;
                        case XmlNodeType.Text:
                            writer.WriteString(reader.Value);
                            allAddress.Add(reader.Value);
                            break;
                        case XmlNodeType.XmlDeclaration:
                        case XmlNodeType.ProcessingInstruction:
                            writer.WriteProcessingInstruction(reader.Name, reader.Value);
                            break;
                        case XmlNodeType.Comment:
                            writer.WriteComment(reader.Value);
                            break;
                        case XmlNodeType.EndElement:
                            writer.WriteFullEndElement();
                            break;
                    }
                }//end while
            }//end xmlwriter
        }//end reader
        if (allAddress.Count == 3)
        {
            lblMsg.Text = "Not found: " + allAddress[0] + ": " + allAddress[1] + " : " + allAddress[2];
            lblMsg.ForeColor = System.Drawing.Color.Red;
            return;
        }
        lblMsg.Text = "";
        for (int i = 0; allAddress.Count > i; i = i + 5)
        {
            dt.Rows.Add(allAddress[i], allAddress[i + 1], allAddress[i + 2], allAddress[i + 3], allAddress[i + 4]);
        }
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}

Now run the application following output you will get.



Have a great time..... your comments are highly appreciable. 



0 comments:

Post a Comment