Skip to content

Consuming a Web Service from a Windows Application

In this section, you will create a view that calls the Greeting function in the Greeting Web Service that you created in the Creating Your First Web Service tutorial.

The view we will create will be very simple, with two forms and a button. The first form will be labeled "Name" and the second form "Result." The user will type their name into the Name form and click the button. The name will then be passed to the Greeting Web Service function, and the returned string will be displayed in the second form.

Important!

This section assumes that you have completed the Creating a Web Service Client Class section. You cannot complete this section without first generating a Web Service client class. If you have not done so, switch to Creating a Web Service Client Class now and return here when you have completed that step.

This section also assumes that you have completed the Creating Your First Windows Application book.

Tip

If you have worked through the entire Quick Start book up to this point, your QuickStart workspace will contain at least one Windows application and a web application.

Steps to Create the View

  1. Ensure that the Current Project in Workspace Explorer is MyFirstWindowsApplication.src, not WebApp.src. If you try to create a Windows project component, such as a view, and the current project is a web application, the Studio will display an error message and not allow you to create the component.

  2. Click on the Create New button on the Studio’s toolbar.

  3. Click on the View / Report tab of the Create New... dialog.

  4. Double-click the Data Entry View icon. The "Create a New Windows View Component" dialog will open. Enter oCallGreetingWebService as the object name. The filename automatically changes to CallGreetingWebService.vw as you type the object name. Accept the default directory path (the AppSrc folder in the QuickStart workspace). Click on the Ok button.

  5. Drag and drop two Form controls from the Base Controls group on the Class Palette onto the blank view. Make both forms about twice as wide as their default width.

Next, drag and drop a Button control from the Base Controls group on the Class Palette onto the view.

  1. In the Properties window, change the following:
  2. Change the Label of the oCallGreetingWebService view object to Call Greeting Web Service.
  3. Change the Object Name of the first form to oNameForm, its Label to Name, and its Label_Col_Offset to 30.
  4. Change the Object Name of the second form to oResultForm, its Label to Result, and its Label_Col_Offset to 30.
  5. Change the Label of the Button control to Go.

Your view should now look similar to this:

  1. Drag and drop a cWSGreetingService control from the Web Services group on the Class Palette onto the view.

  2. Double-click on the oWSGreetingService1 object in Code Explorer to display its source code. The source code for this object is made up entirely of comments, which contain information about the class.

The first thing in the comments for this object is the class's interface; you can see that the only function in the class's interface is Function wsGreeting. You also see in its declaration that it accepts a string argument and returns a string, which will aid you in calling this function.

Below the interface listing is a comment about debugging this web service. Ignore this for now; it is discussed in detail in Debugging a Windows Application Web Service Call.

  1. Now you will add code to the Button control. The code here will get the name the user has entered from form oNameForm, call the Web Service function wsGreeting, and display the result of wsGreeting in form oResultForm.

Type the following lines of code inside the already declared Procedure OnClick.

The first line of code is:

String sName sResult

The next line of code is:

Get Value of oNameForm to sName

The next line of code is:

Get wsGreeting of oWSGreetingService1 sName to sResult

This code line makes the call to the wsGreeting function of the oWSGreetingService Web Service client object. It passes the name in sName to wsGreeting and receives the return argument in sResult.

The last line of code is:

Set Value of oResultForm to sResult
  1. The completed code should look like this:

  2. So, let's test the completed view.

    Click on the Run button on the Studio's toolbar. The Studio will compile, then run your project.

    When the application runs, select oCallGreetingWebService from the View menu. Type your name into the form labeled "Name," then click the Go button. You will see "Hello, {Your Name}, how are you today?" in the form labeled "Result."

    The result will look like this:

As you can see, calling a Web Service from a DataFlex Windows application is simple and completely transparent; any complexities have been handled by Studio.

Next Step

Debugging a Windows Application Web Service Call