Using Web Services: How to Consume

Consuming a web service is easier that creating one. All you have to do is create a reference and then add a couple of lines of code and you have the web service to use in your application.

 

The way to add a web reference to your web service is to first figure out where the web service resides.  It can be a full web address or it can be a local folder.  Once you figure that out you need to create a reference to it. To do this:

 

Right click on the project and select add web reference. When you do this you are presented with a box – Add Web Reference. From here you will choose the correct link under Browse to: and select the web service you will use.  I chose local machine since I am working within an Intranet.  The service URL shows up. I just have to click it and the URL at the top auto fills with the URL of the web service. Then you choose add reference under the right window to add the reference to your application.  Visual Studio will ask you if you want to put it in its own folder or not. I like to keep my things separate so I choose to keep them separate. 

 

Once you do that, all you have to do is add one of code to start using it in your application:

 

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim wcConvert As New foldername.Servicename()

 

When you create the web service it will be in a folder called App_WebReferences. Under that will be the folder name mentioned above. Under the folder is the service file. So you want to reference it by foldername.servicename().  That is it! You can start using it now.

 

What I did was convert it to a DataView so I could extract the data out of the dataset I needed. In order to do this, it woul dbe best to convert the dataset (web service) to a dataview. You have more options with a dataview.  I found out through trial and error that you cannot set up a dataview as a web service.  It is not a recognized XML format apparently.  So to convert you need to add this to your code:

 

Imports System.Data

Imports System.Data.DataView

Dim dv As New DataView

dv = wcConvert.functionname().Tables(0).DefaultView

 

With the dataview you can now sort (dv.sort) and select distinct values DDLcontrolname.DataSource = dv.ToTable(True, “columnName”).

 

With all the above in place you can now use the DataSource to bind to a control.

 

DDLcontrolname.DataTextField = “Name”

DDLcontrolname.DataValueField = “Name”

DDLcontrolname.DataBind() 

That is all there is to it. Now I have to figure out how to implament caching so not to hit the web service every page refresh. Until next time, always remember, with great power comes great responsibility.