货币转换Web服务不起作用

时间:2012-05-16 21:19:08

标签: asp.net

我想在http://www.webservicex.com/CurrencyConvertor.asmx?wsdl使用货币转换器。但它没有工作我在下拉列表菜单中什么都没有。

这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.webservicex.www;

public partial class cc : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        //using web service at http://www.webservicex.com/CurrencyConvertor.asmx?wsdl
        double rate = 0, result = 0;
        Currency curr1, curr2;
        try
        {
            CurrencyConvertor ws = new CurrencyConvertor();
            curr1 = (Currency)Enum.Parse(typeof(Currency), DropDownList1.SelectedItem.Value);
            curr2 = (Currency)Enum.Parse(typeof(Currency), DropDownList2.SelectedItem.Value);
            rate = ws.ConversionRate(curr1, curr2);
            result = System.Convert.ToDouble(TextBox1.Text) * rate;
            Label2.Text = "The exchange rate between" + DropDownList1.SelectedItem.Text + " and " + DropDownList2.SelectedItem.Text + " is " + rate;
            Label3.Text = "Converted " + TextBox1.Text + " to " + result;
        }
        catch
        {
            Label2.Text = "Web service not available please try again later."; 
        }
    }
}

由于

1 个答案:

答案 0 :(得分:0)

我建议每次要显示下拉菜单时避免调用Web服务。货币的选择根本不会经常变化。 (无论如何,你需要准备好处理错误。)

而是,预先下载一次可用的数据并将其存储在适合支持下拉列表的XML文件中:

<Currencies>
     <currency Name="EUR" />
     <currency Name="RUB" />

然后,使用以下ASP.NET代码:

<asp:XmlDataSource ID="XmlDataSource1" runat="server"
        DataFile="~/Currencies.xml">
</asp:XmlDataSource>
<asp:DropDownList ID="DropDownList1" runat="server"
        DataSourceID="XmlDataSource1" DataTextField="Name" DataValueField="Name">
</asp:DropDownList>