SIGN UP MEMBER LOGIN:    
ARTICLE

Get a Conversion Rate From One Currency to Another Currency

Posted by Sandeep Shekhawat Articles | C# Language June 06, 2011
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.
Reader Level:
Download Files:
 


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>Value</td>
  <td> <asp:TextBox ID="txtvalue" runat="server"></asp:TextBox> </td> 
  <td>From</td>
  <td><asp:DropDownList ID="ddfromcurrency" runat="server"></asp:DropDownList></td> 
  <td>To</td>
  <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.
 

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor