How to Live Convert Currency Using WebService in Asp.Net

Description:- In one of my projects there is one module that requires a currency converter application. I am applying various techniques to convert the currency, such as storing the country name and related currency values in a database and retrieve them according to the related country but that is a very lengthy and Complicated Process.

Then after trying many options I decided to use a third party API to complete my requirement; I chose the Google API. So my intent is to write this article to help others that might me need to do the same type of task.

So let us start with the work through.
 
Create the project as in:
  1. "Start" - "All Programs" - "Microsoft Visual Studio 2010".
  2. "File" - "New Project" - "C#" - "Empty Project" (to avoid adding a master page).
  3. Give the Project name (such as LiveCurrencyconverter) or another as you wish and specify the location.
  4. Then right-click on Solution Explorer - "Add New Item" - "Default.aspx" page. 
  5. Then right-click on Solution Explorer - "Add New Item" -”WebService.asmx" page. 
  6. To see the "Default.aspx" source code please download the sample zip file.
First Design your Webpage Like below.

<table>
<tr><td align="right">Enter Amount: </td>
<td><asp:TextBox ID="txtAmount" runat="server"></asp:TextBox></td></tr>
<tr><td align="right">From: </td><td>
<asp:DropDownList ID="DDLFrom" runat="server">
<asp:ListItem Value="USD">US Dollar (USD)</asp:ListItem>
<asp:ListItem Value="AED">United Arab Emirates Dirham (AED)</asp:ListItem>
<asp:ListItem Value="CAD">Canadian Dollar (CAD)</asp:ListItem>
<asp:ListItem Value="INR">Indian Rupee (INR)</asp:ListItem>
<asp:ListItem Value="EUR">Euro (EUR)</asp:ListItem>
</asp:DropDownList></td></tr>
<tr><td align="right">to: </td><td>
<asp:DropDownList ID="DDLFrom" runat="server">
<asp:ListItem Value="USD">US Dollar (USD)</asp:ListItem>
<asp:ListItem Value="AED">United Arab Emirates Dirham (AED)</asp:ListItem>
<asp:ListItem Value="CAD">Canadian Dollar (CAD)</asp:ListItem>
<asp:ListItem Value="INR">Indian Rupee (INR)</asp:ListItem>
<asp:ListItem Value="EUR">Euro (EUR)</asp:ListItem>
</asp:DropDownList></td></tr>
<tr><td></td><td>
<asp:Button ID="btnConvert" runat="server" Text="Convert" OnClick="btnConvert_Click" />
</td></tr></table>

Use the following code in WebService.asmx.cs of the WebService.asmx page:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Net;
using System.Text.RegularExpressions;
namespace GirdCRUD
{
    /// <summary>
    /// Summary description for WebService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 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("https://www.igolder.com/exchangerate.ashx?from={2}{0}&to={1}", fromCurrency.ToUpper(), toCurrency.ToUpper(), amount);
            string response = web.DownloadString(url);
            Regex regex = new Regex(@":(?<rhs>.+?),");
            string[] arrDigits = regex.Split(response);
            string rate = arrDigits[0];
            return rate;
        }
    }
}

Now Generate button Click event and code for Call web service.

protected void btnConvert_Click(object sender, EventArgs e)
{
    WebService Client = new WebService();
    Label1.Text = Client.CurrencyConversion(Convert.ToInt32(txtAmount.Text), DDLFrom.SelectedItem.Value, DDLTo.SelectedItem.Value);
}

In the preceding web services I have used one string named URL; on it I have saved a path of the Google API. Now add the service reference of the WebService.asmx web service to the Default.aspx page by right-clicking on the Solution Explorer, now run the application which looks such as in the following:
In the preceding Screen we can clearly see that, I have converted 1 USD into Indian Rupees and the output is exactly the current market price, which is 67.084Rs.
I hope this article is useful for all readers, if you have any suggestion then please contact me, because of your suggestion others can get help from this article by improving it.

Related Posts

Previous
Next Post »

Thanks for comments.....