Current Currency Rate in Your Website
Do you want the current currency rates embedded in your webpage? Here is the tutorial.
Here I am using a webservice again.
Open this link
webservice
Now, before using it, ensure it's running properly, then scroll down and click the function "ConversionRate".
Now enter the parameters and click the "Invoke" button.
Finally, you will get the current rate in a new window, as in the following:
Fine, it's working.
Well, now let's embed this into a webpage.
First of all, add the reference for this webservice to your website.
- Create a new website
- Open Solution Explorer, select your website and right-click and select "Add Web Reference"
- Now the following window will be opened, enter http://www.webservicex.net/CurrencyConvertor.asmx?WSDL as the URL, press Enter and change the web reference name (it's optional, I changed it to "myservice"), and click the "Add Reference" button (circled in the image)
Now enter the following code in the Default.aspx file. I put the country's names in the dropdownlist and put their codes in the value property (country codes are copied from the given webservice).
Now copy the following code into the Default.aspx.cs page:
- using System;
- using System.Collections.Generic;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
-
- public partial class _Default: System.Web.UI.Page {
- protected void Page_Load(object sender, EventArgs e) {}
- protected void Button1_Click(object sender, EventArgs e) k {
-
- try {
-
- myservice.CurrencyConvertor cc = new myservice.CurrencyConvertor();
-
- myservice.Currency c = new myservice.Currency();
-
-
-
- double v = cc.ConversionRate((myservice.Currency) System.Enum.Parse(c.GetType(), drpFrom.SelectedValue), ((myservice.Currency) System.Enum.Parse(c.GetType(), drpTo.SelectedValue)));
- double unit = 0;
- if ((txtAmt.Text.Trim() != "") || (txtAmt.Text == null)) {
- unit = Convert.ToDouble(txtAmt.Text.Trim());
- }
- double amt = v * (unit);
- lblAmt.Text = "Total Amount =" + amt;
- } catch (Exception ex) {
- lblAmt.Text = "Error";
- }
- }
- }
Finally, run your webpage.