Files
docs-desktop/dotnet-desktop-guide/framework/winforms/controls/how-to-bind-to-a-web-service-using-the-windows-forms-bindingsource.md
Andy De George c0ba284473 Initial winforms content migrated (#18)
* 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
2020-09-01 16:26:21 -07:00

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
csharp
vb
cpp
Web services [Windows Forms], binding controls
BindingSource component [Windows Forms], binding to a Web service
examples [Windows Forms], BindingSource component
controls [Windows Forms], binding to Web service
BindingSource component [Windows Forms], examples
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

  1. Create a Windows Form in the directory of your choice, with an appropriate namespace.

  2. Add a xref:System.Windows.Forms.BindingSource component to the form.

  3. Open the Windows Software Development Kit (SDK) command prompt, and navigate to the same directory that your form is located in.

  4. Using the WSDL tool, enter wsdl and 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# type wsdl http://webservices.eraserver.net.zipcoderesolver/zipcoderesolver.asmx /n:BindToWebService, or for Visual Basic type wsdl 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.

  5. 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]

  6. 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

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.

See also