Determine Web Visitors Country of Origin in the Drop Down List using .NET Framework in C# or VB.NET

With the emergence of online technologies such as the Internet, people and businesses have increased their reliance and use of these mediums as an avenue for commerce as it can be more convenient. During the transaction online, there are times when it is important to preset the web visitor's country of origin, ZIP code, ISP and domain name at the drop down list to prevent fraud and to ease the complexity of registration task. This article shows you how by using .NET Framework, it can be done.

Let us take a simple example of a user login from Canada and he needs to fill up a shopping cart. The form may be quite complex as some businesses need more information to prevent fraud. In this case, there are needs to preset certain info in the drop down list such as country of origin, ZIP code, ip and domain name of where the users login. As a result, the drop down list in this example will preset to Cananda, with the correct zip code and ip address.

In this example, we use a fully functional IP2Location™ .NET component available at http://www.ip2location.com/software/dot-net-component to query country by visitor's IP address. Firstly, install the IP2Location™ .NET component. The IP2Location™ .NET component will be installed in your local drive. Next, get the IP2Location.DLL .NET component and sample database from the directory, ie. c:\Program Files\IP2Location by default. You need to add a reference to this component from your Visual Studio web project. A copy of this component will be copied into /bin directory under the project. For unregistered component, there is a random 5-second delay in one out of ten queries.

Sample Code (VB.NET)

Imports IP2Location
Private Sub Query(ByVal strIPAddress As String)
   Dim oIPResult As New IP2Location.IPResult
   Try
   If strIPAddress <> "" Then
      IP2Location.Component.IPDatabasePath = "C:\\Program Files\\IP2Location\\Database\\IP-COUNTRY.SAMPLE.BIN"
oIPResult = IP2Location.Component.IPQuery(strIPAddress)
      Select Case oIPResult.Status
        Case "OK"
          If oIPResult.CountryShort <> "-" Then
            Response.Write("")
          End If
        Case "EMPTY_IP_ADDRESS"
            Response.Write("IP Address cannot be blank.")
        Case "INVALID_IP_ADDRESS"
            Response.Write("Invalid IP Address.")
        Case "MISSING_FILE"
            Response.Write("Invalid Database Path.")
     End Select
  Else
     Response.Write("IP Address cannot be blank.")
  End If
Catch ex As Exception
     Response.Write(ex.Message)
Finally
  oIPResult = Nothing
End Try
End Sub

Sample Code (C#)

Using IP2Location;
private void Query(string strIPAddress, string billingCountry)
{
  IPResult oIPResult = new IP2Location.IPResult();
  try
  {
    if (strIPAddress != "")
     {
      IP2Location.Component.IPDatabasePath = "C:\\Program Files\\IP2Location\\Database\\IP-COUNTRY.SAMPLE.BIN";
      oIPResult = IP2Location.Component.IPQuery(strIPAddress);
       switch(oIPResult.Status.ToString())
        {
          case "OK":
            if (oIPResult.CountryShort == billingCountry) {
              // buyer is from the same country by IP address
              } else {
              // buyer is from the different country by IP address
              }
          break;
           case "EMPTY_IP_ADDRESS":
                Response.Write("IP Address cannot be blank.");
                break;
           case "INVALID_IP_ADDRESS":
                Response.Write("Invalid IP Address.");
                break;
           case "MISSING_FILE":
                Response.Write("Invalid Database Path.");
                break;
           }
       }
       else
       {
        Response.Write("IP Address cannot be blank.");
       }
   }
    catch(Exception ex)
     {
      Response.Write(ex.Message);
     }
    finally
     {
      oIPResult = null;
     }
}

Related Products

IP2Location .NET Component
IP2Location .NET Component


Do you like this article? Share it with others by clicking the social media buttons below. We will write more articles related to this topic.