One of the powerful features of InfoPath, especially
when it comes to generating browser-based forms, is the ability to
connect to additional data sources. We can reuse our earlier
Demonstration Equipment Request form to see this in action.
Open
the EquipmentRequest.xsn file from the local file system. Our form has
two sections: the top section captures customer details and the bottom
section captures a list of products. Rather than manually keying in
customer details, we’ll make use of a SQL Server database to look up
the required information and automatically populate the fields. Tip
For
demonstration purposes, SQL connections are easiest to use. However,
their usefulness within InfoPath forms is pretty limited. Passing
parameters generally requires custom code, as you’ll see later. As a
general rule, the best way to communicate with external data sources is
to create a custom web service interface. InfoPath can then parse the
Web Service Definition Language (WSDL) and create fields for any
parameters that may be required.
To provide some sample data to work within
our various examples, we need to download and install the SQL Server
2008 sample databases, which can be found at www.codeplex.com/MSFTDBProdSamples. Our examples make use of the AdventureWorksLT database installed on the local instance of SQL Server 2008. At
the bottom of the Customer Details section, add a button. Buttons and
other controls can be found in the Controls section of the Home ribbon.
Type the button label as Find Customer. To add a data connection, switch to the Data tab on the ribbon, and then click From Other Sources | From Database, as shown: Click
Select Database to select from the data sources that are available on
the current machine. If one is not available for the AdventureWorksLT
database, click New Source to add one. Select the Customer table when
prompted. Once the data connection has been made, the columns will be
listed, as shown next: Since we want to use a query to extract data from more than one table, click Edit SQL and enter the following SQL statement: Select C.CustomerID, C.CompanyName, A.AddressLine1, A.AddressLine2, A.City, A.StateProvince, A.PostalCode From SalesLT.Customer as C Inner Join SalesLT.CustomerAddress as CA On CA.CustomerID=C.CustomerID Inner Join SalesLT.Address as A On A.AddressID=CA.AddressID
Click
Next. Leave the Store A Copy Of The Data In The Form Template checkbox
unchecked. Click Next and then uncheck the Automatically Retrieve Data
When Form Is Opened checkbox. Set the connection name to Customer and then click Finish to create the connection. We’ll
make use of the Company Name field to search for customers. All other
fields will be populated automatically from the search results. To make
all other fields read-only, select the field and then from the
Properties tab of the ribbon, check the Read-Only option in the Modify
section. Repeat this process for all fields in the Customer Details
section other than the Company Name field. We’ll display our search results on a separate page. From the Page Design tab’s Views section, select New. Name the new view Customer Search Results. Type the title of the new view as Customer Search
and then, from the Fields pane, in the Fields drop-down, select
Customer (Secondary). (The Fields drop-down is something of a misnomer.
In reality, the drop-down contains a list of the data connections that
are available to the current form.) Drag
the d:Customer repeating group onto the Customer Search form. Select
Repeating Table as the control type. When producing a schema from a SQL
statement, InfoPath also adds columns for both sides of a join
relationship. As a result, three redundant columns are named
CustomerID1, AddressID, and AddressID1. To remove these from our
repeating table, simply select the offending columns and choose Delete
| Columns from the Layout tab. Since
we don’t want the user to be able to edit the contents of these fields,
using the Change Control button on the Properties menu that we
demonstrated earlier, change the controls to Calculated Values. The
final item that we need to add to our search form is a button to select
the correct customer. Place the cursor in the first column of the table
and then, from the Table tab in the ribbon, click Insert Left. Add a button in the data area of the new column and type the label as Select. The completed form should look like this:
|