Filtering, Viewing, and Adding Data in the Data Interop Developer Sample Form [InfoPath 2003 SDK Documentation]

Applies to:

Microsoft Office InfoPath 2003

For information on working with master/detail controls, see "About master/detail controls" in the InfoPath 2003 Help.

The Data Interop developer sample form uses multiple views to select, display, filter, add and update data. In Query view, the employee identifies himself or herself and calls up a list of his or her existing orders in Orders view. In Orders view, the salesperson can see more detail in Order Details, enter a new order in Create Order view, and generate an invoice that is displayed in read-only Invoice view. Sales summary reports are also available.

Querying for orders

The Data Interop developer sample form opens initially in Query view. A drop-down list box is filled with the employees of Northwind Traders from the Employees secondary data source. When the user makes a selection and clicks the Query button, the OnClick event procedure for the button confirms that the EmployeeID query field is not empty, then queries the database for the selected employee's orders and the corresponding order details by calling the Query method of the XDocument object.

Viewing and editing order details

In Orders view, the employee sees a list of his or her existing orders. A button is available in the leftmost column for each order to display Order Details view for the selected order. The OnClick event procedure for this button, btnShowOrderDetails, calls the ShowOrderDetails function with the OrderID of the current record. The ShowOrderDetails function switches to Order Details view, and the .xsl transform retrieves the Order ID from the gstrOrderID global variable to display the requested order record in a view rich with controls. The controls in Order Details view include drop-down list boxes containing the customers, employees, and products secondary data sources, as well as date picker controls and navigation buttons.

In Order Details view, the user can modify the order record, then submit changes to the database by clicking Submit and Print Invoice on the custom toolbar. This button's OnClick event procedure cautions the user that all new orders will be submitted. Then it calls the Submit method of the XDocument object, and finally the SwitchView method to display the invoice for the last order viewed.

Note  If the user deletes an order that contains related order details, an ADO error will occur upon submission. To avoid this error, enable the "Cascade delete related records" relationship property of the foreign key fields, OrderID and ProductID, in the Order Details table. By enabling this property, deleting will work as expected if the user has the required permissions for modifying tables in the SQL Northwind database.

In the Data Interop developer sample form, you can create a filter to find and return specific records. For example, you can locate all orders for a particular customer. In the Orders view, you will see only the orders meeting the criteria you specified. In the Details view, you can use navigation buttons to move through the records. If you do this, however, you will see all records, even ones that do not match the filter criteria you specified.

Creating and submitting a new order

The Northwind employee can create a new order by clicking Create new order in the custom task pane or Create Order on the custom toolbar and switching to Create Order view. The CreateOrder function sets up the new order using the following steps:

  1. Get the EmployeeID of the current user as the default.

    strEmployeeId = XDocument.DOM.selectSingleNode("/dfs:myFields/dfs:queryFields/q:Orders/@EmployeeID").text;
    
  2. Create a new XML order template node from the template file.

    var strNSPart1 = "xmlns:q=\"http://schemas.microsoft.com/office/infopath/2003/ado/queryFields\"";
    var strNSPart2 = " xmlns:d=\"http://schemas.microsoft.com/office/infopath/2003/ado/dataFields\"";
    var strNSPart3 = " xmlns:dfs=\"http://schemas.microsoft.com/office/infopath/2003/dataFormSolution\"";
    var strNSPart4 = " xmlns:my=\"http://schemas.microsoft.com/office/infopath/2003/myXSD/2003-02-22T00:54:56\"";
    var strNamespace = strNSPart1 + strNSPart2 + strNSPart3 + strNSPart4;
    domTemplate.setProperty("SelectionNamespaces", strNamespace);
    gobjNewOrderFragment = domTemplate.selectSingleNode("/dfs:myFields/dfs:dataFields/d:Orders");
    
  3. Generate a random unique OrderID to use until the order is saved to the database and a permanent OrderID is assigned.

    while (strOrderId = -1 * Math.floor(9998*Math.random()+1))
    {
        // Verify the new OrderId is unique amongst currently downloaded orders.
        if (XDocument.DOM.selectSingleNode("/dfs:myFields/dfs:dataFields/d:Orders[string(@OrderID)='" + strOrderId + "']") == null)
            break;
    }
    
  4. Add the new order fragment to the form's XML data document.

    objFragmentToInsert = gobjNewOrderFragment.cloneNode(true);
        objOrderIdNodes = objFragmentToInsert.selectNodes("//@OrderID");
        for (var i=0; i<objOrderIdNodes.length; i++)
            objOrderIdNodes.item(i).nodeTypedValue = strOrderId;
    XDocument.DOM.selectSingleNode("/dfs:myFields/dfs:dataFields").insertBefore(objFragmentToInsert, null);
    
  5. Switch to Create Order view. The following line of code calls the Calls the ShowOrderDetails function with the Order ID.

    ShowOrderDetails(strOrderId);
    

    The ShowOrderDetails function:

    function ShowOrderDetails(strOrderId)
    {
       var strTargetView = "Order Details";
    
       if (strOrderId)
       {
          // Filter the current view on the specified OrderId.
          gstrOrderId = strOrderId;
    
                // Set the intended view to the Create Order view
                // if the order ID is a temporary ID assigned by
                // CreateOrder().
          if (gstrOrderId < 0)
             strTargetView = "Create Order";
    
             if (XDocument.View.Name == strTargetView)
             {
                // Force the view contents to be updated.
                XDocument.View.ForceUpdate();
             }
             else
             {
                // Switch to order detail view.
                XDocument.View.SwitchView(strTargetView);
             }
       }
    }