RAD Studio for Microsoft .NET |  |

Adding Web References in ASP.NET Projects
See Also
Collapse All

If you want to consume a web service, you must create a client application, and add a Web Reference. These procedures describe how to create an ASP.NET client application that consumes a third-party web service. The client application consumes the DeadOrAliveWS web service available from the XMethods Web site. This web service lets you query a simple database of celebrities and their respective birthdates and expiration dates.

To create an ASP.NET project

1. Choose FileNewOther. The New Items dialog box appears.
2. Double-click the ASP.NET Web Application icon in the Delphi for .NET Projects item categories. The New ASP.NET Application dialog box appears.
3. In the Name field, enter a name for your project.
4. In the Location field, enter a path for your project.
   Tip: Most ASP.NET projects reside in the IIS directory Inetpub\wwwroot
   .
5. If necessary, click the View Server Options button to change your Web server settings.
   Tip: The default Server Options will usually be sufficient, so this step is optional.
6. Click OK. The Web Forms Designer appears.

To design the ASP.NET web page

1. If necessary, click Design view.
2. From the Web Controls category of the Tool Palette, place a Button component onto the Designer surface. The Button control appears on the Designer. Make sure the control is selected.
3. In Object Inspector, set the Text property to Dead or Alive?.
4. From the Web Controls category of the Tool Palette, place a TextBox component onto the Designer above the Button. This is where you type your query to the Web Service.
5. Place a Label component below the Button. This is where the results of the web service query are displayed.

Use the UDDI browser to locate the DeadOrAlive Web Service on the internet. This allows you to use the methods and objects published by the Web Service Definition Language (WSDL).
To add the Web Reference for DeadOrAliveWS

1. Choose ProjectAdd Web Reference.
2. In the CodeGear UDDI Browser web dialog box, click the XMethods Full link in the list of available UDDI directories. A list of various web services published on the XMethods Web site appears.
3. Find and click the DeadOrAliveWS link.
   Tip: You can use Ctrl+F
   to search within the CodeGear UDDI Browser.
4. Click the link to the WSDL file:

Copy Code

    http://www.abundanttech.com/webservices/deadoralive/deadoralive.wsdl

A WSDL document appears. This XML document describes the interface to the DeadOrAliveWS web service.

1. Click Add Reference to add the WSDL document to the client application. A Web References folder containing a com.abundanttech.www node is added to the Project directory in the Project Manager.

To write the application logic

1. If necessary, click Design view.
2. Double-click the Dead or Alive? button to view the code-behind file.
3. For a Delphi for .NET Web Services application, implement the Click event in the Code Editor with the following code :

Copy Code

    procedure TWebForm1.Button1_Click(sender: System.Object; e: System.EventArgs);
    var
      result: DataSet;
      ws: DeadOrAlive;
      currentTable: DataTable;
      currentRow: DataRow;
      currentCol: DataColumn;
    begin
      //This initializes the web service
      ws := DeadOrAlive.Create;

      //Send input to the web service
      result := ws.getDeadOrAlive(TextBox1.Text);

      //parse results and display them
      Label1.Text := '';
      for currentTable in result.Tables do
        begin
          Label1.Text := Label1.Text + '<p>' + #13#10;
          for currentRow in currentTable.Rows do
            begin
              for currentCol in currentTable.Columns do
                begin
                  Label1.Text := Label1.Text + currentCol.ColumnName + ': ';
                  Label1.Text := Label1.Text + (currentRow[currentCol]).ToString;
                  Label1.Text := Label1.Text  + '<br>' + #13#10;
                end;
            end;
          Label1.Text := Label1.Text + '</p>';
        end;

    end;

When you added the Web Reference to your application, RAD Studio used the WSDL to generate a proxy class representing the "Hello World" web service. The Click event uses methods from the proxy class to access the web service. For Delphi for .NET Web Services, you may need to add the unit name of the proxy class, abundanttech.deadoralive, to the uses clause of your Web Form unit to prevent errors in your Click event.

To run the application

1. Choose ProjectBuild All Projects. Now your project is built and resides on your ASP.NET server.
2. Open a Web browser.
3. Type the URL of your Web Application's .aspx file and press Enter.
   Tip: If you are using Microsoft IIS, the URL is the path of the .aspx
   file after Inetpub\wwwroot. For example, if the path of your Web Application is c:\Inetpub\wwwroot\WebApplication1 and your .aspx file is named "WebForm1.aspx", the URL would be http://localhost/WebApplication1/WebForm1.aspx.
4. If necessary, enter your user name and password for your ASP.NET server. The web page for your web application appears.
5. Enter the name of a celebrity (for example, Isaac Asimov) in the text box and click the Dead or Alive? button. Your web application requests the information from the DeadOrAliveWS web service and displays the result in the label.
   Note: If no information is displayed, that name may not be in the database. Check your spelling or try a different name.

See Also

Creating ASP.NET Web Applications

ASP.NET Web Services Overview

Building an ASP.NET "Hello World" Web Services Application

Accessing an ASP.NET "Hello World" Web Services Application

TDataset

Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!