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.