Calculating date differences in InfoPath 2007 browser forms using C# code

Write C# code which uses the Site property of the XMLEventArgs object and the TimeSpan object in the Changed event of the date fields to calculate the difference between the dates in each row of a repeating table.

Problem

You have a repeating table that contains start and end date fields and you would like to calculate and display the difference in days between these fields.

Solution

Write C# code which uses the Site property of the XMLEventArgs object and the TimeSpan object in the Changed event of the date fields to calculate the difference between the dates in each row of the repeating table.

Note: You do not have to write C# code to calculate the difference between two date picker fields in InfoPath, but can use rules, formulas, and functions instead. For more information, see Calculating date differences in InfoPath using rules and formulas - no code!.

Discussion

You can accomplish this functionality as follows:

  1. Design an InfoPath browser-compatible form template as shown in figure 1.

    Figure 1. The InfoPath form template in design mode.

    with a Main data source that resembles the following figure:


    Figure 2. The Main data source of the form template.
  2. Select the startDate field. On the Tools menu, select Programming, and then click Changed Event to add a Changed event handler for the startDate field.
  3. Repeat the previous step for the endDate field.
  4. Put the following code in the Changed event handler for the startDate field:
    if (e.Operation == XmlOperation.ValueChange)
    {
      // Retrieve the values for the start and end dates
      string startDateValue = e.Site.Value;
      string endDateValue = e.Site.SelectSingleNode("../my:endDate",
        NamespaceManager).Value;

      if (!String.IsNullOrEmpty(startDateValue) &&
        !String.IsNullOrEmpty(endDateValue))
      {
        // Convert the date values to DateTime objects
        DateTime startDate = DateTime.ParseExact(startDateValue,
          "yyyy-MM-dd", null);
        DateTime endDate = DateTime.ParseExact(endDateValue,
          "yyyy-MM-dd", null);

        // Retrieve the days field node and remove its nil attribute
        XPathNavigator nodeNav = e.Site.SelectSingleNode("../my:days",
          NamespaceManager);
        if (nodeNav.MoveToAttribute("nil", NamespaceManager.LookupNamespace("xsi")))
          nodeNav.DeleteSelf();

        // Retrieve and display the days
        e.Site.SelectSingleNode("../my:days", NamespaceManager).SetValue(
          ((TimeSpan)endDate.Subtract(startDate)).Days.ToString());
      }
    }
  5. Put the following code in the Changed event handler for the endDate field:
    if (e.Operation == XmlOperation.ValueChange)
    {
      // Retrieve the values for the start and end dates
      string endDateValue = e.Site.Value;
      string startDateValue = e.Site.SelectSingleNode("../my:startDate",
        NamespaceManager).Value;

      if (!String.IsNullOrEmpty(startDateValue)
        && !String.IsNullOrEmpty(endDateValue))
      {
        // Convert the date values to DateTime objects
        DateTime startDate = DateTime.ParseExact(startDateValue,
          "yyyy-MM-dd", null);
        DateTime endDate = DateTime.ParseExact(endDateValue,
          "yyyy-MM-dd", null);

        // Retrieve the days field node and remove its nil attribute
        XPathNavigator nodeNav = e.Site.SelectSingleNode("../my:days",
          NamespaceManager);
        if (nodeNav.MoveToAttribute("nil", NamespaceManager.LookupNamespace("xsi")))
          nodeNav.DeleteSelf();

        // Retrieve and display the days
        e.Site.SelectSingleNode("../my:days", NamespaceManager).SetValue(
          ((TimeSpan)endDate.Subtract(startDate)).Days.ToString());
      }
    }
  6. Double-click the startDate field to open its Properties dialog box.
  7. On the Date Picker Properties dialog box, click the Browser forms tab.
  8. On the Browser forms tab, choose Always for the Postback settings.
  9. Repeat the previous 3 steps for the endDate field.
  10. Publish the form template to a SharePoint Server running Forms Services. Since the form template contains managed code, you will have to perform an administrator-approved InfoPath form template deployment.

You should now be able to enter dates and have the difference between the dates in each row of the repeating table calculated soon after a date has been entered.

 
 Subscribe for updates via RSS or Email

Related InfoPath Articles:

Copyright: This article may not be used on web sites (whether personal or otherwise), copied, disseminated, altered, printed, published, broadcasted, or reproduced in any way without an expressed written consent of S.Y.M. Wong-A-Ton. The techniques demonstrated in this article may be used within any Microsoft InfoPath project. This article is provided without any warranties. Copyright for this article is non-transferrable and remains with the author, S.Y.M. Wong-A-Ton.

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners

InfoPath 2013 Cookbook 2: 121 Codeless Recipes for SharePoint 2013

InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners

InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010

InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers

InfoPath 2010 Cookbook 4: 101 Code Recipes for VB Developers

InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services