Display Visitor's Country Currency Using ASP and MS-SQL 2008 Database

E-commerce businesses often have to deal with customers from around the globe. However, some customers may have difficulty to convert the currency displayed to their origin currency. It’s user friendly to show the pricing in a website in visitor origin currency. With the origin currency, visitors can navigate faster and feel more comfortable to make purchases.

Step 1: Start the SQL Server Management Studio. Run the following SQL commands.

CREATE DATABASE ip2location;
			USE ip2location;

Step 2: Create 'ip_country' table

CREATE TABLE [ip2location].[dbo].[ip_country] (
				[ip_from] bigint NOT NULL,
				[ip_to] bigint NOT NULL,
				[country_code] nvarchar(2) NOT NULL,
				[country_name] nvarchar(64) NOT NULL
				) ON [PRIMARY]
			GO

Step 3. Import the data into the table.

			BULK INSERT [ip2location].[dbo].[ip_country]
				FROM 'C:\[path to your CSV file]\IP-COUNTRY.CSV'
				WITH
				(
					FORMATFILE = 'C:\[path to your DB1.FMT file]\DB1.FMT'
				)
			GO
			

NOTE: You will need to copy the FMT code below and save it as a file named DB1.FMT on your computer.

			10.0
			5
			1 SQLCHAR 0 1 "\"" 0 first_double_quote  Latin1_General_CI_AI
			2 SQLCHAR 0 20 "\",\"" 1 ip_from ""
			3 SQLCHAR 0 20 "\",\"" 2 ip_to ""
			4 SQLCHAR 0 2 "\",\"" 3 country_code Latin1_General_CI_AI
			5 SQLCHAR 0 64 "\"\r\n" 4 country_name Latin1_General_CI_AI
			

Run the below SQL command to create a clustered index on the ip_to field.

			CREATE UNIQUE CLUSTERED INDEX [ip_to] ON [ip2location].[dbo].[ip_country]
			(
				[ip_to] ASC
			) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
			

The full version of IP-Country database is available for subscription at $49/year at http://ip2location.com/databases/db1-ip-country.

Step 4. Download world country information at here. Create 'country' table

CREATE TABLE [ip2location].[dbo].[country] (
				[iso] [nvarchar] (2),
				[country_name][nvarchar] (100),
				[capital_name] [nvarchar] (100),
				[tld] [nvarchar] (3),
				[currency_code] [nvarchar] (3),
				[currency_name] [nvarchar] (50)
			) ON [PRIMARY]
			GO

Step 5. Import the data into the table.

			BULK INSERT [ip2location].[dbo].[country]
				FROM 'C:\[path to your CSV file]\country.csv'
				WITH
				(
					FORMATFILE = 'C:\[path to your country.FMT file]\country.FMT'
				)
			GO
			

NOTE: You will need to copy the FMT code below and save it as a file named country.FMT on your computer.

			10.0
			7
			1 SQLCHAR 0 1 "\"" 0 first_double_quote  Latin1_General_CI_AI			
			2 SQLCHAR 0 2 "\",\"" 1 iso ""
			3 SQLCHAR 0 100 "\",\"" 2 country_name Latin1_General_CI_AI
			4 SQLCHAR 0 100 "\",\"" 3 capital_name Latin1_General_CI_AI
			5 SQLCHAR 0 3 "\",\"" 4 tld ""
			6 SQLCHAR 0 3 "\",\"" 5 currency_code ""
			7 SQLCHAR 0 50 "\"\r\n" 6 currency_name Latin1_General_CI_AI
			

Sample Code:

<%
	Dim conn, myDSN, mySQL, rs, mySQL2, rs2

  ' SQL database connection. NOTE: Replace servername, username and password to your own values.
  Set conn = Server.CreateObject("ADODB.Connection")
 myDSN="DRIVER={SQLServer};SERVER=servername;UID=username;PWD=password;DATABASE=ip2location"
 

  conn.open myDSN

  ' retrieve visitor IP address and translate it to IP address number
  IPno = Dot2LongIP(Request.ServerVariables("REMOTE_ADDR"))

  ' SQL query to lookup valid range of IP address
  mySQL = "SELECT TOP 1 * FROM [ip2location].[dbo].[ip_country] WHERE ip_to >=" & IPno & " ORDER BY ip_to"

  Set rs = Server.CreateObject("ADODB.Recordset")
  rs.open mySQL, conn

  ' assign country name for reference
  countryCode = rs("country_code")

' SQL query to lookup currency code
  mySQL2 = "SELECT currency_code FROM country WHERE iso='" & countryCode & "'"

  Set rs2 = Server.CreateObject("ADODB.Recordset")
  rs2.open mySQL2, conn

' assign currency code for reference
  currency_code = rs2("currency_code")

  ' close and free connections
  rs.close
  rs2.close
  conn.close
  set rs = nothing
  set rs2 = nothing  
  Set conn = nothing

  //set the currency rate
  set currencyRate = Server.CreateObject("Scripting.Dictionary")
  currencyRate.Add "USD",1
  currencyRate.Add "JPY",119.91
  US_Price = 20.00
  Response.Write "Price : " & currency_code & " " & FormatNumber(currencyRate.Item(currency_code)* US_Price,2) 
  Response.Write " (Original. Price : USD " & FormatNumber(US_Price,2) & ")
" Function Dot2LongIP (ByVal DottedIP) Dim i, pos Dim PrevPos, num If DottedIP = "" Then Dot2LongIP = 0 Else For i = 1 To 4 pos = InStr(PrevPos + 1, DottedIP, ".", 1) If i = 4 Then pos = Len(DottedIP) + 1 End If num = Int(Mid(DottedIP, PrevPos + 1, pos - PrevPos - 1)) PrevPos = pos Dot2LongIP = ((num Mod 256) * (256 ^ (4 - i))) + Dot2LongIP Next End If End Function %>

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.