In this short article I will explain how to call (consume) Web Service (ASMX) in Windows Forms (WinForms) application using C# and VB.Net.
 
Note: For illustration purpose, I am making use of the Free Web Service used for finding the geographical location of an IP Address.
 
 
Adding Reference of the Web Service in Visual Studio
1. Right click the project in Solution Explorer and choose Add Service Reference option from the context menu.
Call (Consume) Web Service (ASMX) in Windows Forms (WinForms) application using C# and VB.Net
 
2. Now in the Add Service Reference Dialog you need to click on the Advanced button.
Call (Consume) Web Service (ASMX) in Windows Forms (WinForms) application using C# and VB.Net
 
3. In the Service Reference Settings Dialog you need to click on Add Web Reference button.
Call (Consume) Web Service (ASMX) in Windows Forms (WinForms) application using C# and VB.Net
 
4. Now in the Add Web Reference Dialog you need to add the URL of the Web Service and click the Green Go button to discover it. Once the Web Service is discovered, give its reference an appropriate name using the Web Reference Name TextBox and click on Add Reference button.
Call (Consume) Web Service (ASMX) in Windows Forms (WinForms) application using C# and VB.Net
 
5. Finally you will notice the Web Service Reference has been added to your project.
Call (Consume) Web Service (ASMX) in Windows Forms (WinForms) application using C# and VB.Net
 
 

Accessing the Web Service and its Methods in Windows Forms (WinForms) application
Using the Web Reference Name given in step #4, you can access the Web Service and its methods.
Form Controls
In the below Form, a TextBox, a Button and a Label control.
Call (Consume) Web Service (ASMX) in Windows Forms (WinForms) application using C# and VB.Net
 
 
Fetching the location from the Web Service
Inside the Button click event handler, an object of GeoIPService is created and then IP Address is passed as parameter to the GetGeoIP method which returns the name of the Country to which the IP address belongs. It is then displayed using the Label control.
C#
private void btnGetCountry_Click(object sender, EventArgs e)
{
    GeoService.GeoIPService service = new GeoService.GeoIPService();
    GeoService.GeoIP output = service.GetGeoIP(txtIPAddress.Text.Trim());
    lblCountry.Text = "Country: " + output.CountryName;
}
 
VB.Net
Private Sub btnGetCountry_Click(sender As System.Object, e As System.EventArgs) Handles btnGetCountry.Click
    Dim service As New GeoService.GeoIPService()
    Dim output As GeoService.GeoIP = service.GetGeoIP(txtIPAddress.Text.Trim())
    lblCountry.Text = "Country: " + output.CountryName
End Sub
 
Call (Consume) Web Service (ASMX) in Windows Forms (WinForms) application using C# and VB.Net
 
 
Downloads