Ok, so we all know that pages and pages of code can really be a pain. Luckily with .NET and Visual Studio, web service calls don't have to be the coding equivalent of war and peace, and in this guide we're going to give a quick overview to using Service References to improve and hasten the implementation of our services.
Step 1: Service References
For this example we're going to use our bank validation service.
So the first step is to pull in all the information from our services WSDL using a service reference. Right Click your application title and select "Add Service Reference.......", this will open the Add Service Reference Window:
As you can see we have given the Service Reference a name of ‘Bank_Account_Validator’, and copied the WSDL address into the "Address:" bar. (All our WSDL addresses can be found on our API documentation pages on postcodeanywhere.co.uk, the WSDL used in this example can be found here.)
Clicking OK will download the service information to Visual Studio. You should now see the service reference has been added to your solution explorer.
Step 2: Form Controls
At this point we'll take some time out to put our Form design together, just like this:
All we have here are two text boxes to take the variables Sort Code and Account Number, a button to trigger the validation, a whole bunch of labels to layout the data, and a whole bunch of matching blank labels to display the data. We also have two other small blank buttons which we will use to display the Boolean responses for the top two returned fields.
Step 3: Code
Right, we have our service reference downloaded, and we have a wonderfully put together form to do the search with. Now it's time to tie it all together.
This is where the service reference approach really comes in handy; not only does it speed up the process by adding the parameters you need to use to the IntelliSense menu, it also negates the need to write a lot of messy code to build the request URLs.
In this example we are going to create some objects in the Public Class Form1 so that they are publicly available within the application.
We're going to create two separate objects:
- objBVSOAPClient – to handle the actual request
- objBVresult – to handle the array of results returned
So in our Public Class we add:
Dim objBVSOAPClient As New Bank_Account_Validator. PostcodeAnywhere_SoapClient Dim objBVresult As New Bank_Account_Validator. BankAccountValidation_Interactive_Validate_v1_00_ArrayOfResults
You'll see how the IntelliSense picks up the Service reference, and allows you to quickly select the required service.
Now we have our objects in place we can set the on-click event code.
Firstly we have to get our array of results, so we set our results object as the result of our soap client web service call:
objBVresult = objBVSOAPClient.BankAccountValidation_Interactive_Validate_v1_00("AA11-AA11-AA11-AA11", tb_accountnumber.text, tb_sortcode.text)
Again the IntelliSense kicks in to enable you to quickly select the correct web service method:
As you can see below, using this method to integrate web services allows you to see all the parameters required by the method as well as their associated type:
The rest of the code is pretty simple, objBVresult now contains an array of our results, and as were only making a request with one row returned, all our data is stored in objBVresult(0).
So to populate the rest of the form we use the code below (make a note of the element names!):
Try objBVresult = objBVSOAPClient.BankAccountValidation_Interactive_Validate_v1_00("XT26-EX38-JF28-XR38", tb_AccountCode.Text, tb_SortCode.Text) If objBVresult(0).IsCorrect = True Then btn_isvalid.BackColor = Color.Green Else btn_isvalid.BackColor = Color.Red End If If objBVresult(0).IsDirectDebitCapable = True Then btn_isDD.BackColor = Color.Green Else btn_isDD.BackColor = Color.Red End If lbl_bankbic.Text = objBVresult(0).BankBIC lbl_branch.Text = objBVresult(0).Branch lb_bank.Text = objBVresult(0).Bank lb_corAN.Text = objBVresult(0).CorrectedAccountNumber lb_iban.Text = objBVresult(0).IBAN lb_statusinfo.Text = objBVresult(0).StatusInformation lbl_branchbic.Text = objBVresult(0).BranchBIC lbl_corSC.Text = objBVresult(0).CorrectedSortCode lbl_Fax.Text = objBVresult(0).ContactFax lbl_line1.Text = objBVresult(0).ContactAddressLine1 lbl_line2.Text = objBVresult(0).ContactAddressLine2 lbl_PC.Text = objBVresult(0).ContactPostcode lbl_Tel.Text = objBVresult(0).ContactPhone lbl_town.Text = objBVresult(0).ContactPostTown Catch MsgBox(ErrorToString) End Try
And just to restate the efficiency of this method you will see the IntelliSense coming back with the returned field names:
As you can see we've thrown a Try – Catch around the whole thing to clear up any mess, and we've also used the back colour of the two small buttons to show a positive or negative response.
So there it is, the finished article!
The Sort Code and Account Code I have used here can be used to test a positive result in the search, obviously you can just make up some details to test a negative result.
All of our web services can be implemented using different versions of this implementation method. So feel free to try them out.
And remember if you get stuck, or have any questions our team are happy to take your enquiry and try to guide you in the right direction.
Can't find what you're looking for?
0800 047 0493