Ep

Ep

Google Currency Converter API C# - ASP.Net

How to use Google currency converter API in asp.net

In this article I would like to explain how to use Google finance currency converter Api real-time in asp.net web application. The following example demonstrates how to implement currency conversion rates using Google API in C#.
google currency converter api c#, yahoo currency converter api, google finance currency converter api, google currency converter api json,google currency converter api javascript
Google currency converter api example

Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>How to use Real Time Currency Converter API in ASP .Net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div style="font-family: Trebuchet MS">
        Enter a amount:
        <asp:TextBox ID="txtAmount" runat="server" Text="1"></asp:TextBox>
        <asp:DropDownList ID="ddlFrom" runat="server">
            <asp:ListItem Value="USD" Selected="true" Text="US Dollars $"></asp:ListItem>
            <asp:ListItem Value="EUR" Text="Euro €"></asp:ListItem>
            <asp:ListItem Value="GBP" Text="United Kingdom Pounds £"></asp:ListItem>
            <asp:ListItem Value="JPY" Text="Japan Yen ¥"></asp:ListItem>
            <asp:ListItem Value="AFN" Text="Afghanistan Afghanis ؋"></asp:ListItem>
            <asp:ListItem Value="ALL" Text="Albania Leke Lek"></asp:ListItem>
            <asp:ListItem Value="DZD" Text="Algeria Dinars DA"></asp:ListItem>
            <asp:ListItem Value="ARS" Text="Argentina Pesos $"></asp:ListItem>
            <asp:ListItem Value="AUD" Text="Australia Dollars $"></asp:ListItem>
            <asp:ListItem Value="BSD" Text="Bahamas Dollars $"></asp:ListItem>
            <asp:ListItem Value="BHD" Text="Bahrain Dinars BD"></asp:ListItem>
            <asp:ListItem Value="BDT" Text="Bangladesh Taka Tk"></asp:ListItem>
            <asp:ListItem Value="BBD" Text="Barbados Dollars $"></asp:ListItem>
            <asp:ListItem Value="BMD" Text="Bermuda Dollars $"></asp:ListItem>
            <asp:ListItem Value="BRL" Text="Brazil Reais R$"></asp:ListItem>
            <asp:ListItem Value="BGN" Text="Bulgaria Leva лв"></asp:ListItem>
            <asp:ListItem Value="CAD" Text="Canada Dollars $"></asp:ListItem>
            <asp:ListItem Value="XOF" Text="CFA Francs BCEAO CFAF"></asp:ListItem>
            <asp:ListItem Value="XAF" Text="CFA Francs BEAC CFAF"></asp:ListItem>
            <asp:ListItem Value="XPF" Text="CFP Francs "></asp:ListItem>
            <asp:ListItem Value="CLP" Text="Chile Pesos $"></asp:ListItem>
            <asp:ListItem Value="CNY" Text="China Yuan Renminbi "></asp:ListItem>
            <asp:ListItem Value="COP" Text="Colombia Pesos $"></asp:ListItem>
            <asp:ListItem Value="CRC" Text="Costa Rica Colones "></asp:ListItem>
            <asp:ListItem Value="HRK" Text="Croatia Kuna kn"></asp:ListItem>
            <asp:ListItem Value="CYP" Text="Cyprus Pounds £"></asp:ListItem>
            <asp:ListItem Value="CZK" Text="Czech Republic Koruny Kč"></asp:ListItem>
            <asp:ListItem Value="DKK" Text="Denmark Kroner kr"></asp:ListItem>
            <asp:ListItem Value="DOP" Text="Dominican Republic Pesos RD$"></asp:ListItem>
            <asp:ListItem Value="XCD" Text="East Caribbean Dollars $"></asp:ListItem>
            <asp:ListItem Value="EGP" Text="Egypt Pounds £"></asp:ListItem>
            <asp:ListItem Value="EEK" Text="Estonia Krooni kr"></asp:ListItem>
            <asp:ListItem Value="FJD" Text="Fiji Dollars $"></asp:ListItem>
            <asp:ListItem Value="HKD" Text="Hong Kong Dollars "></asp:ListItem>
            <asp:ListItem Value="HUF" Text="Hungary Forint Ft"></asp:ListItem>
            <asp:ListItem Value="ISK" Text="Iceland Kronur kr"></asp:ListItem>
            <asp:ListItem Value="XDR" Text="IMF Special Drawing Rights "></asp:ListItem>
            <asp:ListItem Value="INR" Text="India Rupees "></asp:ListItem>
            <asp:ListItem Value="IDR" Text="Indonesia Rupiahs Rp"></asp:ListItem>
            <asp:ListItem Value="IRR" Text="Iran Rials "></asp:ListItem>
            <asp:ListItem Value="IQD" Text="Iraq Dinars ID"></asp:ListItem>
            <asp:ListItem Value="ILS" Text="Israel New Shekels "></asp:ListItem>
            <asp:ListItem Value="JMD" Text="Jamaica Dollars J$"></asp:ListItem>
            <asp:ListItem Value="JOD" Text="Jordan Dinars JD"></asp:ListItem>
            <asp:ListItem Value="KZT" Text="Kazakhstan Tenge лв"></asp:ListItem>
            <asp:ListItem Value="KES" Text="Kenya Shillings K Sh"></asp:ListItem>
            <asp:ListItem Value="KWD" Text="Kuwait Dinars KD"></asp:ListItem>
            <asp:ListItem Value="LBP" Text="Lebanon Pounds £"></asp:ListItem>
            <asp:ListItem Value="MYR" Text="Malaysia Ringgits RM"></asp:ListItem>
            <asp:ListItem Value="MTL" Text="Malta Liri Lm"></asp:ListItem>
            <asp:ListItem Value="MUR" Text="Mauritius Rupees "></asp:ListItem>
            <asp:ListItem Value="MXN" Text="Mexico Pesos $"></asp:ListItem>
            <asp:ListItem Value="MAD" Text="Morocco Dirhams DH"></asp:ListItem>
            <asp:ListItem Value="NZD" Text="New Zealand Dollars $"></asp:ListItem>
            <asp:ListItem Value="NGN" Text="Nigeria Nairas "></asp:ListItem>
            <asp:ListItem Value="NOK" Text="Norway Kroner kr"></asp:ListItem>
            <asp:ListItem Value="OMR" Text="Oman Rials "></asp:ListItem>
            <asp:ListItem Value="PKR" Text="Pakistan Rupees "></asp:ListItem>
            <asp:ListItem Value="XPD" Text="Palladium Ounces "></asp:ListItem>
            <asp:ListItem Value="PEN" Text="Peru Nuevos Soles S/."></asp:ListItem>
            <asp:ListItem Value="PHP" Text="Philippines Pesos Php"></asp:ListItem>
            <asp:ListItem Value="PLN" Text="Poland Zlotych zł"></asp:ListItem>
            <asp:ListItem Value="QAR" Text="Qatar Riyals "></asp:ListItem>
            <asp:ListItem Value="RON" Text="Romania New Lei lei"></asp:ListItem>
            <asp:ListItem Value="RUB" Text="Russia Rubles руб"></asp:ListItem>
            <asp:ListItem Value="SAR" Text="Saudi Arabia Riyals "></asp:ListItem>
            <asp:ListItem Value="XAG" Text="Silver Ounces "></asp:ListItem>
            <asp:ListItem Value="SGD" Text="Singapore Dollars $"></asp:ListItem>
            <asp:ListItem Value="ZAR" Text="South Africa Rand R"></asp:ListItem>
            <asp:ListItem Value="KRW" Text="South Korea Won "></asp:ListItem>
            <asp:ListItem Value="LKR" Text="Sri Lanka Rupees "></asp:ListItem>
            <asp:ListItem Value="SDG" Text="Sudan Pounds "></asp:ListItem>
            <asp:ListItem Value="SEK" Text="Sweden Kronor kr"></asp:ListItem>
            <asp:ListItem Value="CHF" Text="Switzerland Francs CHF"></asp:ListItem>
            <asp:ListItem Value="TWD" Text="Taiwan New Dollars NT$"></asp:ListItem>
            <asp:ListItem Value="THB" Text="Thailand Baht ฿"></asp:ListItem>
            <asp:ListItem Value="TTD" Text="Trinidad and Tobago Dollars TT$"></asp:ListItem>
            <asp:ListItem Value="TND" Text="Tunisia Dinars TD"></asp:ListItem>
            <asp:ListItem Value="TRY" Text="Turkey Lira YTL"></asp:ListItem>
            <asp:ListItem Value="AED" Text="United Arab Emirates Dirhams Dh"></asp:ListItem>
            <asp:ListItem Value="VEF" Text="Venezuela Bolivares Fuertes "></asp:ListItem>
            <asp:ListItem Value="VND" Text="Vietnam Dong "></asp:ListItem>
            <asp:ListItem Value="ZMK" Text="Zambia Kwacha ZK"></asp:ListItem>
        </asp:DropDownList>
        &nbsp;&nbsp; To&nbsp;&nbsp;
        <asp:DropDownList ID="ddlTo" runat="server">
            <asp:ListItem Value="USD" Text="United States Dollars $"></asp:ListItem>
            <asp:ListItem Value="EUR" Text="Euro €"></asp:ListItem>
            <asp:ListItem Value="GBP" Text="United Kingdom Pounds £"></asp:ListItem>
            <asp:ListItem Value="JPY" Text="Japan Yen ¥"></asp:ListItem>
            <asp:ListItem Value="AFN" Text="Afghanistan Afghanis ؋"></asp:ListItem>
            <asp:ListItem Value="ALL" Text="Albania Leke Lek"></asp:ListItem>
            <asp:ListItem Value="DZD" Text="Algeria Dinars DA"></asp:ListItem>
            <asp:ListItem Value="ARS" Text="Argentina Pesos $"></asp:ListItem>
            <asp:ListItem Value="AUD" Text="Australia Dollars $"></asp:ListItem>
            <asp:ListItem Value="BSD" Text="Bahamas Dollars $"></asp:ListItem>
            <asp:ListItem Value="BHD" Text="Bahrain Dinars BD"></asp:ListItem>
            <asp:ListItem Value="BDT" Text="Bangladesh Taka Tk"></asp:ListItem>
            <asp:ListItem Value="BBD" Text="Barbados Dollars $"></asp:ListItem>
            <asp:ListItem Value="BMD" Text="Bermuda Dollars $"></asp:ListItem>
            <asp:ListItem Value="BRL" Text="Brazil Reais R$"></asp:ListItem>
            <asp:ListItem Value="BGN" Text="Bulgaria Leva лв"></asp:ListItem>
            <asp:ListItem Value="CAD" Text="Canada Dollars $"></asp:ListItem>
            <asp:ListItem Value="XOF" Text="CFA Francs BCEAO CFAF"></asp:ListItem>
            <asp:ListItem Value="XAF" Text="CFA Francs BEAC CFAF"></asp:ListItem>
            <asp:ListItem Value="XPF" Text="CFP Francs "></asp:ListItem>
            <asp:ListItem Value="CLP" Text="Chile Pesos $"></asp:ListItem>
            <asp:ListItem Value="CNY" Text="China Yuan Renminbi "></asp:ListItem>
            <asp:ListItem Value="COP" Text="Colombia Pesos $"></asp:ListItem>
            <asp:ListItem Value="CRC" Text="Costa Rica Colones "></asp:ListItem>
            <asp:ListItem Value="HRK" Text="Croatia Kuna kn"></asp:ListItem>
            <asp:ListItem Value="CYP" Text="Cyprus Pounds £"></asp:ListItem>
            <asp:ListItem Value="CZK" Text="Czech Republic Koruny Kč"></asp:ListItem>
            <asp:ListItem Value="DKK" Text="Denmark Kroner kr"></asp:ListItem>
            <asp:ListItem Value="DOP" Text="Dominican Republic Pesos RD$"></asp:ListItem>
            <asp:ListItem Value="XCD" Text="East Caribbean Dollars $"></asp:ListItem>
            <asp:ListItem Value="EGP" Text="Egypt Pounds £"></asp:ListItem>
            <asp:ListItem Value="EEK" Text="Estonia Krooni kr"></asp:ListItem>
            <asp:ListItem Value="FJD" Text="Fiji Dollars $"></asp:ListItem>
            <asp:ListItem Value="HKD" Text="Hong Kong Dollars "></asp:ListItem>
            <asp:ListItem Value="HUF" Text="Hungary Forint Ft"></asp:ListItem>
            <asp:ListItem Value="ISK" Text="Iceland Kronur kr"></asp:ListItem>
            <asp:ListItem Value="XDR" Text="IMF Special Drawing Rights "></asp:ListItem>
            <asp:ListItem Value="INR" Selected="True" Text="India Rupees "></asp:ListItem>
            <asp:ListItem Value="IDR" Text="Indonesia Rupiahs Rp"></asp:ListItem>
            <asp:ListItem Value="IRR" Text="Iran Rials "></asp:ListItem>
            <asp:ListItem Value="IQD" Text="Iraq Dinars ID"></asp:ListItem>
            <asp:ListItem Value="ILS" Text="Israel New Shekels "></asp:ListItem>
            <asp:ListItem Value="JMD" Text="Jamaica Dollars J$"></asp:ListItem>
            <asp:ListItem Value="JOD" Text="Jordan Dinars JD"></asp:ListItem>
            <asp:ListItem Value="KZT" Text="Kazakhstan Tenge лв"></asp:ListItem>
            <asp:ListItem Value="KES" Text="Kenya Shillings K Sh"></asp:ListItem>
            <asp:ListItem Value="KWD" Text="Kuwait Dinars KD"></asp:ListItem>
            <asp:ListItem Value="LBP" Text="Lebanon Pounds £"></asp:ListItem>
            <asp:ListItem Value="MYR" Text="Malaysia Ringgits RM"></asp:ListItem>
            <asp:ListItem Value="MTL" Text="Malta Liri Lm"></asp:ListItem>
            <asp:ListItem Value="MUR" Text="Mauritius Rupees "></asp:ListItem>
            <asp:ListItem Value="MXN" Text="Mexico Pesos $"></asp:ListItem>
            <asp:ListItem Value="MAD" Text="Morocco Dirhams DH"></asp:ListItem>
            <asp:ListItem Value="NZD" Text="New Zealand Dollars $"></asp:ListItem>
            <asp:ListItem Value="NGN" Text="Nigeria Nairas "></asp:ListItem>
            <asp:ListItem Value="NOK" Text="Norway Kroner kr"></asp:ListItem>
            <asp:ListItem Value="OMR" Text="Oman Rials "></asp:ListItem>
            <asp:ListItem Value="PKR" Text="Pakistan Rupees "></asp:ListItem>
            <asp:ListItem Value="XPD" Text="Palladium Ounces "></asp:ListItem>
            <asp:ListItem Value="PEN" Text="Peru Nuevos Soles S/."></asp:ListItem>
            <asp:ListItem Value="PHP" Text="Philippines Pesos Php"></asp:ListItem>
            <asp:ListItem Value="PLN" Text="Poland Zlotych zł"></asp:ListItem>
            <asp:ListItem Value="QAR" Text="Qatar Riyals "></asp:ListItem>
            <asp:ListItem Value="RON" Text="Romania New Lei lei"></asp:ListItem>
            <asp:ListItem Value="RUB" Text="Russia Rubles руб"></asp:ListItem>
            <asp:ListItem Value="SAR" Text="Saudi Arabia Riyals "></asp:ListItem>
            <asp:ListItem Value="XAG" Text="Silver Ounces "></asp:ListItem>
            <asp:ListItem Value="SGD" Text="Singapore Dollars $"></asp:ListItem>
            <asp:ListItem Value="ZAR" Text="South Africa Rand R"></asp:ListItem>
            <asp:ListItem Value="KRW" Text="South Korea Won "></asp:ListItem>
            <asp:ListItem Value="LKR" Text="Sri Lanka Rupees "></asp:ListItem>
            <asp:ListItem Value="SDG" Text="Sudan Pounds "></asp:ListItem>
            <asp:ListItem Value="SEK" Text="Sweden Kronor kr"></asp:ListItem>
            <asp:ListItem Value="CHF" Text="Switzerland Francs CHF"></asp:ListItem>
            <asp:ListItem Value="TWD" Text="Taiwan New Dollars NT$"></asp:ListItem>
            <asp:ListItem Value="THB" Text="Thailand Baht ฿"></asp:ListItem>
            <asp:ListItem Value="TTD" Text="Trinidad and Tobago Dollars TT$"></asp:ListItem>
            <asp:ListItem Value="TND" Text="Tunisia Dinars TD"></asp:ListItem>
            <asp:ListItem Value="TRY" Text="Turkey Lira YTL"></asp:ListItem>
            <asp:ListItem Value="AED" Text="United Arab Emirates Dirhams Dh"></asp:ListItem>
            <asp:ListItem Value="VEF" Text="Venezuela Bolivares Fuertes "></asp:ListItem>
            <asp:ListItem Value="VND" Text="Vietnam Dong "></asp:ListItem>
            <asp:ListItem Value="ZMK" Text="Zambia Kwacha ZK"></asp:ListItem>
        </asp:DropDownList>
        &nbsp;&nbsp;<br />
        <br />
        <asp:Button ID="btnRateExchange" runat="server" Text="ConvertUsingRate-Exchange"
            OnClick="btnRateExchange_Click" />
        &nbsp;&nbsp;
        <asp:Button ID="btnGoogleApi" runat="server" Text="ConvertUsingGoogle-API" OnClick="btnGoogleApi_Click" />
        <br />
        <br />
        <asp:Label ID="lblMessage" runat="server" Text="" Style="font-weight: 700; color: #009999"></asp:Label>
    </div>
    </form>
</body>
</html>
 
Default.aspx.cs
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Web.Script.Serialization;
using System.Net;
using System.Text.RegularExpressions;
using System.Globalization;

public partial class _Default : System.Web.UI.Page
{

    // Calling live service from Google Currency Calculator API
    protected void btnGoogleApi_Click(object sender, EventArgs e)
    {
        decimal amount = 0; string fromCurrency; string toCurrency;
        amount = decimal.Parse(txtAmount.Text.Trim(), System.Globalization.NumberStyles.Any, CultureInfo.InvariantCulture);
        fromCurrency = ddlFrom.SelectedItem.Value;
        toCurrency = ddlTo.SelectedItem.Value;
        if (amount != 0)
        {
            WebClient web = new WebClient();
            string url = string.Format("http://www.google.co.in/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: \\\"(\\d*.\\d*)");
            decimal rate = System.Convert.ToDecimal(regex.Match(response).Groups[1].Value);
            lblMessage.Text = "Real-Time Rate: 1 " + ddlFrom.SelectedItem.Value + " = " + rate + " " + ddlTo.SelectedItem.Value;
        }
    }

    // Calling live service from rate-exchange.appshot.com
    protected void btnRateExchange_Click(object sender, EventArgs e)
    {
        double amount = 0;
        if (double.TryParse(txtAmount.Text.Trim(), out amount))
        {
            string url = string.Format("http://rate-exchange.appspot.com/currency?from={0}&to={1}", ddlFrom.SelectedItem.Value, ddlTo.SelectedItem.Value);
            WebClient client = new WebClient();
            string rates = client.DownloadString(url);
            Rate rate = new JavaScriptSerializer().Deserialize<Rate>(rates);
            double converted_amount = amount * rate.rate;
            string message = string.Empty;
            message += "Real-Time Rate: 1 " + ddlFrom.SelectedItem.Value + " = " + converted_amount + " " + ddlTo.SelectedItem.Value;
            lblMessage.Text = message;
        }
        else
        {
            lblMessage.Text = "Invalid amount value.";
        }

    }
}
public class Rate
{
    public string to { get; set; }
    public string from { get; set; }
    public double rate { get; set; }

Download Code
I hope you will enjoy the development tip while programming with Google API C#. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome. 

Also If you like this article, don't forget to share this article with your friends and colleagues.
Google Currency Converter API C# - ASP.Net Reviewed by Ravi Kumar on 12:33:00 PM Rating: 5

2 comments:

  1. When I tried this code I am getting an error like" The remote server returned an error: (503) Server Unavailable".What was the problem?

    ReplyDelete
    Replies
    1. Actually Google blocks automated requests after 60 to 70 successful requests. There are several things you could try, to avoid google bot detection, which blocks you:

      - Sleep in between the requests for at least a second.
      - Vary your user agent string (just a guess)
      - Vary your IP address (-> use proxy servers to route your requests)

      Delete

All Rights Reserved by Etechpulse © 2012 - 2017
Powered By Blogger

Contact Form

Name

Email *

Message *

Powered by Blogger.