Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Asp.net Google Currency Converter JSON API Example in jQuery

Jan 5, 2013
Introduction

Here I will explain how to create
asp.net Google currency converter using json api in jQuery.


Actually Google has implemented currency conversion tool by using that we can get latest currency details and Google has provided json api also by using that we can implement this functionality in our website.

To implement this functionality we need to write the code like as shown below


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jQuery Google Currency Conversion in Asp.net</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript">
$(function() {
$('#btnConvert').click(function() {
var amount = $('#txtAmount').val();
var from = $('#ddlfrom').val();
var to = $('#ddlto').val();
$.ajax({ type: "POST",
url: "WebService.asmx/CurrencyConversion",
data: "{amount:" + amount + ",fromCurrency:'" + from + "',toCurrency:'" + to + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
$('#currency_converter_result').html(data.d);
}
});
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr><td align="right">Enter Amount:</td><td>  <input id="txtAmount" maxlength="12" size="5" value="1" /></td></tr>
<tr><td align="right">From:</td><td>
<select id="ddlfrom">
<option value="AED">United Arab Emirates Dirham (AED)</option>
<option value="ANG">Netherlands Antillean Guilder (ANG)</option>
<option value="ARS">Argentine Peso (ARS)</option>
<option value="AUD">Australian Dollar (AUD)</option>
<option value="BDT">Bangladeshi Taka (BDT)</option>
<option value="BGN">Bulgarian Lev (BGN)</option>
<option value="BHD">Bahraini Dinar (BHD)</option>
<option value="BND">Brunei Dollar (BND)</option>
<option value="BOB">Bolivian Boliviano (BOB)</option>
<option value="BRL">Brazilian Real (BRL)</option>
<option value="BWP">Botswanan Pula (BWP)</option>
<option value="CAD">Canadian Dollar (CAD)</option>
<option value="CHF">Swiss Franc (CHF)</option>
<option value="CLP">Chilean Peso (CLP)</option>
<option value="CNY">Chinese Yuan (CNY)</option>
<option value="COP">Colombian Peso (COP)</option>
<option value="CRC">Costa Rican Colón (CRC)</option>
<option value="CZK">Czech Republic Koruna (CZK)</option>
<option value="DKK">Danish Krone (DKK)</option>
<option value="DOP">Dominican Peso (DOP)</option>
<option value="DZD">Algerian Dinar (DZD)</option>
<option value="EEK">Estonian Kroon (EEK)</option>
<option value="EGP">Egyptian Pound (EGP)</option>
<option value="EUR">Euro (EUR)</option>
<option value="FJD">Fijian Dollar (FJD)</option>
<option value="GBP">British Pound Sterling (GBP)</option>
<option value="HKD">Hong Kong Dollar (HKD)</option>
<option value="HNL">Honduran Lempira (HNL)</option>
<option value="HRK">Croatian Kuna (HRK)</option>
<option value="HUF">Hungarian Forint (HUF)</option>
<option value="IDR">Indonesian Rupiah (IDR)</option>
<option value="ILS">Israeli New Sheqel (ILS)</option>
<option value="INR">Indian Rupee (INR)</option>
<option value="JMD">Jamaican Dollar (JMD)</option>
<option value="JOD">Jordanian Dinar (JOD)</option>
<option value="JPY">Japanese Yen (JPY)</option>
<option value="KES">Kenyan Shilling (KES)</option>
<option value="KRW">South Korean Won (KRW)</option>
<option value="KWD">Kuwaiti Dinar (KWD)</option>
<option value="KYD">Cayman Islands Dollar (KYD)</option>
<option value="KZT">Kazakhstani Tenge (KZT)</option>
<option value="LBP">Lebanese Pound (LBP)</option>
<option value="LKR">Sri Lankan Rupee (LKR)</option>
<option value="LTL">Lithuanian Litas (LTL)</option>
<option value="LVL">Latvian Lats (LVL)</option>
<option value="MAD">Moroccan Dirham (MAD)</option>
<option value="MDL">Moldovan Leu (MDL)</option>
<option value="MKD">Macedonian Denar (MKD)</option>
<option value="MUR">Mauritian Rupee (MUR)</option>
<option value="MVR">Maldivian Rufiyaa (MVR)</option>
<option value="MXN">Mexican Peso (MXN)</option>
<option value="MYR">Malaysian Ringgit (MYR)</option>
<option value="NAD">Namibian Dollar (NAD)</option>
<option value="NGN">Nigerian Naira (NGN)</option>
<option value="NIO">Nicaraguan Córdoba (NIO)</option>
<option value="NOK">Norwegian Krone (NOK)</option>
<option value="NPR">Nepalese Rupee (NPR)</option>
<option value="NZD">New Zealand Dollar (NZD)</option>
<option value="OMR">Omani Rial (OMR)</option>
<option value="PEN">Peruvian Nuevo Sol (PEN)</option>
<option value="PGK">Papua New Guinean Kina (PGK)</option>
<option value="PHP">Philippine Peso (PHP)</option>
<option value="PKR">Pakistani Rupee (PKR)</option>
<option value="PLN">Polish Zloty (PLN)</option>
<option value="PYG">Paraguayan Guarani (PYG)</option>
<option value="QAR">Qatari Rial (QAR)</option>
<option value="RON">Romanian Leu (RON)</option>
<option value="RSD">Serbian Dinar (RSD)</option>
<option value="RUB">Russian Ruble (RUB)</option>
<option value="SAR">Saudi Riyal (SAR)</option>
<option value="SCR">Seychellois Rupee (SCR)</option>
<option value="SEK">Swedish Krona (SEK)</option>
<option value="SGD">Singapore Dollar (SGD)</option>
<option value="SKK">Slovak Koruna (SKK)</option>
<option value="SLL">Sierra Leonean Leone (SLL)</option>
<option value="SVC">Salvadoran Colón (SVC)</option>
<option value="THB">Thai Baht (THB)</option>
<option value="TND">Tunisian Dinar (TND)</option>
<option value="TRY">Turkish Lira (TRY)</option>
<option value="TTD">Trinidad and Tobago Dollar (TTD)</option>
<option value="TWD">New Taiwan Dollar (TWD)</option>
<option value="TZS">Tanzanian Shilling (TZS)</option>
<option value="UAH">Ukrainian Hryvnia (UAH)</option>
<option value="UGX">Ugandan Shilling (UGX)</option>
<option value="USD">US Dollar (USD)</option>
<option value="UYU">Uruguayan Peso (UYU)</option>
<option value="UZS">Uzbekistan Som (UZS)</option>
<option value="VEF">Venezuelan Bolívar (VEF)</option>
<option value="VND">Vietnamese Dong (VND)</option>
<option value="XOF">CFA Franc BCEAO (XOF)</option>
<option value="YER">Yemeni Rial (YER)</option>
<option value="ZAR">South African Rand (ZAR)</option>
<option value="ZMK">Zambian Kwacha (ZMK)</option>
</select></td>
</tr>
<tr> <td align="right">to:</td><td>
<select id="ddlto">
<option value="AED">United Arab Emirates Dirham (AED)</option>
<option value="ANG">Netherlands Antillean Guilder (ANG)</option>
<option value="ARS">Argentine Peso (ARS)</option>
<option value="AUD">Australian Dollar (AUD)</option>
<option value="BDT">Bangladeshi Taka (BDT)</option>
<option value="BGN">Bulgarian Lev (BGN)</option>
<option value="BHD">Bahraini Dinar (BHD)</option>
<option value="BND">Brunei Dollar (BND)</option>
<option value="BOB">Bolivian Boliviano (BOB)</option>
<option value="BRL">Brazilian Real (BRL)</option>
<option value="BWP">Botswanan Pula (BWP)</option>
<option value="CAD">Canadian Dollar (CAD)</option>
<option value="CHF">Swiss Franc (CHF)</option>
<option value="CLP">Chilean Peso (CLP)</option>
<option value="CNY">Chinese Yuan (CNY)</option>
<option value="COP">Colombian Peso (COP)</option>
<option value="CRC">Costa Rican Colón (CRC)</option>
<option value="CZK">Czech Republic Koruna (CZK)</option>
<option value="DKK">Danish Krone (DKK)</option>
<option value="DOP">Dominican Peso (DOP)</option>
<option value="DZD">Algerian Dinar (DZD)</option>
<option value="EEK">Estonian Kroon (EEK)</option>
<option value="EGP">Egyptian Pound (EGP)</option>
<option value="EUR">Euro (EUR)</option>
<option value="FJD">Fijian Dollar (FJD)</option>
<option value="GBP">British Pound Sterling (GBP)</option>
<option value="HKD">Hong Kong Dollar (HKD)</option>
<option value="HNL">Honduran Lempira (HNL)</option>
<option value="HRK">Croatian Kuna (HRK)</option>
<option value="HUF">Hungarian Forint (HUF)</option>
<option value="IDR">Indonesian Rupiah (IDR)</option>
<option value="ILS">Israeli New Sheqel (ILS)</option>
<option value="INR">Indian Rupee (INR)</option>
<option value="JMD">Jamaican Dollar (JMD)</option>
<option value="JOD">Jordanian Dinar (JOD)</option>
<option value="JPY">Japanese Yen (JPY)</option>
<option value="KES">Kenyan Shilling (KES)</option>
<option value="KRW">South Korean Won (KRW)</option>
<option value="KWD">Kuwaiti Dinar (KWD)</option>
<option value="KYD">Cayman Islands Dollar (KYD)</option>
<option value="KZT">Kazakhstani Tenge (KZT)</option>
<option value="LBP">Lebanese Pound (LBP)</option>
<option value="LKR">Sri Lankan Rupee (LKR)</option>
<option value="LTL">Lithuanian Litas (LTL)</option>
<option value="LVL">Latvian Lats (LVL)</option>
<option value="MAD">Moroccan Dirham (MAD)</option>
<option value="MDL">Moldovan Leu (MDL)</option>
<option value="MKD">Macedonian Denar (MKD)</option>
<option value="MUR">Mauritian Rupee (MUR)</option>
<option value="MVR">Maldivian Rufiyaa (MVR)</option>
<option value="MXN">Mexican Peso (MXN)</option>
<option value="MYR">Malaysian Ringgit (MYR)</option>
<option value="NAD">Namibian Dollar (NAD)</option>
<option value="NGN">Nigerian Naira (NGN)</option>
<option value="NIO">Nicaraguan Córdoba (NIO)</option>
<option value="NOK">Norwegian Krone (NOK)</option>
<option value="NPR">Nepalese Rupee (NPR)</option>
<option value="NZD">New Zealand Dollar (NZD)</option>
<option value="OMR">Omani Rial (OMR)</option>
<option value="PEN">Peruvian Nuevo Sol (PEN)</option>
<option value="PGK">Papua New Guinean Kina (PGK)</option>
<option value="PHP">Philippine Peso (PHP)</option>
<option value="PKR">Pakistani Rupee (PKR)</option>
<option value="PLN">Polish Zloty (PLN)</option>
<option value="PYG">Paraguayan Guarani (PYG)</option>
<option value="QAR">Qatari Rial (QAR)</option>
<option value="RON">Romanian Leu (RON)</option>
<option value="RSD">Serbian Dinar (RSD)</option>
<option value="RUB">Russian Ruble (RUB)</option>
<option value="SAR">Saudi Riyal (SAR)</option>
<option value="SCR">Seychellois Rupee (SCR)</option>
<option value="SEK">Swedish Krona (SEK)</option>
<option value="SGD">Singapore Dollar (SGD)</option>
<option value="SKK">Slovak Koruna (SKK)</option>
<option value="SLL">Sierra Leonean Leone (SLL)</option>
<option value="SVC">Salvadoran Colón (SVC)</option>
<option value="THB">Thai Baht (THB)</option>
<option value="TND">Tunisian Dinar (TND)</option>
<option value="TRY">Turkish Lira (TRY)</option>
<option value="TTD">Trinidad and Tobago Dollar (TTD)</option>
<option value="TWD">New Taiwan Dollar (TWD)</option>
<option value="TZS">Tanzanian Shilling (TZS)</option>
<option value="UAH">Ukrainian Hryvnia (UAH)</option>
<option value="UGX">Ugandan Shilling (UGX)</option>
<option value="USD">US Dollar (USD)</option>
<option value="UYU">Uruguayan Peso (UYU)</option>
<option value="UZS">Uzbekistan Som (UZS)</option>
<option value="VEF">Venezuelan Bolívar (VEF)</option>
<option value="VND">Vietnamese Dong (VND)</option>
<option value="XOF">CFA Franc BCEAO (XOF)</option>
<option value="YER">Yemeni Rial (YER)</option>
<option value="ZAR">South African Rand (ZAR)</option>
<option value="ZMK">Zambian Kwacha (ZMK)</option>
</select></td></tr>
<tr><td></td><td> <input id="btnConvert" type="button" value="Convert" /></td></tr>
</table>
<div style="overflow:hidden; padding:10px; width:500px; margin:10px; background:#EAEAFF; border:solid 1px #ccc;">
<div id="currency_converter_result" style="padding: 2px; margin: 5px; font-weight:bold; font-size:14pt">
</div>
</div>
</form>
</body>
</html>
If you observe above code in header section I mentioned url field as “WebService.asmx/CurrencyConversion” this mean we are calling CurrencyConversion method from WebService.asmx webservice.

To creat this webservice right click on your application >> Select Add New Item >> select Web Service >> click OK
Once webservice created open code behind file and write the following code

C# Code


using System.Net;
using System.Text.RegularExpressions;
using System.Web.Services;

/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {
[WebMethod]
public string CurrencyConversion(decimal amount, string fromCurrency, string toCurrency)
{
WebClient web = new WebClient();
string url = string.Format("http://www.google.com/ig/calculator?hl=en&q={2}{0}%3D%3F{1}", fromCurrency.ToUpper(), toCurrency.ToUpper(), amount);
string response = web.DownloadString(url);
Regex regex = new Regex(@":(?<rhs>.+?),");
string[] arrDigits = regex.Split(response);
string rate = arrDigits[3];
return rate;
}
}
VB.NET Code:


Imports System.Net
Imports System.Text.RegularExpressions
Imports System.Web.Services

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
<System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
Public Class WebService2
Inherits System.Web.Services.WebService
<WebMethod()> _
Public Function CurrencyConversion(ByVal amount As Decimal, ByVal fromCurrency As String, ByVal toCurrency As String) As String
Dim web As New WebClient()
Dim url As String = String.Format("http://www.google.com/ig/calculator?hl=en&q={2}{0}%3D%3F{1}", fromCurrency.ToUpper(), toCurrency.ToUpper(), amount)
Dim response As String = web.DownloadString(url)
Dim regex As New Regex(":(?<rhs>.+?),")
Dim arrDigits As String() = regex.Split(response)
Dim rate As String = arrDigits(3)
Return rate
End Function
End Class
Demo


Download sample code attached


If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

11 comments :

Narasimhareddy Venna said...

Hi Sir I am narsimha reddy good example for webservices, Thank u very much

Anonymous said...

thanks a lot for this post

Maruthi Ram said...

Hi sir,I am not getting this.I have implemented all the steps but i am not getting any error also.can u help me please??

Naveen said...

Nice Post using Webservice

vabhi said...

the result is not proper so.. help me out

Anonymous said...

Im getting encrypted result

Shankar said...

Im getting encrypted result like RIrjUsiaIIGTrgfM3YH4AQ

HeXtoR said...

i got this as answer "WoQEU-OQFue0iQer-IDgBg"

Seraj Khan said...

hello sir i am getting encrypted result

Nauna IMY said...

I have download the code but it is giving result.

Abdul Samad said...
This comment has been removed by a blog administrator.

Give your Valuable Comments

Other Related Posts

© 2010-2012 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.