Consuming the Live Currency Conversion Web Service in asp.net C#
Here I choose http://www.webservicex.net/CurrencyConvertor.asmx?WSDL
Step 2)Add 2 DropDownList and 1 button control and one Label to Aspx page(as shown below)
<td class="auto-style1">
<asp:DropDownList ID="DropDownList1From" runat="server"></asp:DropDownList></td>
</td>
<td class="auto-style2">
<asp:DropDownList ID="DropDownList1To" runat="server"></asp:DropDownList></td>
</tr>
<tr><td colspan="2">
<asp:Button ID="btnConvertCurrency"
runat="server" Text="Convert"
OnClick="btnConvertCurrency_Click"
BackColor="Blue"
ForeColor="white"
Font-Size="1em" Font-Names="Comic Sans MS"
/>
</td></tr>
<tr><td class="auto-style1">
<asp:Label
ID="lblCurrencyValue"
runat="server"
Text="Converted Value Here..."
ForeColor="Black"
BackColor="Goldenrod"
Font-Size="2em" Font-Names="Comic Sans MS"
Width="300px">
</asp:Label>
Step 3) Load Currency Codes to DropDownList
void LoadCurrencyCodes()
{
try
{
String[] CurCodes = Enum.GetNames(typeof(CurService.Currency));
DropDownList1To.DataSource = CurCodes; DropDownList1To.DataBind();
DropDownList1From.DataSource = CurCodes; DropDownList1From.DataBind();
DropDownList1From.Items.Insert(0, "--From-");
DropDownList1To.Items.Insert(0, "--To-");
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
Step 4) In the Convert Button Event handler
Call web Service Method
protected void btnConvertCurrency_Click(object sender, EventArgs e)
{
String from = DropDownList1From.SelectedValue as String; String to = DropDownList1To.SelectedValue as String;
double convValue = client.ConversionRate((CurService.Currency)Enum.Parse(typeof(CurService.Currency), from),
(CurService.Currency)Enum.Parse(typeof(CurService.Currency), to));
lblCurrencyValue.Text = convValue.ToString();
}
Step 5) Here Currency converter Namespace is CurService
CurService.CurrencyConvertorSoapClient client = new CurService.CurrencyConvertorSoapClient();
Here is the Output
Complete Source Code
aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="LiveCurrencyConverter.aspx.cs" Inherits="WebApplication1.asmxlive.LiveCurrencyConverter" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
h1
{
color:lightgoldenrodyellow;
background-color:gray;
border-top-left-radius:12px;
width:300px;
height:50px;
font-feature-settings:off;
font-family:'Comic Sans MS';
font-size:1.0em;
}
body
{ margin-left:100px;
margin-top:100px;
}
th
{
background-color:slateblue;
color:white;
}
tr
{
border-style:dotted;
}
.auto-style1
{
width: 230px;
}
.auto-style2
{
width: 166px;
}
table
{
background-color:teal;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Live Currency Convetor in asp.net C#/VB.NET</h1>
<table>
<tr>
<th class="auto-style1">From Currency</th>
<th class="auto-style2">To Currency</th>
</tr>
<tr>
<td class="auto-style1">
<asp:DropDownList ID="DropDownList1From" runat="server"></asp:DropDownList></td>
</td>
<td class="auto-style2">
<asp:DropDownList ID="DropDownList1To" runat="server"></asp:DropDownList></td>
</tr>
<tr><td colspan="2">
<asp:Button ID="btnConvertCurrency"
runat="server" Text="Convert"
OnClick="btnConvertCurrency_Click"
BackColor="Blue"
ForeColor="white"
Font-Size="1em" Font-Names="Comic Sans MS"
/>
</td></tr>
<tr><td class="auto-style1">
<asp:Label
ID="lblCurrencyValue"
runat="server"
Text="Converted Value Here..."
ForeColor="Black"
BackColor="Goldenrod"
Font-Size="2em" Font-Names="Comic Sans MS"
Width="300px">
</asp:Label>
</td></tr>
</table>
</div>
</form>
</body>
</html>
aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApplication1.CurService;
namespace WebApplication1.asmxlive
{
public partial class LiveCurrencyConverter : System.Web.UI.Page
{
CurService.CurrencyConvertorSoapClient client = new CurService.CurrencyConvertorSoapClient();
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
LoadCurrencyCodes();
}
void LoadCurrencyCodes()
{
try
{
String[] CurCodes = Enum.GetNames(typeof(CurService.Currency));
DropDownList1To.DataSource = CurCodes;
DropDownList1To.DataBind();
DropDownList1From.DataSource = CurCodes; DropDownList1From.DataBind();
DropDownList1From.Items.Insert(0, "--From-");
DropDownList1To.Items.Insert(0, "--To-");
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
protected void btnConvertCurrency_Click(object sender, EventArgs e)
{
try{
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApplication1.CurService;
namespace WebApplication1.asmxlive
{
public partial class LiveCurrencyConverter : System.Web.UI.Page
{
CurService.CurrencyConvertorSoapClient client = new CurService.CurrencyConvertorSoapClient();
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
LoadCurrencyCodes();
}
void LoadCurrencyCodes()
{
try
{
String[] CurCodes = Enum.GetNames(typeof(CurService.Currency));
DropDownList1To.DataSource = CurCodes;
DropDownList1To.DataBind();
DropDownList1From.DataSource = CurCodes; DropDownList1From.DataBind();
DropDownList1From.Items.Insert(0, "--From-");
DropDownList1To.Items.Insert(0, "--To-");
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
protected void btnConvertCurrency_Click(object sender, EventArgs e)
{
try{
String from = DropDownList1From.SelectedValue as String;
String to = DropDownList1To.SelectedValue as String;
double convValue = client.ConversionRate((CurService.Currency)Enum.Parse(typeof(CurService.Currency), from),
(CurService.Currency)Enum.Parse(typeof(CurService.Currency), to));
lblCurrencyValue.Text = convValue.ToString();
}
}
String to = DropDownList1To.SelectedValue as String;
double convValue = client.ConversionRate((CurService.Currency)Enum.Parse(typeof(CurService.Currency), from),
(CurService.Currency)Enum.Parse(typeof(CurService.Currency), to));
lblCurrencyValue.Text = convValue.ToString();
}
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
}
}
}
}
No comments:
Post a Comment