“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
Event Handlers
Let us now look at the code that you need to add for the event handlers. In the Form load event handler, we populate the two listboxes with the names of the countries retrieved from the web service by calling GetCountryNames( ). This is shown below.
private void CurrencyClient_Load ( object sender, EventArgs e )
{
FromList.DataSource = service.GetCountryNames( ) ;
ToList.DataSource = service.GetCountryNames( ) ;
}
When the user selects the source and target country names, and clicks on the Go button, the doConversion( ) function is called by the Go button's click handler.
private void GoButton_Click ( object sender, EventArgs e )
{
doConversion( ) ;
}
In the doConversion( ) function, we walk through the list of source and target countries one by one and get their conversion rates using their respective currency symbols. GetRateBySymbol( ) web service function is used for this purpose. This is shown below.
private void doConversion( )
{
if ( FromList.SelectedItems.Count == 0 || ToList.SelectedItems.Count == 0 )
return ;
ResultBox.Text = "" ;
for ( int i = 0 ; i < FromList.SelectedItems.Count ; i++ )
{
string from = FromList.SelectedItems[ i ].ToString( ) ;
CountryCurrency fromC = countries[ from ];
for ( int j = 0 ; j < ToList.SelectedItems.Count ; j++ )
{
CountryCurrency toC ;
toC = countries[ ToList.SelectedItems[ j ].ToString( ) ] ;
ResultBox.Text +=
String.Format ( "1 {0} {1} = {2} {3} {4}" + Environment.NewLine,
fromC.country, fromC.currency,
service.GetRateBySymbol ( fromC.symbol, toC.symbol ),
toC.country, toC.currency ) ;
}
}
}
If you select America and Australia from the source listbox and India and Japan from the target listbox, and click Go button, then the conversion rates will be displayed in the ResultBox as shown in Figure 6.
Figure 6 – Conversion rates for many to many
That brings us to the end of our first tryst with the Web Services. You would agree that with modern development tools like VS2005, building an application that consumes a web service is almost child's play. Having said that, let us tell you that we have merely scratched the surface. Down below, there are very many issues that one needs to tackle while consuming Web Services. Do some of these issues occur to you? Think it over, or wait till the next article in this series.




I always get the value as -1
Abhishek Srivastava
June 12, 2008