Sending SOAP Requests by Using Visual Studio 2005 Client (Visual Basic)
The example in this topic is similar to the example provided in Sending SOAP Requests by Using Visual Studio 2005 Client (C#). The only difference is that this example is written in Visual Basic. Therefore, only the required code and the steps to create the example are provided in this topic.
Creating a Working Sample
Because of the many steps in creating this sample, the application is separated into four parts:
- Part 1: Execute the stored procedure.
- Part 2: Execute the SQL query without parameters.
- Part 3: Execute the SQL query with parameter.
- Part 4: Execute the user-defined function.
Each part provides a set of instructions. At the end of each part, you can test the application.
To create the required proxy class
- Create a folder on the computer for this application.
- On the client computer, from the Microsoft Visual Studio 2005 program group, start Microsoft Visual Studio 2005.
- Click New Project.
- Select Visual Basic Projects as the Project Type.
- In the Name text box, specify NativeSOAPApp2.
- In the Location text box, specify a folder path where you want to save the project.
- Select Windows Application as the template, and then click OK.
- In the Solution Explorer window, Right-click References, and then select Add Web Reference. Another way to add a Web reference is from the Project menu, and select Add Web Reference.
- In the Address box, type https://Server/sql?wsdl, where Server is the name of the server, and then press ENTER. If the WSDL generation succeeds, you will see the endpoint description. At this point, click AddReference. This creates the required proxy classes so that you can call the methods in the WSDL document.
Part 1: Execute the Stored Procedure
In this part, the application requests the GetCustomerInfo Web method.
- In Form1, add a text box (textBox1), a button (button1), and a list box (listBox1).
- Right-click text box, and then select Properties. Change the Text value from textBox1 to 1. This is the default Customer ID value.
- Right-click button1, and then select Properties.
- Change the Text property value from button1 to ExecSP.
- Change the (name) property value to ExecSP.
- Right-click the list box (listBox1), and then select Properties. Change the HorizontalScrollbar property value to True.
- Double-click ExecSP.
- Copy the code from Visual Basic Code Listing for ExecSP to this function.
- Update the code. Change references to
server
by the host name identified when the endpoint was created by using CREATE ENDPOINT. - Save and compile the project. For more information, see the previous section "Compiling the Code."
Part 2: Execute the SQL Query Without Parameters
In this part client application executes an ad hoc query (FOR XML query) that retrieves three employees from the Employee table in AdventureWorks database.
- In Form1, in the [Design] tab, add another button (button1).
- Right-click this new button, and then select Properties.
- Change the Text property value from button1 to ExecBatchFindAllEmps.
- Change the (name) property value to ExecBatchFindAllEmps.
- Double-click ExecBatchFindAllEmp.
- Copy the code from Visual Basic Code Listing for FindAllEmpsto this function.
- Update the code. Change references to
server
by the host name identified when the endpoint was created by using CREATE ENDPOINT. - Save and compile the project. For more information, see the previous section "Compiling the Code."
Part 3: Execute the SQL Query with Parameters
This part is similar to the previous part except that the SOAP request for the ad hoc query includes a query parameter. The FOR XML query retrieves employee information for the specified employee ID.
- In the Form1, in the [Design] tab, add a button (button1).
- Right-click this new button, and then select Properties.
- Change the Text property value from button1 to ExecBatchFindAnEmp.
- Change the (name) property value to ExecBatchFindAnEmp.
- Double-click ExecBatchFindAnEmp.
- Copy the code from Visual Basic Code Listing for FindAnEmp to this function.
- Update the code. Change references to
server
by the host name identified when the endpoint was created by using CREATE ENDPOINT. - Save and compile the project. For more information, see the previous section "Compiling the Code."
Part 4: Execute the User-defined Function
In this part, the client application sends a SOAP request for the UDFReturningScalar Web method. This Web method corresponds to a user-defined function that returns an integer value.
- In the Form1, in the [Design] tab, add another button (button1).
- Right-click this new button, and then select Properties.
- Change the Text property value from button1 to ExecUDFReturningScalar, and
- Change the (name) property value to ExecUDFReturningScalar.
- Double-click ExecUDFReturningScalar.
- Copy the code from Visual Basic Code Listing for ExecUDF to this function.
- Update the code. Change references to server by the host name identified when the endpoint was created by using CREATE ENDPOINT.
- Save and compile the project. For more information, see the previous section "Compiling the Code."
Because the endpoint specifies Integrated Authentication, the following line appears in the code:proxy.Credentials = System.Net.CredentialCache.DefaultCredentials
.
If you use SQL Server authentication, we recommend that you remove this line and replace it with code that implements WS-Security headers to the provided SQL Server-based authentication credentials. For more information, see SQL Server Authentication over SOAP.
Note
If you are using either SQL Server authentication or Basic authentication, SQL Server 2005 requires that you use Secure Sockets Layer (SSL) for secure encrypted transmission of user credentials that would otherwise be visible as clear text. For more information about how to enable SSL to use with HTTP endpoints, see Configuring Certificate for Use by SSL.
Compiling the Code
SQL Server 2005 installs the Microsoft .NET Framework 2.0 as part of its installation process. These compilation instructions use the latest .NET Framework (This is the highest number in the \WINDOWS\Microsoft.NET\Framework folder.)
To compile the code
- Save the whole project.
Open a command prompt and locate the folder where the project is saved.
From this folder, copy the Reference.vb file from Web Reference\Server subfolder (for example, copy "Web Reference\Server\Reference.vb"). The Form1.vb and the Reference.vb files should be in the same folder.
Compile the code and specify the name of your executable (.exe) file. For example, if the name of the executable is NativeSOAPApp2.exe, the command line would be as follows:
\WINDOWS\Microsoft.NET\Framework\v2.0.xxxxx\vbc.exe /out:NativeSOAPApp2.exe /r:System.dll /r:System.Data.dll /r:System.Xml.dll /r:System.Web.Services.dll /r:Microsoft.VisualBasic.dll /r:System.Drawing.dll /r:System.Windows.Forms.dll /m:Form1 Form1.vb Reference.vb
xxxxx is the folder number corresponding to the version of the .NET Framework.
This creates the executable (NativeSOAPApp2.exe) in the current directory.Note
If you are using an earlier version of the .NET Framework, you may receive a run-time error. If this occurs, try updating the GetCustomerInfo stored procedure by adding the
FOR XML AUTO
clause at the end of theSELECT
statement as shown:SELECT TOP 3 SalesOrderID, OrderDate FROM SalesOrderHeader WHERE CustomerID = @CustomerID FOR XML AUTO
The results of executing the stored procedure are displayed in the list box.
See Also
Reference
Setting the Server to Listen for Native XML Web Services Requests
SOAP Request and Response Message Structure
Sample Applications for Sending Native XML Web Services Requests
Guidelines and Limitations in Native XML Web Services
Concepts
Best Practices for Using Native XML Web Services