Currency Convertor using WebServices
1)Go To Visual Studio 2010 -> New Project -> Select Asp.NET Web Service Application -> OK
2)Design CurrencyConvertor.aspx
We Need 1 Textbox -TxtAmount
2 DropDownList - DdlFromCurrency , DdlToCurrency
1 Button - Button1
1 Label - LblResult
Design Source of CurrencyConvertor.aspx
3)Write Code in CurrencyConvertor.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using System.Xml;
namespace Exchange_Rate_Calculator_WebService
{
public partial class Currency_Convertor : System.Web.UI.Page
{
Service1 CurrenyConvertorService = new Service1();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
double CurrencyAmount;
//Check for a numeric value entered into the Amount textbox
try
{
CurrencyAmount = double.Parse(TxtAmount.Text);
}
catch
{
LblResult.Text="Please enter a Numeric Value!";
return;
}
try
{
string Result = null;
string url;
//Call the web service to get the exchange rate for the two currencies selected by the user
url = "http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate?FromCurrency=" + DdlFromCurrency.SelectedValue + "&ToCurrency=" + DdlToCurrency.SelectedValue + "";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader Stream = new StreamReader(response.GetResponseStream());
XmlDocument doc = new XmlDocument();
Result = Stream.ReadToEnd();
doc.LoadXml(Result);
//Retrieve the conversion rate
string NewConversionRate = doc.GetElementsByTagName("double").Item(0).InnerText;
//Convert the conversion rate string to a double
double ConversionRate = double.Parse(NewConversionRate);
//Calculate the currency exchange
double ConvertedAmount = CurrencyAmount * ConversionRate;
//Display the results on the Default.aspx page
LblResult.Text = TxtAmount.Text + " " + DdlFromCurrency.SelectedItem + " = " + dblConverted + " " + DdlToCurrency.SelectedItem;
}
catch
{
LblResult.Text = "Oops..Web Service Not available. Try again later";
}
}
}
}
4) Enter the Currency Amount you want to convert and check the result.