Display Advertisement by Country Using .NET Framework in C# or VB.NET


Hexasoft Development Sdn. Bhd. (645996-K)
1-2-15 Mayang Mall Complex,
Jalan Mayang Pasir 1,
11950 Bandar Bayan Baru,
Penang, Malaysia.
{ sales@ip2location.com }

Online advertising is another way to promote company products. It’s very important to show the right advertisements to the right consumers to have an optimum respond. A company selling their products in Japan showing their advertisement to visitors from United States is totally ineffective. On the other hand, localized advertisements catch visitor attention and improve sales.

In this example, we use a fully functional IP2Location™ .NET component available at http://www.ip2location.net/download/IP2LocationDotNetComponent.ZIP  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 Codes in VB.NET Webform


------------------------------
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 = "JP" Then
                 ' Visitor is from Japan
                 ' Show advertisement from JP
                 Response.Write "<img src=\"Japan.jpg\" border=\"0\" width=\"100\" height=\"200\">"
               Else
                  ' Visitor is not from Japan
                  ' Show other advertisement
                 Response.Write "<img src=\"US.jpg\" border=\"0\" width=\"100\" height=\"200\">"
               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
    
   Finally
    oIPResult = Nothing
   End Try
End Sub


Sample Codes in C# Webform
--------------------------
Using IP2Location;
private void Query(string strIPAddress)
{
    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 == "JP") {
              Response.Write "<img src=\"Japan.jpg\" border=\"0\" width=\"100\" height=\"200\">"
            }
            else {
              Response.Write "<img src=\"US.jpg\" border=\"0\" width=\"100\" height=\"200\">"
            }
            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)
    {
        ;
    }
    finally
    {
        oIPResult = null;
    }
}

 

Hexasoft Development Sdn. Bhd. © 2001-2009 All Right Reserved

To obtain permission to reuse or republish this article, please write to sales@ip2location.com. Republication is welcome for no charge.