Accessing a Web Service from a Desktop in VB.NET

What is a Web Service?

A web service is a collection of protocols and standards used for exchanging data between applications or systems. Software applications written in various programming languages and running on various platforms can use web services to exchange data over computer networks like the Internet in a manner similar to inter-process communication on a single computer.

What is WSDL?

WSDL or Web Services Description Language is an XML format for describing network services as a set of endpoints operating on messages containing either document-oriented or procedure-oriented information. The operations and messages are described abstractly, and then bound to a concrete network protocol and message format to define an endpoint. Related concrete endpoints are combined into abstract endpoints

Advantages of Using Web Services 

  • Interoperability - This is the most important benefit of Web Services. Web Services typically work outside of private networks, offering developers a non-proprietary route to their solutions. Services developed are likely, therefore, to have a longer life-span, offering better return on investment of the developed service. Web Services also let developers use their preferred programming languages. At this date, Java, C++, VBScript, JavaScript, and Perl all have one or more Web Service APIs maintained by an array of vendors. In addition, thanks to the use of standards-based communications methods, Web Services are virtually platform-independent.
  • Usability - Web Services allow the business logic of many different systems to be exposed over the Web. This gives your applications the freedom to chose the Web Services that they need. Instead of re-inventing the wheel for each client, you need only include additional application-specific business logic on the client-side. This allows you to develop services and/or client-side code using the languages and tools that you want.
  • Reusability - Web Services provide not a component-based model of application development, but the closest thing possible to zero-coding deployment of such services. This makes it easy to reuse Web Service components as appropriate in other services. It also makes it easy to deploy legacy code as a Web Service.

Creating a Web Service steps are as

Create new project

>>ASP.Net Web Service called as "DbService". Add new class called as "DbHelper.vb".

Function openConnection() As SqlConnection
Function closeConnection(ByVal con As SqlConnection)
Function getDataSet(ByVal strSql As String) As DataSet
Function getDataTable(ByVal strSql As String) As DataTable

Db_Webserv.asmx

Add <WebMethod()> _

  1. Function connectToDB()
  2. Function getProducts() As DataSet

to the code behind "Db_Webserv.asmx.vb".

Now create "ApplnServ" as desktop application in VB.Net. Add new form "FrmServ.vb" or rename existing default form. Now we need to add webreference of web service to our desktop applicatoin. Right click on "ApplnServ" click on "Add Web Reference...". New window will appear in which you can perform following:

  1. Web service on the local machine
  2. Browse UDDI service on the local network
  3. UDDI Directory
  4. Test Microsoft UDDI Directory

Select the very first option. It will locate all the list of web service available on local machine. Click on Webservice name i.e Db_WebServ, give name "DbService" & click on Add Reference. Add a datagrid on FrmServ, finally add following code in "FrmServ.vb".

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

      Dim objServ As New DbService.Db_Webserv

      Dim retVal As Integer

      retVal = objServ.connectToDB()

      If retVal = 1 Then

          txtMessage.Text = "Connected to database successfully."

          Dim dtst As New DataSet

          dtst = objServ.getProducts()

          dtgrdProducts.DataSource = dtst.Tables(0)

      Else

          txtMessage.Text = "Unable to connect to the database, check the connection information"

      End If

End Sub

 

Idea is you can keep these two application at different location and you can access webservice from either local machine, UDDI Directory or using Microsoft UDDI Directory.

Table used here is products from "Northwind" database.

DbHelper.vb

Imports System.Data

Imports System.Data.SqlClient

Imports System.Configuration

Public Class DbHelper   

    '' Connection to perform database activity

#Region "Open Sqlconnection"

    Public Function openConnection() As SqlConnection

        Dim con As SqlConnection

        con = New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))

        If (con.State = ConnectionState.Open) Then

            If (con.State = ConnectionState.Open) Then

                con.Close()

            End If

        End If

        con.Open()

        Return con

    End Function

#End Region

 

#Region "Close Connection"

    Public Function closeConnection(ByVal con As SqlConnection)

        If (con.State = ConnectionState.Open) Then

            con.Close()

        End If

    End Function

#End Region

 

#Region "getDataset"

    Public Function getDataSet(ByVal strSql As String) As DataSet

 

        Dim con As SqlConnection

        Dim sda As SqlDataAdapter

        Dim scb As SqlCommandBuilder

 

        Try

            Dim dtst = New DataSet

            con = New SqlConnection

            con = openConnection()

            Dim cmd = New SqlCommand(strSql, con)

            sda = New SqlDataAdapter(cmd)

            scb = New SqlCommandBuilder(sda)

            sda.Fill(dtst)

            Return dtst

 

        Catch ex As Exception

            Throw New Exception(ex.Message)

        Finally

            sda.Dispose()

            scb.Dispose()

            closeConnection(con)

        End Try

 

    End Function

#End Region

 

#Region "getDataTable"

    Public Function getDataTable(ByVal strSql As String) As DataTable

        Dim con As SqlConnection

        Dim sda As SqlDataAdapter

        Dim scb As SqlCommandBuilder

 

        Try

            Dim dtst As New DataSet

            Dim dt As New DataTable

            con = New SqlConnection

            con = openConnection()

            Dim cmd As New SqlCommand(strSql, con)

            sda = New SqlDataAdapter(cmd)

            scb = New SqlCommandBuilder(sda)

            sda.Fill(dtst)

            dt = dtst.Tables(0)

            Return dt

 

        Catch ex As Exception

            Throw New Exception(ex.Message())

        Finally

            sda.Dispose()

            scb.Dispose()

            closeConnection(con)

        End Try

    End Function

#End Region

 

End Class 

To test this application you can run webservice on your local machine and invoke different methods available.

Up Next
    Ebook Download
    View all
    Learn
    View all