Web Service Tutorial - Metric Conversion
page 1 of 1
Published: 05 Mar 2004
Unedited - Community Contributed
Abstract
A quick tutorial on web services with VS.NET; implement a metric conversion web service and call it from an ASP.NET page.
by Steven Swafford
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 19288/ 24

Metric Conversion

Here I will attempt to show how simple a web service can be created. This web service will take a integer input a do the appropriate conversions.

If you not familiar with web services this article should be a good starting point to build upon.

1. First, create a webservice within Visual Studio.Net
2. Name this page conversions.asmx
3. Open the code behind "conversions.asmx.vb (below is what my sample contains).

......
<WebMethod(Description:="Converts Inches To Millimeters")> Public Function millimeters_to_inches(ByVal inches As Double) As Double
millimeters_to_inches = CStr(inches * 25)
End Function

<WebMethod(Description:="Converts Feet To Centimeters")> Public Function feet_to_centimeters(ByVal feet As Double) As Double
feet_to_centimeters = CStr(feet * 30)
End Function

<WebMethod(Description:="Converts Yards To Meters")> Public Function yards_to_meters(ByVal yards As Double) As Double
yards_to_meters = CStr(yards * 0.9)
End Function

<WebMethod(Description:="Converts Miles To Kilometers")> Public Function miles_to_kilometers(ByVal miles As Double) As Double
miles_to_kilometers = CStr(miles * 1.6)
End Function

<WebMethod(Description:="Converts Centimeters To Inches")> Public Function centimeters_to_inches(ByVal centimeters As Double) As Double
centimeters_to_inches = CStr(centimeters * 0.393)
End Function

<WebMethod(Description:="Converts Meters To Yards")> Public Function meters_to_yards(ByVal meters As Double) As Double
meters_to_yards = CStr(meters * 1.1)
End Function

... Code removed for saving of space

Now that you have you web service page complete we can now compile the code to a dll (In this case I will use MetricConversions.dll).

To accomplish this we will use the following batch file. Be sure the paths reflects your current paths and this file resides in the same folder as your code. At that point execute the batch file and then copy your newly created dll to your applications bin folder.

@echo off
Set UseLanguage=VB
Set WSDLPath=http://localhost/webservices/MetricConversion/conversions.asmx?WSDL
Set outCodeFileName=MetricConversions.vb
Set outDLLFileName=MetricConversions.dll
Set myNamespace=MetricConversions
set assemblies=System.Web.dll,System.dll,System.Web.Services.dll,System.Xml.dll,System.Data.dll

wsdl.exe /n:%myNamespace% /l:%UseLanguage% /out:%outCodeFileName% %WSDLPath%

vbc.exe /out:%outDLLFileName% /t:library /r:%assemblies% %outCodeFileName%

Now that you have you web service page coding is complete and you have created the application dll we can create a web form to consume the service. Be sure to add this as a Web Reference to you project.

My sample page is named "conversion_demo.aspx" (below you will see the server controls the web service namespace).

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="conversion_demo.aspx.vb" Inherits="WebServices.conversion_demo"%>
<%@ Import Namespace="MetricConversions" %>

<form id="Form1" method="post" runat="server">

Convert Millimeters to Inches:
<asp:TextBox id="TextBox1" Width="50" runat="server"></asp:TextBox>
<asp:Button id="Button1" runat="server" OnClick="mtoi_click" Text="Button"></asp:Button>
<asp:Label id="Label1" runat="server" Font-Bold="true"></asp:Label>

Convert Feet to Centimeters
<asp:TextBox id="TextBox2" Width="50" runat="server"></asp:TextBox>
<asp:Button id="Button2" runat="server" OnClick="ftoc_click" Text="Button"></asp:Button>
<asp:Label id="Label2" runat="server" Font-Bold="true"></asp:Label>

Convert Yards to Meters
<asp:TextBox id="TextBox3" Width="50" runat="server"></asp:TextBox>
<asp:Button id="Button3" runat="server" OnClick="ytom_click" Text="Button"></asp:Button>
<asp:Label id="Label3" runat="server" Font-Bold="true"></asp:Label>

</form>

Here is the code behind that handles the OnClick events:

Public Sub mtoi_click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim objConv As New MetricConversions.conversions()
Dim TextBox As Integer
TextBox = TextBox1.Text
Label1.Text = objConv.millimeters_to_inches(TextBox) & (" Inches")
End Sub

Public Sub ftoc_click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim objConv As New MetricConversions.conversions()
Dim TextBox As Integer
TextBox = TextBox2.Text
Label2.Text = objConv.feet_to_centimeters(TextBox) & (" Centimeters")
End Sub

Public Sub ytom_click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim objConv As New MetricConversions.conversions()
Dim TextBox As Integer
TextBox = TextBox3.Text
Label3.Text = objConv.yards_to_meters(TextBox) & (" Meters")
End Sub

This example will allow the user to input an integer value and the appropriate conversion value will be returned and assigned to the appropriate lables.



User Comments

Title: Web Services   
Name: ryan
Date: 2004-10-18 11:09:10 AM
Comment:
I am doing a project where i have to create a form that implements 3 web services. I have to create one myself, and i found this tutorial very helpful. however, i was wondering if you have any links to other web services that do conversions that i could use?

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2017 ASPAlliance.com  |  Page Processed at 2017-12-17 3:07:10 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search