Insert & Remove an optional section - InfoPath Dev
in

InfoPath Dev

Insert & Remove an optional section

Last post 04-27-2006 05:28 PM by SamDahl. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 07-28-2005 02:39 PM

    Insert & Remove an optional section

    I am using the ExecuteAction to insert an optional section, if a specific value is selected in a drop down menu (note the optional section is in a repeated section). First time the drop down value matches it inserts the optional section with out a problem but if I remove the section and then try to reselect the value in the drop down menu it gives me the following error

    "The attempted fragment operation cannot be completed because there is no valid insertion point for the fragment."

    and then it still inserts the section....How can I get rid of this error...

    Code Below

    <InfoPathEventHandler(MatchPath:="/mstns:SHORT_SEARCH/mstns:INSTRUMENT/mstns:INST_NAME", EventType:=InfoPathEventType.OnAfterChange)> _
    Public Sub INST_NAME_OnAfterChange(ByVal e As DataDOMEvent)

    If thisXDocument.DOM.getElementsByTagName("mstns:INST_NAME").Item(0).text = "DEED" Then
    thisXDocument.View.ExecuteAction("xOptional::insert", "DEED_30")
    End If
    End Sub
  • 04-25-2006 11:33 PM In reply to

    • SamDahl
    • Top 500 Contributor
    • Joined on 03-28-2006
    • Australia
    • Posts 44

    Re: Insert &amp; Remove an optional section

    I'm having the same error with fundamentally the same situation.

    I have a DDL called deviceType.
    I have multiple optional sections relevant to different devices.
    (for this test I'll use 2)

    When the user selects the device type I want the relevant optional section to open.

    I've used the following code:
    (note the 4 alert messages to track the flow of operation)

    function msoxd_my_deviceType::OnAfterChange(eventObj)
    {
    // Write code here to restore the global state.
    try
    {

    if (eventObj.IsUndoRedo)
    {
    // An undo or redo operation has occurred and the DOM is read-only.
    return;
    }

    XDocument.UI.Alert("Function Test");

    var typeValue = XDocument.DOM.selectSingleNode("/my:myFields/my:group4/my:deviceType");

    if (typeValue.text == "Switch")
    {
    try
    {
    XDocument.UI.Alert("Switch Try");
    var view = eventObj.XDocument.View;
    view.ExecuteAction("xOptional::insert","group32_54");
    }
    catch(exception)
    {
    XDocument.UI.Alert("Switch Error");
    }
    }

    if (typeValue.text == "Server")
    {
    try
    {
    XDocument.UI.Alert("Server Try");
    var view = eventObj.XDocument.View;
    view.ExecuteAction("xOptional::insert","group55_69");
    }
    catch(exception)
    {
    XDocument.UI.Alert("Server Error");
    }
    }

    }
    catch(exception)
    {
    XDocument.UI.Alert("Function Error");
    }
    }

    This works perfectly for the initial selection.

    You select switch in the DDL and the switch section opens. No problem.
    Alerts seen:
    "Function Test"
    "Switch Try"

    If I then select Server in the DDL, I get
    "Function Test"
    "Server Try"
    "Function Test"
    "Server Try"
    Infopath error: "InfoPath cannot insert the section because of an error"
    ->Details: "The attempted fragment operation cannot be completed because there is no valid insertion point for the fragment."
    [the Server optional section opens]

    (note the function appears to run twice: succeed once and then fail. The onAfterChange event triggers twice? once when Switch is deselected and again when Server is selected? if this is the case how would I handle this?)

    Some further scenarios I tested:

    If I then select Switch in the DDL I get:
    "Function Test"
    "Switch Try"
    "Switch Error"
    "Function Test"
    "Switch Try"
    "Switch Error"
    (this is acceptable: won't be noticed when alerts are removed)

    If I then delete the Server section and then choose Server in the DDL I get:
    "Function Test"
    "Server Try"
    "Function Test"
    "Server Try"
    Infopath error: "InfoPath cannot insert the section because of an error"
    [the Server optional section opens]

    Any ideas how to stop this error occurring?
  • 04-27-2006 05:28 PM In reply to

    • SamDahl
    • Top 500 Contributor
    • Joined on 03-28-2006
    • Australia
    • Posts 44

    Re: Insert &amp; Remove an optional section

    Solution achieved! (quite proud of that as a second year uni student on work placement)

    Effectively what I'm doing is testing whether a variable inside the optional section is there (i'e the section is inserted), I used a hidden text field with default text and a UI.Alert to display it. If the section is not inserted it throws an error and I catch and insert the section.
    Since I didn't want a UI.Alert all the time I changed it to storing a variable for no better reason than to see if I can. Surely there's a better way to do that?

    here's my code:
    (I really should comment my code)

    function msoxd_my_deviceType::OnAfterChange(eventObj)
    {
    // Write code here to restore the global state.


    if (eventObj.IsUndoRedo)
    {
    // An undo or redo operation has occurred and the DOM is read-only.
    return;
    }

    var serverSection;
    var switchSection;
    var switchTest;
    var serverTest;
    var typeValue = XDocument.DOM.selectSingleNode("/my:myFields/my:group4/my:deviceType");

    if (typeValue.text == "Switch")
    {
    try
    {
    switchSection = XDocument.DOM.selectSingleNode("//*[local-name()='isSwitchesOption']");
    switchTest = switchSection.text;
    }
    catch(exception)
    {
    eventObj.XDocument.View.ExecuteAction("xOptional::insert","group32_54");
    }
    }

    if (typeValue.text == "Server")
    {
    try
    {
    serverSection = XDocument.DOM.selectSingleNode("//*[local-name()='isServerOption']");
    serverTest = serverSection.text;
    }
    catch(exception)
    {
    eventObj.XDocument.View.ExecuteAction("xOptional::insert","group55_69");
    }
    }
    }
Page 1 of 1 (3 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.