I am trying to use the DeleteRange method in my form to clear out a repeating table. The repeating table is nested in another repeating table, like this:
MyFields
gpBrands (group)
gpBrandDetails (repeating group)
gpContainer (group)
gpRepeating (repeating group)
There are lots of fields in gpBrandDetails and 9 fields in gpRepeating. I'm trying to clear out gpRepeating.
I have a drop down list that sets a product brand which then runs code to populate gpBrandDetails table from an XML file using the method described in this article:
http://www.infopathdev.com/blogs/hilary/archive/2008/07/04/how-do-i-copy-repeating-data-from-an-external-source-into-my-form.aspx
Each node created via the XML file includes three blank nodes in gpContainer (so three gpRepeatings with all associated fields within)
If I open my form and immediately use the button that runs this code it works just fine. However, if I set the product brand first and then try to clear out the table, I receive an error message:
"Value cannot be null. Parameter name: lastSiblingToDelete"
Here is the code:
XPathNavigator xn = this.MainDataSource.CreateNavigator();
XPathExpression xe = xn.Compile("/my:myFields/my:gpBrands/my:gpBrandDetails/my:gpContainer/my:gpRepeating");
XPathNodeIterator xi = xn.Select(xe);
XPathNavigator firstNode = xn.SelectSingleNode("/my:myFields/my:gpBrands/my:gpBrandDetails/my:gpContainer/my:gpRepeating[1]", this.NamespaceManager);
XPathNavigator lastNode = xn.SelectSingleNode("/my:myFields/my:gpBrands/my:gpBrandDetails/my:gpContainer/my:gpRepeating[" + xi.Count + "]", this.NamespaceManager);
firstNode.DeleteRange(lastNode);
When I step through the code, it shows a number for xi.Count, but yet I get the error message. I think that means it sees the rows.
I'm confused - what is the difference between the nodes created via code and the nodes created before the code is run? I've looked at the XML in notepad and one thing I notice is that the tags aren't quite the same. In the original (before the repeating table is populated via code) the nodes look like this:
<my:gpRepeating>
<my:dt_NextPRMilestone xsi:nil="true"></my:dt_NextPRMilestone>
<my:dt_milestoneEnd xsi:nil="true"></my:dt_milestoneEnd>
<my:dt_PRMilestoneComments></my:dt_PRMilestoneComments>
<my:dt_prmMarket></my:dt_prmMarket>
<my:dt_prmBrand></my:dt_prmBrand>
<my:gpRepeating>
whereas, when I use code, it looks like this:
<my:gpRepeating>
<my:dt_NextPRMilestone xsi:nil="true"/>
<my:dt_milestoneEnd xsi:nil="true"/>
<my:dt_PRMilestoneComments/>
<my:dt_prmMarket>Brazil</my:dt_prmMarket>
<my:dt_prmBrand>BrandName</my:dt_prmBrand>
</my:gpRepeating>
Could that be what's tripping it up? Any help would be so very appreciated!
Thanks,
Helen