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 https://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("<select name="country">")
            Response.Write("<option value="&
oIPResult.CountryShort & ">" & oIPResult.CountryLong & "</option>")
            Response.Write("<option value="AF">AFGHANISTAN</option>")
            Response.Write("<option value="AL">ALBANIA</option>")
            Response.Write("<option value="DZ">ALGERIA</option>")
            Response.Write("<option value="AS">AMERICAN SAMOA</option>")
            Response.Write("<option value="AD">ANDORRA</option>")
            Response.Write("<option value="AO">ANGOLA</option>")
            Response.Write("<option value="AI">ANGUILLA</option>")
            Response.Write("<option value="AQ">ANTARCTICA</option>")
            Response.Write("<option value="AG">ANTIGUA AND BARBUDA</option>")
            Response.Write("<option value="AR">ARGENTINA</option>")
            Response.Write("<option value="AM">ARMENIA</option>")
            Response.Write("<option value="AW">ARUBA</option>")
            Response.Write("<option value="AP">ASIA PACIFIC</option>")
            Response.Write("<option value="AU">AUSTRALIA</option>")
            Response.Write("<option value="AT">AUSTRIA</option>")
            Response.Write("<option value="AZ">AZERBAIJAN</option>")
            Response.Write("<option value="BS">BAHAMAS</option>")
            Response.Write("<option value="BH">BAHRAIN</option>")
            Response.Write("<option value="BD">BANGLADESH</option>")
            Response.Write("<option value="BB">BARBADOS</option>")
            Response.Write("<option value="BY">BELARUS</option>")
            Response.Write("<option value="BE">BELGIUM</option>")
            Response.Write("<option value="BZ">BELIZE</option>")
            Response.Write("<option value="BJ">BENIN</option>")
            Response.Write("<option value="BM">BERMUDA</option>")
            Response.Write("<option value="BT">BHUTAN</option>")
            Response.Write("<option value="BO">BOLIVIA</option>")
            Response.Write("<option value="BA">BOSNIA AND HERZEGOWINA</option>")
            Response.Write("<option value="BW">BOTSWANA</option>")
            Response.Write("<option value="BV">BOUVET ISLAND</option>")
            Response.Write("<option value="BR">BRAZIL</option>")
            Response.Write("<option value="IO">BRITISH INDIAN OCEAN TERRITORY</option>")
            Response.Write("<option value="BN">BRUNEI DARUSSALAM</option>")
            Response.Write("<option value="BG">BULGARIA</option>")
            Response.Write("<option value="BF">BURKINA FASO</option>")
            Response.Write("<option value="BI">BURUNDI</option>")
            Response.Write("<option value="KH">CAMBODIA</option>")
            Response.Write("<option value="CM">CAMEROON</option>")
            Response.Write("<option value="CA">CANADA</option>")
            Response.Write("<option value="CV">CAPE VERDE</option>")
            Response.Write("<option value="KY">CAYMAN ISLANDS</option>")
            Response.Write("<option value="CF">CENTRAL AFRICAN REPUBLIC</option>")
            Response.Write("<option value="TD">CHAD</option>")
            Response.Write("<option value="CL">CHILE</option>")
            Response.Write("<option value="CN">CHINA</option>")
            Response.Write("<option value="CX">CHRISTMAS ISLAND</option>")
            Response.Write("<option value="CC">COCOS (KEELING) ISLANDS</option>")
            Response.Write("<option value="CO">COLOMBIA</option>")
            Response.Write("<option value="KM">COMOROS</option>")
            Response.Write("<option value="CG">CONGO</option>")
            Response.Write("<option value="CD">CONGO, THE DEMOCRATIC REPUBLIC OF THE</option>")
            Response.Write("<option value="CK">COOK ISLANDS</option>")
            Response.Write("<option value="CR">COSTA RICA</option>")
            Response.Write("<option value="CI">COTE D'IVOIRE</option>")
            Response.Write("<option value="HR">CROATIA</option>")
            Response.Write("<option value="CU">CUBA</option>")
            Response.Write("<option value="CY">CYPRUS</option>")
            Response.Write("<option value="CZ">CZECH REPUBLIC</option>")
            Response.Write("<option value="CS">CZECHOSLOVAKIA (FORMER)</option>")
            Response.Write("<option value="DK">DENMARK</option>")
            Response.Write("<option value="DJ">DJIBOUTI</option>")
            Response.Write("<option value="DM">DOMINICA</option>")
            Response.Write("<option value="DO">DOMINICAN REPUBLIC</option>")
            Response.Write("<option value="TP">EAST TIMOR</option>")
            Response.Write("<option value="EC">ECUADOR</option>")
            Response.Write("<option value="EG">EGYPT</option>")
            Response.Write("<option value="SV">EL SALVADOR</option>")
            Response.Write("<option value="GQ">EQUATORIAL GUINEA</option>")
            Response.Write("<option value="ER">ERITREA</option>")
            Response.Write("<option value="EE">ESTONIA</option>")
            Response.Write("<option value="ET">ETHIOPIA</option>")
            Response.Write("<option value="EU">EUROPEAN UNION</option>")
            Response.Write("<option value="FK">FALKLAND ISLANDS (MALVINAS)</option>")
            Response.Write("<option value="FO">FAROE ISLANDS</option>")
            Response.Write("<option value="FJ">FIJI</option>")
            Response.Write("<option value="FI">FINLAND</option>")
            Response.Write("<option value="FR">FRANCE</option>")
            Response.Write("<option value="FX">FRANCE, METROPOLITAN</option>")
            Response.Write("<option value="GF">FRENCH GUIANA</option>")
            Response.Write("<option value="PF">FRENCH POLYNESIA</option>")
            Response.Write("<option value="TF">FRENCH SOUTHERN TERRITORIES</option>")
            Response.Write("<option value="GA">GABON</option>")
            Response.Write("<option value="GM">GAMBIA</option>")
            Response.Write("<option value="GE">GEORGIA</option>")
            Response.Write("<option value="DE">GERMANY</option>")
            Response.Write("<option value="GH">GHANA</option>")
            Response.Write("<option value="GI">GIBRALTAR</option>")
            Response.Write("<option value="GB">GREAT BRITAIN</option>")
            Response.Write("<option value="GR">GREECE</option>")
            Response.Write("<option value="GL">GREENLAND</option>")
            Response.Write("<option value="GD">GRENADA</option>")
            Response.Write("<option value="GP">GUADELOUPE</option>")
            Response.Write("<option value="GU">GUAM</option>")
            Response.Write("<option value="GT">GUATEMALA</option>")
            Response.Write("<option value="GN">GUINEA</option>")
            Response.Write("<option value="GW">GUINEA-BISSAU</option>")
            Response.Write("<option value="GY">GUYANA</option>")
            Response.Write("<option value="HT">HAITI</option>")
            Response.Write("<option value="HM">HEARD ISLAND AND MCDONALD ISLANDS</option>")
            Response.Write("<option value="VA">HOLY SEE (VATICAN CITY STATE)</option>")
            Response.Write("<option value="HN">HONDURAS</option>")
            Response.Write("<option value="HK">HONG KONG</option>")
            Response.Write("<option value="HU">HUNGARY</option>")
            Response.Write("<option value="IS">ICELAND</option>")
            Response.Write("<option value="IN">INDIA</option>")
            Response.Write("<option value="ID">INDONESIA</option>")
            Response.Write("<option value="IR">IRAN, ISLAMIC REPUBLIC OF</option>")
            Response.Write("<option value="IQ">IRAQ</option>")
            Response.Write("<option value="IE">IRELAND</option>")
            Response.Write("<option value="IL">ISRAEL</option>")
            Response.Write("<option value="IT">ITALY</option>")
            Response.Write("<option value="JM">JAMAICA</option>")
            Response.Write("<option value="JP">JAPAN</option>")
            Response.Write("<option value="JO">JORDAN</option>")
            Response.Write("<option value="KZ">KAZAKSTAN</option>")
            Response.Write("<option value="KE">KENYA</option>")
            Response.Write("<option value="KI">KIRIBATI</option>")
            Response.Write("<option value="KP">KOREA, DEMOCRATIC PEOPLE'S REPUBLIC OF</option>")
            Response.Write("<option value="KR">KOREA, REPUBLIC OF</option>")
            Response.Write("<option value="KW">KUWAIT</option>")
            Response.Write("<option value="KG">KYRGYZSTAN</option>")
            Response.Write("<option value="LA">LAO PEOPLE'S DEMOCRATIC REPUBLIC</option>")
            Response.Write("<option value="LV">LATVIA</option>")
            Response.Write("<option value="LB">LEBANON</option>")
            Response.Write("<option value="LS">LESOTHO</option>")
            Response.Write("<option value="LR">LIBERIA</option>")
            Response.Write("<option value="LY">LIBYAN ARAB JAMAHIRIYA</option>")
            Response.Write("<option value="LI">LIECHTENSTEIN</option>")
            Response.Write("<option value="LT">LITHUANIA</option>")
            Response.Write("<option value="LU">LUXEMBOURG</option>")
            Response.Write("<option value="MO">MACAU</option>")
            Response.Write("<option value="MK">MACEDONIA, THE FORMER YUGOSLAV REPUBLIC OF</option>")
            Response.Write("<option value="MG">MADAGASCAR</option>")
            Response.Write("<option value="MW">MALAWI</option>")
            Response.Write("<option value="MY">MALAYSIA</option>")
            Response.Write("<option value="MV">MALDIVES</option>")
            Response.Write("<option value="ML">MALI</option>")
            Response.Write("<option value="MT">MALTA</option>")
            Response.Write("<option value="MH">MARSHALL ISLANDS</option>")
            Response.Write("<option value="MQ">MARTINIQUE</option>")
            Response.Write("<option value="MR">MAURITANIA</option>")
            Response.Write("<option value="MU">MAURITIUS</option>")
            Response.Write("<option value="YT">MAYOTTE</option>")
            Response.Write("<option value="MX">MEXICO</option>")
            Response.Write("<option value="FM">MICRONESIA, FEDERATED STATES OF</option>")
            Response.Write("<option value="MD">MOLDOVA, REPUBLIC OF</option>")
            Response.Write("<option value="MC">MONACO</option>")
            Response.Write("<option value="MN">MONGOLIA</option>")
            Response.Write("<option value="MS">MONTSERRAT</option>")
            Response.Write("<option value="MA">MOROCCO</option>")
            Response.Write("<option value="MZ">MOZAMBIQUE</option>")
            Response.Write("<option value="MM">MYANMAR</option>")
            Response.Write("<option value="NA">NAMIBIA</option>")
            Response.Write("<option value="NR">NAURU</option>")
            Response.Write("<option value="NP">NEPAL</option>")
            Response.Write("<option value="NL">NETHERLANDS</option>")
            Response.Write("<option value="AN">NETHERLANDS ANTILLES</option>")
            Response.Write("<option value="NC">NEW CALEDONIA</option>")
            Response.Write("<option value="NZ">NEW ZEALAND</option>")
            Response.Write("<option value="NI">NICARAGUA</option>")
            Response.Write("<option value="NE">NIGER</option>")
            Response.Write("<option value="NG">NIGERIA</option>")
            Response.Write("<option value="NU">NIUE</option>")
            Response.Write("<option value="NF">NORFOLK ISLAND</option>")
            Response.Write("<option value="MP">NORTHERN MARIANA ISLANDS</option>")
            Response.Write("<option value="NO">NORWAY</option>")
            Response.Write("<option value="OM">OMAN</option>")
            Response.Write("<option value="PK">PAKISTAN</option>")
            Response.Write("<option value="PW">PALAU</option>")
            Response.Write("<option value="PS">PALESTINIAN TERRITORY, OCCUPIED</option>")
            Response.Write("<option value="PA">PANAMA</option>")
            Response.Write("<option value="PG">PAPUA NEW GUINEA</option>")
            Response.Write("<option value="PY">PARAGUAY</option>")
            Response.Write("<option value="PE">PERU</option>")
            Response.Write("<option value="PH">PHILIPPINES</option>")
            Response.Write("<option value="PN">PITCAIRN</option>")
            Response.Write("<option value="PL">POLAND</option>")
            Response.Write("<option value="PT">PORTUGAL</option>")
            Response.Write("<option value="PR">PUERTO RICO</option>")
            Response.Write("<option value="QA">QATAR</option>")
            Response.Write("<option value="RE">REUNION</option>")
            Response.Write("<option value="RO">ROMANIA</option>")
            Response.Write("<option value="SU">RUSSIAN FEDERATION</option>")
            Response.Write("<option value="RW">RWANDA</option>")
            Response.Write("<option value="SH">SAINT HELENA</option>")
            Response.Write("<option value="KN">SAINT KITTS AND NEVIS</option>")
            Response.Write("<option value="LC">SAINT LUCIA</option>")
            Response.Write("<option value="PM">SAINT PIERRE AND MIQUELON</option>")
            Response.Write("<option value="VC">SAINT VINCENT AND THE GRENADINES</option>")
            Response.Write("<option value="WS">SAMOA</option>")
            Response.Write("<option value="SM">SAN MARINO</option>")
            Response.Write("<option value="ST">SAO TOME AND PRINCIPE</option>")
            Response.Write("<option value="SA">SAUDI ARABIA</option>")
            Response.Write("<option value="SN">SENEGAL</option>")
            Response.Write("<option value="YU">SERBIA AND MONTENEGRO</option>")
            Response.Write("<option value="SC">SEYCHELLES</option>")
            Response.Write("<option value="SL">SIERRA LEONE</option>")
            Response.Write("<option value="SG">SINGAPORE</option>")
            Response.Write("<option value="SK">SLOVAKIA</option>")
            Response.Write("<option value="SI">SLOVENIA</option>")
            Response.Write("<option value="SB">SOLOMON ISLANDS</option>")
            Response.Write("<option value="SO">SOMALIA</option>")
            Response.Write("<option value="ZA">SOUTH AFRICA</option>")
            Response.Write("<option value="GS">SOUTH GEORGIA AND THE SOUTH SANDWICH ISLANDS</option>")
            Response.Write("<option value="ES">SPAIN</option>")
            Response.Write("<option value="LK">SRI LANKA</option>")
            Response.Write("<option value="SD">SUDAN</option>")
            Response.Write("<option value="SR">SURINAME</option>")
            Response.Write("<option value="SJ">SVALBARD AND JAN MAYEN</option>")
            Response.Write("<option value="SZ">SWAZILAND</option>")
            Response.Write("<option value="SE">SWEDEN</option>")
            Response.Write("<option value="CH">SWITZERLAND</option>")
            Response.Write("<option value="SY">SYRIAN ARAB REPUBLIC</option>")
            Response.Write("<option value="TW">TAIWAN, PROVINCE OF CHINA</option>")
            Response.Write("<option value="TJ">TAJIKISTAN</option>")
            Response.Write("<option value="TZ">TANZANIA, UNITED REPUBLIC OF</option>")
            Response.Write("<option value="TH">THAILAND</option>")
            Response.Write("<option value="TG">TOGO</option>")
            Response.Write("<option value="TK">TOKELAU</option>")
            Response.Write("<option value="TO">TONGA</option>")
            Response.Write("<option value="TT">TRINIDAD AND TOBAGO</option>")
            Response.Write("<option value="TN">TUNISIA</option>")
            Response.Write("<option value="TR">TURKEY</option>")
            Response.Write("<option value="TM">TURKMENISTAN</option>")
            Response.Write("<option value="TC">TURKS AND CAICOS ISLANDS</option>")
            Response.Write("<option value="TV">TUVALU</option>")
            Response.Write("<option value="UG">UGANDA</option>")
            Response.Write("<option value="UA">UKRAINE</option>")
            Response.Write("<option value="AE">UNITED ARAB EMIRATES</option>")
            Response.Write("<option value="UK">UNITED KINGDOM</option>")
            Response.Write("<option value="US">UNITED STATES</option>")
            Response.Write("<option value="UM">UNITED STATES MINOR OUTLYING ISLANDS</option>")
            Response.Write("<option value="UY">URUGUAY</option>")
            Response.Write("<option value="UZ">UZBEKISTAN</option>")
            Response.Write("<option value="VU">VANUATU</option>")
            Response.Write("<option value="VE">VENEZUELA</option>")
            Response.Write("<option value="VN">VIET NAM</option>")
            Response.Write("<option value="VG">VIRGIN ISLANDS, BRITISH</option>")
            Response.Write("<option value="VI">VIRGIN ISLANDS, U.S.</option>")
            Response.Write("<option value="WF">WALLIS AND FUTUNA</option>")
            Response.Write("<option value="EH">WESTERN SAHARA</option>")
            Response.Write("<option value="YE">YEMEN</option>")
            Response.Write("<option value="ZM">ZAMBIA</option>")
            Response.Write("<option value="ZW">ZIMBABWE</option>")
            Response.Write("</select>")
          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;
     }
}

Was this article helpful?

Related Articles