Tuesday, November 15, 2011

Get a Conversion Rate From One Currency to Another Currency

Here we are developing a web application that is used for getting a conversion rate for converting from one currency to another currency. For developing this application we need a web service to find the current conversion rate of currencies. The following are the steps to develop this:

1. The following web service add with web application by click on Add Web Reference:

http://www.webservicex.net/CurrencyConvertor.asmx?WSDL

Paste this url in the URL: block and click on Go.

CurrConver1.gif

Figure 1. Web service Reference add
Here we get the method:

ConversionRate ( FromCurrency As Currency , ToCurrency As Currency ) As double

2. Create a web form that is:
<table>  <tr>  <td>Valuetd>  <td> <asp:TextBox ID="txtvalue" runat="server">asp:TextBox> td> 
  <td>Fromtd>  <td><asp:DropDownList ID="ddfromcurrency" runat="server">asp:DropDownList>td> 
  <td>Totd>  <td><asp:DropDownList ID="ddtocurrency" runat="server">asp:DropDownList>td>    <td><asp:Button ID="btnconvert" runat="server" Text="Convert"
            onclick="btnconvert_Click"/>td>  tr>  table>   <asp:Label ID="lblcurrency" runat="server" Visible="false">asp:Label>

3. Now on .aspx.cs we fill DropDownList by web service on Page Load.
protected void Page_Load(object sender, EventArgs e)
 {
  if (!Page.IsPostBack)
     {
  // Fill  from currency dropdown by web service ddfromcurrency.DataSource = Enum.GetValues(typeof(net.webservicex.www.Currency));/*net.webservice          * x.www.Currency is an Enum type in WSDL*/ddfromcurrency.DataBind();

                // Fill To currency dropdown by web service ddtocurrency.DataSource = Enum.GetValues(typeof(net.webservicex.www.Currency));
 ddtocurrency.DataBind();
            }
4. Now for the Convert button's Click Event use for conversion currency rate.
protected void btnconvert_Click(object sender, EventArgs e)
        {
            net.webservicex.www.CurrencyConvertor conver = new net.webservicex.www.CurrencyConvertor();
            net.webservicex.www.Currency From = (net.webservicex.www.Currency)Enum.Parse(typeo(net.webservicex.www.Currency),
                                                   ddfromcurrency.SelectedValue.ToString(), true);
            net.webservicex.www.Currency To = (net.webservicex.www.Currency)Enum.Parse(typeof(net.webservicex.www.Currency,
                                                ddtocurrency.SelectedValue.ToString(), true);
            double d = conver.ConversionRate(From, To);/* web service  * method to call for conversion currency rate */         lblcurrency.Text="Convert Currency : "+(d * Convert.ToDouble(txtvalue.Text)).ToString();            lblcurrency.Visible = true;
        }
5. Run the application and click on the Convert Button and get the result:

CurrConver2.gif

Figure 2 : Currency Conversion Output

Conclusion : Consume web service in ASP.Net.
 

No comments:

Post a Comment