September 17, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Dynamically Data-bind in InfoPath

  • September 24, 2003
  • By Thiru Thangarathinam
  • Send Email »
  • More Articles »

In Part 1 of this article series, we looked at some of the powerful and compelling features of InfoPath 2003. In this installment, we will see how to create a dynamic data-bound InfoPath from an ASP.NET Web Service. We will also see how to customize the task pane by adding custom content that is relevant to the form that the user is filling out.

Creating a Data-Bound InfoPath Using an ASP.NET Web Service as the Data Source

In this section, we will understand how to construct a dynamic, data-bound InfoPath form using an ASP.NET Web Service as the data source. Before we move on to consuming a Web Service from an InfoPath form, let us create an ASP.NET Web Service, which can then be used from within an InfoPath form.

Creating an ASP.NET Web Service

Let us start by creating a Web Service named InfoPathWebServiceExample by selecting Visual C# ASP.NET Web Service as the project template in the New Project dialog box. Once the project is created, rename the default service from Service1 to AuthorsService. To the AuthorsService, add the following two methods:

[WebMethod]
public XmlDocument GetAuthorByAuthorID(string authorID)
{
  XmlDocument doc = new XmlDocument();
  DataSet authorsDataSet = new DataSet("Authors");
  using (SqlConnection conn = new
SqlConnection("server=localhost;uid=sa;pwd=thiru;database=Pubs"))
  {
    SqlDataAdapter adapter = new SqlDataAdapter("Select au_id,
                             au_lname, au_fname,
                             phone from authors
                             Where au_id = '" +
                             authorID + "'" ,conn);
    adapter.Fill(authorsDataSet,"Author");
  }
  doc.LoadXml(authorsDataSet.GetXml());
  return doc;
}

[WebMethod]
public XmlDocument GetAllAuthors()
{
  XmlDocument doc = new XmlDocument();
  DataSet authorsDataSet = new DataSet("Authors");
  using (SqlConnection conn = new
         SqlConnection("server=localhost;uid=sa;pwd=thiru;
                        database=Pubs"))
  {
    SqlDataAdapter adapter = new SqlDataAdapter("Select au_id,
                             au_lname, au_fname,
                             phone from authors " ,conn);
adapter.Fill(authorsDataSet,"Author");
  }
  doc.LoadXml(authorsDataSet.GetXml());
  return doc;
}

The above methods are very similar except for the difference that the GetAuthorByAuthorID method takes an authorID as an argument and returns information about that particular author. The GetAllAuthors method returns all the authors present in the authors table in the pubs database. If you navigate to the AuthorsService.asmx file from the browser and invoke the GetAllAuthors method using the default test harness provided by ASP.NET, you will get the following output.



Click here for a larger image.

Creating the InfoPath form

Now that we have constructed the Web Service, let us create the InfoPath form. To create the form, go through the following steps.

  1. Start by selecting File->Design a Form from the menu.
  2. In the Design a new form task pane, click on New from Data Source.
  3. Select Web Service as the type of the data source in the first screen of the Data Source Setup Wizard. Clicking on Next in the first screen results in the following screen.
  4. In the above screen, you must indicate how the form will interact with the Web Service, indicating options such as whether it receives and submits data to the service. You can build a form that only sends data, one that only receives data, or one that does both. If you choose Receive and submit data in the above screen, InfoPath will create two views for your form: one view for submitting data to the service and another view for the data returned from the service. In this case, the data source view also contains two groups of fields. The queryFields contain the data that needs to be supplied when invoking the service. The dataFields contain the data returned from the Web Service. For the purposes of this article, let us select Receive data option in the above screen. Clicking Next on the above screen results in the following screen, where you need to specify the location of your Web Service.
  5. In the above screen, enter the location of the Web Service and click Next.
  6. From the location of the Web Service specified, InfoPath extracts the WSDL definition and displays the available Web Service methods.
  7. In the above screen, select the GetAllAuthors method from the list and click Next to complete the wizard.
  8. After that, open up the Data Entry View from the task pane and then click on Data Source.
  9. In the Data Source treeview, open up the dataFields node and select the Authors node. Right-click on the Authors node and select the Section with Controls option from the context menu.
  10. Now, navigate back to the Query view and then click on the Preview Form option from the toolbar.
  11. In the Preview mode, if you click on the Run Query button, you will see the following output being displayed.


  12. Click here for a larger image.

    The above output is retrieved from the GetAllAuthors Web Service method call that returns details about all the authors.





Page 1 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel