User Id :    Password :      New Member   Forgot Password  
How to get visitors Country, longitude, latitude from IP address is
Description Here I will show you to how to get visitor's Country,longitude,latitude from IP address is   No. of Views     21670
  Rating     1
Author Ahsan Murshed   Posted On     11 Jul 2010
Tags ASP.NET,C#,XML    

Sample Code   Download Code


It is very common questions for new web developer how to get the client's country location to track the user.In various forum I have faced this questions. So that today I want to share how to get client's country location,longitude,latitude,country code etc. To get this you need to use a
free database which has IP to Location mapping
call a web service that does the same for you.

Using free web services is the easy way to get the location of the user based on its IP address. After goggling I have found the following web service which provide this service absolutely free and that too without any complex interface to do the same.

The above website provides free IP Geolocation Web Service that returns data in three formats .

1. XML [Extended Markup Language]
2. CSV [Comma Separated Values]
3. JSON [JavaScript Object Notation]

Here I am explaining how to get the data in XML format. It is very easy to use this web service,just send your ip address through the URL

The returned XML with result are as below:


How to consume this web service and get the result: Here WebRequest and WebProxy is responsible for make call this url and xml response is received by WebResponse and store it to dataset using XMLTextReader.



private DataTable GetGeoLocation(string ipaddress)
    //Create a WebRequest
    WebRequest rssReq = WebRequest.Create(""
    + ipaddress);
    //Create a Proxy
    WebProxy px =new WebProxy("" + ipaddress, true);

    //Assign the proxy to the WebRequest
    rssReq.Proxy = px;

    //Set the timeout in Seconds for the WebRequest
    rssReq.Timeout = 2000;
    { //Get the WebResponse
    WebResponse rep = rssReq.GetResponse();

    //Read the Response in a XMLTextReader
    XmlTextReader xtr = new XmlTextReader(rep.GetResponseStream());

    //Create a new DataSet
    DataSet ds = new DataSet();

    //Read the Response into the DataSet
    return ds.Tables[0];
    return null;

How to show result in aspx page:
You can place this code on Page_Load event or under any button event whenever you like:

    protected void ShowGeoLocatio()
    string sIpaddress;
    sIpaddress= Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
    if(sIpaddress== "" || sIpaddress== null)
    sIpaddress= Request.ServerVariables["REMOTE_ADDR"];

    //call the function to consume web service and store result into datatable
    DataTable odtGeoLocation = GetGeoLocation(sIpaddress);
    if (odtGeoLocation != null)
    if (odtGeoLocation .Rows.Count > 0)
    lblCity.Text = odtGeoLocation .Rows[0]["City"].ToString();
    lblRegion.Text = odtGeoLocation .Rows[0]["RegionName"].ToString();
    lblCountry.Text = odtGeoLocation .Rows[0]["CountryName"].ToString();
    lblCountryCode.Text = odtGeoLocation .Rows[0]["CountryCode"].ToString();
    lblError="Sorry,no data found!!";

Hope that it may helps the developer.Thanks.

About Author

About Author I am working as Software engineer having 4+ years of experience in Microsoft Technologies, System Design and Integration and deployment, Search Engine Optimization (SEO). I have developed web based application in and database design. My Site: My blog: Ahsan
No Photo
Country N/A
Company N/A
Home Page N/A

Rate this article

Rating options from poor, fair, good, very good to excelent.  


Posted By Shaheen on 25 Jul 2011 at 11:04 PM
Hello Ankit,
Client location is tracked through the internet connection and the communication devices like moden and router. So depending on the router location it shows you the client location.
Posted By Najmul Hoda on 25 Jul 2011 at 10:19 PM
Hi Ankit,
If this dont work for you please try this link.

Hope this will help you.
Posted By Ankit Garg on 25 Jul 2011 at 10:36 AM
API not working properly(
After publishing that code, it is providing different results when i check it from my landline connection and the same with my mobile device. when i use it from mobile , it connects to other countries routers and displays that countries latitude-longitude.
please help me
Write your comment here.
Verification Code