Date Calculation in a Repeating Table - InfoPath Dev
in

InfoPath Dev

Date Calculation in a Repeating Table

Last post 10-19-2005 06:13 PM by whapakk. 4 replies.
Page 1 of 1 (5 items)
Sort Posts: Previous Next
  • 10-17-2005 05:48 AM

    • whapakk
    • Not Ranked
    • Joined on 09-23-2005
    • Philippines
    • Posts 22

    Date Calculation in a Repeating Table

    Hi, I have a repeating section that has a date picker for the 'Start' and 'End' dates. It also has a field that computes for the difference in days between the two dates. I found an article that shows how to do date calculations at this site,

    http://blogs.msdn.com/infopath/archive/2005/01/25/360318.aspx

    It works for fields that are not repeating. But for my repeating table, nothing happens to the 'ManDays' field. Even after trying relative paths. Here's a snippet of my code. Using managed code,

    // Date calculations for man days
    private static DateTime Iso8601ToDate( string iso8601Date )
    {
    if( iso8601Date == null )
    throw new ArgumentNullException( "iso8601Date" );
    return DateTime.ParseExact( iso8601Date, "yyyy-MM-dd", null );
    }
    private static string DateToIso8601( DateTime dateTime )
    {
    return dateTime.ToString( "yyyy-MM-dd" );
    }


    [InfoPathEventHandler(MatchPath="/my:ServiceOrder/my:ResourceRequirementDetails/my:Items/my:ResourceStartDate", EventType=InfoPathEventType.OnAfterChange)]
    public void ResourceStartDate_OnAfterChange(DataDOMEvent e)
    {

    if (e.IsUndoRedo)
    {
    return;

    }

    SyncDifference();
    }

    private void SyncDifference()

    {
    IXMLDOMNode date1Node = thisXDocument.DOM.selectSingleNode( "../my:ResourceStartDate" );
    IXMLDOMNode date2Node = thisXDocument.DOM.selectSingleNode( "../my:ResourceReleaseDate" );
    IXMLDOMNode diffNode = thisXDocument.DOM.selectSingleNode( "../my:ManDays" );

    if( date1Node != null && date2Node != null && diffNode != null )
    {
    try
    {
    DateTime dt1 = Iso8601ToDate( date1Node.text );
    DateTime dt2 = Iso8601ToDate( date2Node.text );

    TimeSpan ts = dt2 - dt1;
    diffNode.text = ts.Days.ToString();
    }
    catch( FormatException ) {}
    }
    }

    // The following function handler is created by Microsoft Office InfoPath. Do not
    // modify the type or number of arguments.
    [InfoPathEventHandler(MatchPath="/my:ServiceOrder/my:ResourceRequirementDetails/my:Items/my:ResourceReleaseDate", EventType=InfoPathEventType.OnAfterChange)]
    public void ResourceReleaseDate_OnAfterChange(DataDOMEvent e)
    {
    if (e.IsUndoRedo)
    {
    return;
    }

    SyncDifference();
    }



    I also tried "../FieldName" and "//FieldName" but it doesn't work also.
    Thanks.

    quote:
    Practice makes perfect, but then, nobody's perfect. So, why practice?
  • 10-17-2005 06:07 AM In reply to

    Re: Date Calculation in a Repeating Table

    Hi,

    For repeating sections, pass you event e to SyncDifference() and to get to date1Node, date2Node and diffNode, use: e.Source.selectSingleNode("my:fieldName");

    Hope that it heps.

    Regards,

    Franck Dauché
  • 10-17-2005 06:18 AM In reply to

    • whapakk
    • Not Ranked
    • Joined on 09-23-2005
    • Philippines
    • Posts 22

    Re: Date Calculation in a Repeating Table

    Hi Franck!

    I already changed my code to what you suggested, but the field still doesn't change. I might be doing something wrong. Here's what I did

    private void SyncDifference(DataDOMEvent e)

    {
    IXMLDOMNode date1Node = e.Source.selectSingleNode( "my:ResourceStartDate" );
    IXMLDOMNode date2Node = e.Source.selectSingleNode( "my:ResourceReleaseDate" );
    IXMLDOMNode diffNode = e.Source.selectSingleNode( "my:ManDays" );

    if( date1Node != null && date2Node != null && diffNode != null )
    {
    try
    {
    DateTime dt1 = Iso8601ToDate( date1Node.text );
    DateTime dt2 = Iso8601ToDate( date2Node.text );

    TimeSpan ts = dt2 - dt1;
    diffNode.text = ts.Days.ToString();
    }
    catch( FormatException ) {}
    }
    }


    private static DateTime Iso8601ToDate( string iso8601Date )
    {
    if( iso8601Date == null )
    throw new ArgumentNullException( "iso8601Date" );
    return DateTime.ParseExact( iso8601Date, "yyyy-MM-dd", null );
    }
    private static string DateToIso8601( DateTime dateTime )
    {
    return dateTime.ToString( "yyyy-MM-dd" );
    }


    [InfoPathEventHandler(MatchPath="/my:ServiceOrder/my:ResourceRequirementDetails/my:Items/my:ResourceStartDate", EventType=InfoPathEventType.OnAfterChange)]
    public void ResourceStartDate_OnAfterChange(DataDOMEvent e)
    {

    if (e.IsUndoRedo)
    {
    return;

    }
    SyncDifference(e);
    }


    [InfoPathEventHandler(MatchPath="/my:ServiceOrder/my:ResourceRequirementDetails/my:Items/my:ResourceReleaseDate", EventType=InfoPathEventType.OnAfterChange)]
    public void ResourceReleaseDate_OnAfterChange(DataDOMEvent e)
    {
    if (e.IsUndoRedo)
    {
    return;
    }

    SyncDifference(e);
    }

    quote:
    Practice makes perfect, but then, nobody's perfect. So, why practice?
  • 10-17-2005 07:22 AM In reply to

    Re: Date Calculation in a Repeating Table

    Sorry, read your post too fast..

    Try in your SyncDifference:
    IXMLDOMNode oN = e.Site.selectSingleNode( ".." );
    IXMLDOMNode date1Node = oN.selectSingleNode( "my:ResourceStartDate" );
    IXMLDOMNode date2Node = oN.selectSingleNode( "my:ResourceReleaseDate" );
    IXMLDOMNode diffNode = oN.selectSingleNode( "my:ManDays" );

    Franck Dauché
  • 10-19-2005 06:13 PM In reply to

    • whapakk
    • Not Ranked
    • Joined on 09-23-2005
    • Philippines
    • Posts 22

    Re: Date Calculation in a Repeating Table

    Nice! Works great, thanks Franck!

    quote:
    Practice makes perfect, but then, nobody's perfect. So, why practice?
Page 1 of 1 (5 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.