* Merge winforms framework content to working branch (#14) * Breadcrumb / TOC / Move net5 to net folder (#15) * change path from net5 to net * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Add .net 5 winforms placeholder article (#16) * added some metadata and adjusted net5 placeholder * corrections * corrections * corrections * Test1 * Swapping landing page vs concept * fix links * Fix links * Fix desc
5.9 KiB
title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
| title | ms.date | dev_langs | helpviewer_keywords | ms.assetid | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Bind to a Web Service Using BindingSource | 03/30/2017 |
|
|
ee261207-4573-4cb9-a8cb-5185037e0fba |
How to: Bind to a Web Service Using the Windows Forms BindingSource
If you want to bind a Windows Form control to the results obtained from calling an XML Web service, you can use a xref:System.Windows.Forms.BindingSource component. This procedure is similar to binding a xref:System.Windows.Forms.BindingSource component to a type. You must create a client-side proxy that contains the methods and types exposed by the Web service. You generate a client-side proxy from the Web service (.asmx) itself, or its Web Services Description Language (WSDL) file. Additionally, your client-side proxy must expose the fields of complex types used by the Web service as public properties. You then bind the xref:System.Windows.Forms.BindingSource to one of the types exposed in the Web service proxy.
To create and bind to a client-side proxy
-
Create a Windows Form in the directory of your choice, with an appropriate namespace.
-
Add a xref:System.Windows.Forms.BindingSource component to the form.
-
Open the Windows Software Development Kit (SDK) command prompt, and navigate to the same directory that your form is located in.
-
Using the WSDL tool, enter
wsdland the URL for the .asmx or WSDL file for the Web service, followed by the namespace of your application, and optionally the language you are working in.The following code example uses the Web service located at
http://webservices.eraserver.net/zipcoderesolver/zipcoderesolver.asmx. For example, for C# typewsdl http://webservices.eraserver.net.zipcoderesolver/zipcoderesolver.asmx /n:BindToWebService, or for Visual Basic typewsdl http://webservices.eraserver.net.zipcoderesolver/zipcoderesolver.asmx /n:BindToWebService /language:VB. Passing the path as an argument to the WSDL tool will generate a client-side proxy in the same directory and namespace as your application, in the specified language. If you are using Visual Studio, add the file to your project. -
Select a type in the client-side proxy to bind to.
This is typically a type returned by a method offered by the Web service. The fields of the chosen type must be exposed as public properties for binding purposes.
[!code-cppSystem.Windows.Forms.DataConnectorWebService#4] [!code-csharpSystem.Windows.Forms.DataConnectorWebService#4] [!code-vbSystem.Windows.Forms.DataConnectorWebService#4]
-
Set the xref:System.Windows.Forms.BindingSource.DataSource%2A property of the xref:System.Windows.Forms.BindingSource to the type you want that is contained in the Web service client-side proxy.
[!code-cppSystem.Windows.Forms.DataConnectorWebService#2] [!code-csharpSystem.Windows.Forms.DataConnectorWebService#2] [!code-vbSystem.Windows.Forms.DataConnectorWebService#2]
To bind controls to the BindingSource that is bound to a Web service
-
Bind controls to the xref:System.Windows.Forms.BindingSource, passing the public property of the Web service type that you want as a parameter.
[!code-cppSystem.Windows.Forms.DataConnectorWebService#3] [!code-csharpSystem.Windows.Forms.DataConnectorWebService#3] [!code-vbSystem.Windows.Forms.DataConnectorWebService#3]
Example
The following code example demonstrates how to bind a xref:System.Windows.Forms.BindingSource component to a Web service, and then how to bind a text box to the xref:System.Windows.Forms.BindingSource component. When you click the button, a Web service method is called and the results will appear in textbox1.
[!code-cppSystem.Windows.Forms.DataConnectorWebService#1] [!code-csharpSystem.Windows.Forms.DataConnectorWebService#1] [!code-vbSystem.Windows.Forms.DataConnectorWebService#1]
Compiling the Code
This is a complete example that includes a Main method, and a shortened version of client-side proxy code.
This example requires:
- References to the System, System.Drawing, System.Web.Services, System.Windows.Forms and System.Xml assemblies.