Export (0) Print
Expand All

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 BindingSource component. This procedure is similar to binding a 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 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 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.

    	[System.SerializableAttribute, System.Xml.Serialization.XmlTypeAttribute(
    		Namespace="http://webservices.eraserver.net/")]
    	public class USPSAddress
    	{
    
    		private string streetField;
    
    		private string cityField;
    
    		private string stateField;
    
    		private string shortZIPField;
    
    		private string fullZIPField;
    
    
    		public string Street    
    		{
    			get
    			{
    				return this.streetField;
    			}
    			set
    			{
    				this.streetField = value;
    			}
    		}
    
    
    		public string City    
    		{
    			get
    			{
    				return this.cityField;
    			}
    			set
    			{
    				this.cityField = value;
    			}
    		}
    
    		public string State    
    		{
    			get
    			{
    				return this.stateField;
    			}
    			set
    			{
    				this.stateField = value;
    			}
    		}
    
    
    		public string ShortZIP    
    		{
    			get
    			{
    				return this.shortZIPField;
    			}
    			set
    			{
    				this.shortZIPField = value;
    			}
    		}
    
    
    		public string FullZIP    
    		{
    			get
    			{
    				return this.fullZIPField;
    			}
    			set
    			{
    				this.fullZIPField = value;
    			}
    		}
    	}
    
  6. Set the DataSource property of the BindingSource to the type you want that is contained in the Web service client-side proxy.

    			BindingSource1.DataSource = typeof(USPSAddress);
    

To bind controls to the BindingSource that is bound to a Web service

  • Bind controls to the BindingSource, passing the public property of the Web service type that you want as a parameter.

    			textBox1.DataBindings.Add("Text", this.BindingSource1, "FullZIP", true);
    

The following code example demonstrates how to bind a BindingSource component to a Web service, and then how to bind a text box to the BindingSource component. When you click the button, a Web service method is called and the results will appear in textbox1.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace BindToWebService {
	class Form1: Form

	{
		[STAThread]
		public static void Main()
		{
			Application.EnableVisualStyles();
			Application.Run(new Form1());
		}

		private BindingSource BindingSource1 = new BindingSource();
		private TextBox textBox1 = new TextBox();
		private TextBox textBox2 = new TextBox();
		private Button button1 = new Button();

		public Form1()
		{
			this.Load += new EventHandler(Form1_Load);
			textBox1.Location = new System.Drawing.Point(118, 131);
			textBox1.ReadOnly = true;
			button1.Location = new System.Drawing.Point(133, 60);
			button1.Click += new EventHandler(button1_Click);
			button1.Text = "Get zipcode";
			ClientSize = new System.Drawing.Size(292, 266);
			Controls.Add(this.button1);
			Controls.Add(this.textBox1);
		}

		private void button1_Click(object sender, EventArgs e)
		{

			textBox1.Text = "Calling Web service..";
			ZipCodeResolver resolver = new ZipCodeResolver();
			BindingSource1.Add(resolver.CorrectedAddressXml("0",
					"One Microsoft Way", "Redmond", "WA"));
						
		}

		public void Form1_Load(object sender, EventArgs e)
		{
			BindingSource1.DataSource = typeof(USPSAddress);
			textBox1.DataBindings.Add("Text", this.BindingSource1, "FullZIP", true);
		}
	}

	[System.Web.Services.WebServiceBindingAttribute(Name="ZipCodeResolverSoap",
		Namespace="http://webservices.eraserver.net/")]
	public class ZipCodeResolver:
		System.Web.Services.Protocols.SoapHttpClientProtocol

	{
		
		public ZipCodeResolver() : base()
		{        
			this.Url = 
				"http://webservices.eraserver.net/zipcoderesolver/zipcoderesolver.asmx";
		}

	
		//''<remarks/>
		[System.Web.Services.Protocols.SoapDocumentMethodAttribute
			("http://webservices.eraserver.net/CorrectedAddressXml", 
			RequestNamespace="http://webservices.eraserver.net/", 
			ResponseNamespace="http://webservices.eraserver.net/", 
			Use=System.Web.Services.Description.SoapBindingUse.Literal, 
			ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
		public USPSAddress CorrectedAddressXml(string accessCode, 
			string address, string city, string state)
		{
			object[] results = this.Invoke("CorrectedAddressXml", 
				new object[]{accessCode, address, city, state});
			return ((USPSAddress) results[0]);
		}

		//''<remarks/> 
		public System.IAsyncResult BeginCorrectedAddressXml(string accessCode,
			string address, string city, string state, 
			System.AsyncCallback callback, object asyncState)
		{

			return this.BeginInvoke("CorrectedAddressXml", 
				new object[]{accessCode, address, city, state}, callback, asyncState);
		}


		public USPSAddress EndCorrectedAddressXml(System.IAsyncResult asyncResult)
		{
			object[] results = this.EndInvoke(asyncResult);
			return ((USPSAddress) results[0]);
		}

	}


	[System.SerializableAttribute, System.Xml.Serialization.XmlTypeAttribute(
		Namespace="http://webservices.eraserver.net/")]
	public class USPSAddress
	{

		private string streetField;

		private string cityField;

		private string stateField;

		private string shortZIPField;

		private string fullZIPField;


		public string Street    
		{
			get
			{
				return this.streetField;
			}
			set
			{
				this.streetField = value;
			}
		}


		public string City    
		{
			get
			{
				return this.cityField;
			}
			set
			{
				this.cityField = value;
			}
		}

		public string State    
		{
			get
			{
				return this.stateField;
			}
			set
			{
				this.stateField = value;
			}
		}


		public string ShortZIP    
		{
			get
			{
				return this.shortZIPField;
			}
			set
			{
				this.shortZIPField = value;
			}
		}


		public string FullZIP    
		{
			get
			{
				return this.fullZIPField;
			}
			set
			{
				this.fullZIPField = value;
			}
		}
	}

	public delegate void CorrectedAddressXmlCompletedEventHandler(object sender,
		CorrectedAddressXmlCompletedEventArgs args);


	public class CorrectedAddressXmlCompletedEventArgs:
    System.ComponentModel.AsyncCompletedEventArgs

	{
		private object[] results;

		internal CorrectedAddressXmlCompletedEventArgs(object[] results,
			System.Exception exception, bool cancelled, object userState) :
			base(exception, cancelled, userState)
		{        
			this.results = results;
		}


		public USPSAddress Result    
		{
			get
			{
				this.RaiseExceptionIfNecessary();
				return ((USPSAddress) this.results[0]);
			}
		}
	}
 }

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.

For information about building this example from the command line for Visual Basic or Visual C#, see Building from the Command Line (Visual Basic) or Command-line Building With csc.exe. You can also build this example in Visual Studio by pasting the code into a new project. How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio
How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio
How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio
How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio
How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio

Show:
© 2014 Microsoft