Programmatically add an instance of a Repeating Section - InfoPath Dev
in

InfoPath Dev

Programmatically add an instance of a Repeating Section

Last post 11-23-2010 04:28 AM by greenawayr. 1 replies.
Page 1 of 1 (2 items)
Sort Posts: Previous Next
  • 11-22-2010 09:36 AM

    Programmatically add an instance of a Repeating Section

    Hi,

    I have a browser enabled form template based on a webservice that is connected to a SQL database.

    On my form I have a simple dropdown box, which when a certain value is selected requires a new part of the form to appear and be filled in.

    The easy way to do this seemed to be Conditional Formatting, however, because the form is connected to database via a webservice, the table that this part of the form relates to has no new row that relates to the rest of the form, therefore the section will not appear automatically. All I get is the link to "Click here to insert". This is the same for all the sections in our form, but for the sections that have been required to display at Form Load, we have added SQL triggers that inject the new row into the correct table and the form automatically displays the section with the Default Values entered in the table by SQL.

    Ideally I'd simply like to have a rule that when "ClientType" has a certain value selected, the new section appears all on it's own. Currently the link simply appears and disappears with the conditional formatting.

    Is there a way to programmatically add the section as if someone had just clicked the "Click here to insert" link? Failing this, is there a way to force a value into a field, so that when the Client Type is selected the section appears automatically?

    I have been looking at this approach which gave me this code

     

    Public Sub TempleType_Changed(ByVal sender As Object, ByVal e As XmlEventArgs)

                Dim ClientType As XPathNavigator = MainDataSource.CreateNavigator().SelectSingleNode("/dfs:myFields/dfs:dataFields/tns:GetFormDatasetResponse/tns:GetFormDatasetResult/ns1:dsForms/ns1:Clients/ns1:TempleType", NamespaceManager)
                Dim Username As XPathNavigator = MainDataSource.CreateNavigator().SelectSingleNode("/dfs:myFields/dfs:queryFields/tns:GetFormDataset/tns:pUserLoginId", NamespaceManager)

                ' Move to the root element of the document
                Dim root As XPathNavigator = Me.MainDataSource.CreateNavigator()

                root.MoveToFollowing(XPathNodeType.Element)

                ' Get an XPathNavigator pointing to the parent node where the repeating elements will be added

                Dim sectionNode As XPathNavigator = root.SelectSingleNode("/dfs:myFields/dfs:dataFields/tns:GetFormDatasetResponse/tns:GetFormDatasetResult/ns1:dsForms/ns1:SelfGeneratedCases", Me.NamespaceManager)

                Dim ns As String = sectionNode.NamespaceURI

                ' Add the XML tree for the child element

                Using writer As XmlWriter = sectionNode.AppendChild()
                    writer.WriteStartElement("my", "SelfGeneratedCases", ns)
                    writer.WriteElementString("my", "ChangeBy", ns, Username.Value)
                    writer.WriteEndElement()
                End Using
            End Sub

     

    But currently I am getting a NullReferenceException on this line

    Dim ns As String = sectionNode.NamespaceURI

     

    Any help anyone could give me on this is hugely appreciated as it's pretty urgent.

    Thanks

  • 11-23-2010 04:28 AM In reply to

    Re: Programmatically add an instance of a Repeating Section

    I found this thread, which sort of helps, but I keep getting NullReferenceExceptions and I think I know why, but am unsure how to overcome it!

    http://www.infopathdev.com/forums/t/12394.aspx?PageIndex=1

    My Datasource is structured as shown in the image attached. As you can see I have a group called (Choice), I presume this is a group that occurs due to the form being based on a webservice. However, if I right-click on it I cannot copy X-Path. Is this the cause for me not being able to write to any of the fields within the Repeating sections that exist within this group? If I copy the XPath for the group SelfGeneratedCases (which is the group that I want to exist when the ClientType dropdown is set to a certain value), then this is what I get

    /dfs:myFields/dfs:dataFields/tns:GetFormDatasetResponse/tns:GetFormDatasetResult/ns1:dsForms/ns1:SelfGeneratedCases

    However, if I try to add this is any piece of code, then when I debug I get a NullReferenceException.

    Any help on overcoming this is most appreciated.



Page 1 of 1 (2 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.