Tuesday, June 30, 2009

Find GEO location through IP address

Geo IP Tool is a free service that helps you find geographical location via IP. I was trying to find out some service through which I can found the GEO information of customers via IP address. Below is a small helper code that can query Geo IP Tool against an IP. All you need is to place following helper class some where in your website. As you can see helper class only contains a single method GetLocationByIp of return type string. In fact it returns HTML containing returned information from Geo Ip Tool. Once you get the Geo information against an IP, all you need is to put it some literal control to be rendered.

Here is the GeoIPTool class, you can place it in your App_Code folder of ASP.NET website. Once you are done with this change then you can use user control code posted in next code portion for demo purpose.

using System;

using System.Data;

public static class GeoIPTool


public static string GetLocationByIP(string ipAddress)


string geoiptoolurl = "http://www.geoiptool.com/?IP={0}";

string gflag = "http://www.geoiptool.com/flags/";

string p1 = "<"+"table width=\"300\" height=\"300\"";

string p2 = " border=\"0\" cellpadding=\"4\"";

string p3 = " cellspacing=\"0\" class=\"tbl_style\">";

geoiptoolurl = string.Format(geoiptoolurl, ipAddress);

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(geoiptoolurl);

using (HttpWebResponse response = (HttpWebResponse)(request.GetResponse()))


using (StreamReader reader = new StreamReader(response.GetResponseStream()))


string htmlResult = reader.ReadToEnd();

if (!string.IsNullOrEmpty(htmlResult))


int sindex = htmlResult.IndexOf(p1+p2+p3);

htmlResult = htmlResult.Remove(0, sindex);

sindex = 0;

int eindex = htmlResult.IndexOf("");

string geoLocationTable = htmlResult.Substring(sindex, eindex + 8);

if (geoLocationTable.Contains("/flags/"))

geoLocationTable = geoLocationTable.Replace("/flags/",gflag);

return geoLocationTable;




return string.Empty;



Following is the demo user control, in order to use this code first you need to create a new user control file with name Sample.ascx in your website and then replace all its contents with following code. Finally replace Your IP Here text in GetLocationByIP call to your desired IP address.

<%@ Control Language="C#" ClassName="Sample" %>

<script runat="server">

protected void Page_Load(object sender, EventArgs e)


string geoLocation = GeoIPTool.GetLocationByIP("YOUR IP HERE");

if (!String.IsNullOrEmpty(geoLocation))

GeoLocationHolder.Controls.Add(new LiteralControl(geoLocation));


<asp:PlaceHolder ID="GeoLocationHolder" runat="server">asp:PlaceHolder>

Please feel free provide your feedback!

Jump to validation summary on large page

Validation summary is an awesome ASP.NET control that can list validation messages in a collective way. Some time if you have validation summary at bottom of a large page, in this case if some validation error occurs the user has to scroll down towards the bottom to view the pages.

You can enhance user experience with Validation Summary control by automatically jumping to the error messages area when some error occurred. I am just explaining the steps to accomplish this.

In ASP.NET there is a client side variable Page_IsValid that indicates whether the page is currently valid or not. The validation scripts keep this up to date at all times. So all you need is to wrap your validation summary within some div and place some anchor upon that. Finally write a small piece of javascript that checks Page_IsValid and if its set to false simply jump to that named anchor. You can found a very helpful topic about ASP.NET Validation available here

How to calculate column value as summation of all previouse values

This is the demonstration of how to calculate a column value that depends upon sum of all previous values in that column. For example check following two tables
Actual Data:
1Product A10
2Product B10
3Product C10
Required Output:
ProductIdNameCal Quantity
1Product A10
2Product B20
3Product C30

SQL Server Query:

SELECT P.ProductId, P.Name, SUM(PC.Quantity) AS Cal Quantity
ON PC.ProductId <= P.ProductId
GROUP BY P.ProductId, P.Name