SIGN UP MEMBER LOGIN:    
ARTICLE

Geographical Information by IP Address in ASP.NET and C#

Posted by Rahul Kumar Saxena Articles | How do I June 02, 2009
This article and attached code demonstrates how to get a website visitor's geographical information such as country, region, city, latitude, longitude, zip code, time zone by using his or her IP address.
Reader Level:

There are a lot of services out there by using them we can get a Website Visitor Geographical information such as country, region, city, latitude, longitude, ZIP code, time zone and so on. It is possible just by using the IP address of website visitor's machine.  


Here, in this article, I am using a service provided by
http://iplocationtools.com/. This service is free and based on the IP, returns geographical data in 3 formats:

1. XML

2. JSON

3. CSV

 

This is the aspx code:

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

 

<!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>Ip Address Location</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:ScriptManager ID="ScriptManager1" runat="server">

        </asp:ScriptManager>

        <table cellpadding="0" cellspacing="0" width="50%" align="center" style="background-color: #f5f5f5;

            color: #4f6b72;">

            <tr>

                <td align="center">

                    <asp:UpdatePanel ID="UpdatePanel1" runat="server">

                        <ContentTemplate>

                            <asp:Panel ID="panelLoc" runat="server">

                                <asp:TextBox ID="txtIP" runat="server"></asp:TextBox>

                                <asp:Button ID="btnGetLoc" runat="server" Text="Get IP Details" OnClick="btnGetLoc_Click" />

                                <br />

                                <asp:Xml ID="Xml1" runat="server"></asp:Xml>

                            </asp:Panel>

                        </ContentTemplate>

                    </asp:UpdatePanel>

                    <asp:UpdateProgress ID="updProgress" AssociatedUpdatePanelID="UpdatePanel1" runat="server">

                        <ProgressTemplate>

                            <img alt="progress" src="images/Img.gif" />

                        </ProgressTemplate>

                    </asp:UpdateProgress>

                </td>

            </tr>

        </table>

    </div>

    </form>

</body>

</html>

 


Here is the code behind.

 

using System;

using System.Xml.Linq;

 

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

 

    }

 

    protected void btnGetLoc_Click(object sender, EventArgs e)

    {

        String url = String.Empty;       

 

        if (txtIP.Text.Trim() != String.Empty)

        {

            url = String.Format("http://iplocationtools.com/ip_query2.php?ip={0}", txtIP.Text.Trim());

            XDocument xDoc = XDocument.Load(url);

            if (xDoc == null | xDoc.Root == null)

            {

                throw new ApplicationException("Data is not Valid");

            }

 

            Xml1.TransformSource = "IP.xslt";

            Xml1.DocumentContent = xDoc.ToString();

        }      

    }

}

 

 

Here, I also observe that a XML control (<asp:Xml ID="Xml1") has been added to the page.

Our XSLT will look similar to the following:

<?xml version='1.0'?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:template match="/">

    <HTML>

      <BODY>

        <TABLE cellspacing="4" cellpadding="4">

          <xsl:for-each select="Locations/Location">

            <TR>

              <TD width="5%" bgcolor="#3BB9FF">

                <B>Ip</B>

              </TD>

              <TD width="5%" valign="top" bgcolor="#48CCCD">

                <xsl:value-of select="Ip"/>

              </TD>

              <TD></TD>

            </TR>

            <TR>

              <TD width="5%" bgcolor="#3BB9FF">

                <B>Status</B>

              </TD>

              <TD width="5%" valign="top" bgcolor="#48CCCD">

                <xsl:value-of select="Status"/>

              </TD>

              <TD></TD>

            </TR>

            <TR>

              <TD width="5%" bgcolor="#3BB9FF">

                <B>CountryCode</B>

              </TD>

              <TD width="5%" valign="top" bgcolor="#48CCCD">

                <xsl:value-of select="CountryCode"/>

              </TD>

              <TD></TD>

            </TR>

            <TR>

              <TD width="5%" bgcolor="#3BB9FF">

                <B>CountryName</B>

              </TD>

              <TD width="5%" valign="top" bgcolor="#48CCCD">

                <xsl:value-of select="CountryName"/>

              </TD>

              <TD></TD>

            </TR>

            <TR>

              <TD width="5%" bgcolor="#3BB9FF">

                <B>RegionCode</B>

              </TD>

              <TD width="5%" valign="top" bgcolor="#48CCCD">

                <xsl:value-of select="RegionCode"/>

              </TD>

              <TD></TD>

            </TR>

            <TR>

              <TD width="5%" bgcolor="#3BB9FF">

                <B>RegionName</B>

              </TD>

              <TD width="5%" valign="top" bgcolor="#48CCCD">

                <xsl:value-of select="RegionName"/>

              </TD>

              <TD></TD>

            </TR>

            <TR>

              <TD width="5%" bgcolor="#3BB9FF">

                <B>City</B>

              </TD>

              <TD width="5%" valign="top" bgcolor="#48CCCD">

                <xsl:value-of select="City"/>

              </TD>

              <TD></TD>

            </TR>

            <TR>

              <TD width="5%" bgcolor="#3BB9FF">

                <B>ZipPostalCode</B>

              </TD>

              <TD width="5%" valign="top" bgcolor="#48CCCD">

                <xsl:value-of select="ZipPostalCode"/>

              </TD>

              <TD></TD>

            </TR>

            <TR>

              <TD width="5%" bgcolor="#3BB9FF">

                <B>Latitude</B>

              </TD>

              <TD width="5%" valign="top" bgcolor="#48CCCD">

                <xsl:value-of select="Latitude"/>

              </TD>

              <TD></TD>

            </TR>

            <TR>

              <TD width="5%" bgcolor="#3BB9FF">

                <B>Longitude</B>

              </TD>

              <TD width="5%" valign="top" bgcolor="#48CCCD">

                <xsl:value-of select="Longitude"/>

              </TD>

              <TD></TD>

            </TR>

          </xsl:for-each>

        </TABLE>

      </BODY>

    </HTML>

  </xsl:template>

</xsl:stylesheet>

When you run the application and enter an IP address, you will data something like this.



IpLocation.JPG


Image 1.


Now if you wish to get geographical information of your website visitors, you can simply get IP address of a visitor from the Browser and pass that IP address in this service and get the desired data you want.



Login to add your contents and source code to this article
share this article :
post comment
 

'=' is an unexpected token. The expected token is ';'. Line 36, position 63.

Posted by ram atri Sep 15, 2011

this code is very good and make me feel better thanks alot again

Posted by manisha chauhan Aug 12, 2011

The 'meta' start tag on line 12 does not match the end tag of 'head'. Line 13, position 3.

Posted by ajay sharma Jul 29, 2011

Doesn't work. The small icon flashes for a second then disappears - nothing happens

Posted by Harold Steptoe Apr 15, 2011

i have download your code and run it. when pass the ip and click the button then create error Error is 'The 'meta' start tag on line 12 does not match the end tag of 'head'. Line 13, position 3.' My userid is:-surendrakata@gmail.com

Posted by surendra Mar 15, 2011
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor