Home / Articles / Programming Languages / Web Services / A Web Services Primer (2/10): Consume It, Your Majesty / Using the Currency Web Service

“Chishiki” is Japanese for “knowledge.” e-chishiki.com aims to bring software developers, information security professionals, IT executives and other IT pros a rich body of knowledge in the form of articles, interviews, tutorials and technical discussions. Our contributors are among the biggest names in the Indian IT industry and include noted authors, educators and practitioners.

Weekly Programming Series - A Web Services Primer

A Web Services Primer (2/10): Consume It, Your Majesty

Yashavant Kanetkar and Asang Dani
Yashavant Kanetkar and Asang Dani

Index


Using the Currency Web Service

The Currency Web Service developed by us has the following features:

  1. It can return a list of all countries in the world
  2. It can return a list of currency symbols and names for respective countries. Note that each currency in the world has a three letter symbol, e.g. INR, JPY, USD, EUR etc.
  3. It can return a rate of conversion from any currency to any other currency. This is a live rate which is updated in real time.

Let us now use these features of the Currency Web Service in our application. To do this, we will create a WinForm application using C#. We will assume that you have at least a nodding acquaintance with the creation of a WinForm application in VS2005. Our application will be able to get currency conversion rates in bulk, even though the web service can only get one rate of conversion at a time.

Given below are the steps involved in building the WinForm application:

(a) Create a new Visual C# WinForm (Windows Application) project called CurrencyClient01.

(b) Drag and drop controls on the WinForm as shown in Figure 4. Give names to the controls as indicated in the figure.

Figure 4 – WinForm Controls

(c) Add Web Reference to the CurrenyWebService from Solution explorer window by right clicking on References node. When prompted to enter the reference name, add com.ksetindia.ws.currency as shown in Figure 5.

Figure 5 – Add Web Reference to CurrencyWebService

Visual Studio creates a proxy class called CurrencyWebService on your machine. It communicates with the web service. The class is automatically placed inside the namespace CurrencyClient01.com.ksetindia.ws.currency. We have to add the following statement at the top of CurrencyClient.cs to be able to use the proxy class:

using CurrencyClient01.com.ksetindia.ws.currency;

(d) Add event handlers CurrencyClient_Load( ) and GoButton_Click( ) to CurrencyClient.cs.

Before we get into the event handlers, we need to do some initialization work in the constructor. This is shown below:

private CurrencyService                                            service ;
private SortedDictionary<String, CountryCurrency> countries ;

public CurrencyClient( )
{
  InitializeComponent( ) ;
  service = new CurrencyService( ) ;
  countries = new SortedDictionary<string, CountryCurrency>( ) ;
  foreach( CountryCurrency c in service.GetCountryCurrencies( ) )
    countries.Add ( c.country, c ) ;
}

To use the web service, we must create an object of the proxy class CurrencyService. Following this, we have obtained the list of counties and their currencies by calling the web service function GetCountryCurrencies( ) using the object service. For easy lookup from country name to its currency and symbol, we have created a sorted dictionary with county name as the key and CountryCurrency object as the value. Each CountryCurrency object holds country name, currency and its symbol.

Comments

I always get the value as -1

I always get the value of converted currency as -1 even when converting Rupee to Rupee. Is there anything wrong with the method returning values??

Log in or create a user account to comment.

On Sale From April 2008

Let Us C
8th Ed.
C programming classic & best seller. 1 million+ copies sold!

Y. Kanetkar

On Sale From April 2008

Introduction to Object Oriented Programming & C++

Y. Kanetkar

On Sale From Fall 2008

Microsoft .NET Framework: Web Application Security

Vijay Mukhi

On Sale From Nolvember 2008

Quest C++ Courseware
12+ hours of instructional audio and animated slides.

Y. Kanetkar Asang Dani

On Sale From November 2008

A Programmer's Guide to Web Application Security

Vijay Mukhi

Latest Forum Posts