Saturday 19 January 2013

Consuming the Live Currency Conversion Web Service in asp.net VB.NET


 Consuming the Live Currency Conversion Web Service in asp.net VB.NET


Step 1)  Add Currency/Exchange convertor WebService Service reference into ASP.NET Application.

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

        Private Sub LoadCurrencyCodes()
            Try
                Dim CurCodes As [String]() = [Enum].GetNames(GetType(CurService.Currency))

                DropDownList1To.DataSource = CurCodes
                DropDownList1To.DataBind()
                DropDownList1From.DataSource = CurCodes
                DropDownList1From.DataBind()
                DropDownList1From.Items.Insert(0, "--From-")
                DropDownList1To.Items.Insert(0, "--To-")
            Catch ex As Exception
                Response.Write(ex.Message)
            End Try
        End Sub


Step 4) In the Convert Button Event handler

Call web Service Method



        Protected Sub btnConvertCurrency_Click(sender As Object, e As EventArgs)
            Dim from As [String] = TryCast(DropDownList1From.SelectedValue, [String])
            Dim [to] As [String] = TryCast(DropDownList1To.SelectedValue, [String])
            Dim convValue As Double = client.ConversionRate(DirectCast([Enum].Parse(GetType(CurService.Currency), from), CurService.Currency), DirectCast([Enum].Parse(GetType(CurService.Currency), [to]), CurService.Currency))

            lblCurrencyValue.Text = convValue.ToString()

        End Sub


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 

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports WebApplication1.CurService
Namespace WebApplication1.asmxlive
    Public Partial Class LiveCurrencyConverter
        Inherits System.Web.UI.Page
        Private client As New CurService.CurrencyConvertorSoapClient()
        Protected Sub Page_Load(sender As Object, e As EventArgs)
            If Not IsPostBack Then
                LoadCurrencyCodes()
            End If
        End Sub


        Private Sub LoadCurrencyCodes()
            Try
                Dim CurCodes As [String]() = [Enum].GetNames(GetType(CurService.Currency))

                DropDownList1To.DataSource = CurCodes
                DropDownList1To.DataBind()
                DropDownList1From.DataSource = CurCodes
                DropDownList1From.DataBind()
                DropDownList1From.Items.Insert(0, "--From-")
                DropDownList1To.Items.Insert(0, "--To-")
            Catch ex As Exception
                Response.Write(ex.Message)
            End Try
        End Sub
        Protected Sub btnConvertCurrency_Click(sender As Object, e As EventArgs)
            Dim from As [String] = TryCast(DropDownList1From.SelectedValue, [String])
            Dim [to] As [String] = TryCast(DropDownList1To.SelectedValue, [String])
            Dim convValue As Double = client.ConversionRate(DirectCast([Enum].Parse(GetType(CurService.Currency), from), CurService.Currency), DirectCast([Enum].Parse(GetType(CurService.Currency), [to]), CurService.Currency))

            lblCurrencyValue.Text = convValue.ToString()

        End Sub

    End Class
End Namespace

Tags:Consuming the Live Currency Conversion Web Service in ASP.NET  Vb.NET,Consuming asmx Webservice in ASP.NET ,ASP.NET  client for asmx webservice,Exchange rate in XAML,currency conversion in ASP.NET

No comments:

Post a Comment