DeleteRange keeps saying the last node is null, but it's not - InfoPath Dev
in

InfoPath Dev

Qdabra® Training

Use our Google Custom Search for best site search results.

DeleteRange keeps saying the last node is null, but it's not

Last post 08-29-2013 09:20 AM by Jimmy. 6 replies.
Page 1 of 1 (7 items)
Sort Posts: Previous Next
  • 08-22-2013 09:12 AM

    DeleteRange keeps saying the last node is null, but it's not

    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

  • 08-24-2013 06:13 PM In reply to

    Re: DeleteRange keeps saying the last node is null, but it's not

    Hi Helen,

    Why not remove the code and add qRules and use the Delete command?

    Benefits of qRules:

    • common library for all forms
    • data-driven, so when data source changes, code won't break
    • reduces maintenance cost
    • piece-of-cake easy
    • cheap or free

    Free trial is here: http://www.qdabra.com/en/products/qRules_new.aspx. After you install, go to qRules command document and search for Delete. Lots of examples.

    It's inexpensive, but you can also get it for free: just get it working and send us a paragraph describing your solution and allow us to tape a <5 minute video in which you demo the solution.

    Good luck!

    PS> we stopped providing code support in this forum and substituted qRules instead because it was taking us too long to answer every specific coding request. We built the 100+ frequently asked coding questions into qRules to simplify. It's best practice for you because it reduces your cost of form maintenance (data-driven, no visual studio required, common library for all templates, quarterly updates, etc.)

    Patrick Halstead
    Project Manager at Qdabra
  • 08-26-2013 05:13 AM In reply to

    Re: DeleteRange keeps saying the last node is null, but it's not

    Hi Patrick,

    Thanks for the reply.  Bummer that you're not providing code support anymore.  Or at least for me it is...I suppose it makes your job a little easier.  :)

    Unfortunately, I read through the documentation and it is clear that I won't be able to install it due to restrictions imposed by my company. I  can't have SQL on my local machine and I have no access to the servers.

    I will have to find help elsewhere...

    Thanks anyhow,

    Helen

  • 08-26-2013 11:38 AM In reply to

    Re: DeleteRange keeps saying the last node is null, but it's not

    Thanks Helen for requesting support.

    We're scheduled to talk tomorrow I believe.

    If you can send us your XSN we can look at the issue tonight.

    Cheers,

    Patrick Halstead
    Project Manager at Qdabra
  • 08-26-2013 11:49 AM In reply to

    Re: DeleteRange keeps saying the last node is null, but it's not

    Ok, so now the truth is about to come out here.   :-)

    I'm really trying to implement a sort of the inner repeating table using the code I found here:  http://blogs.msdn.com/b/infopath/archive/2006/12/15/another-way-of-sorting-repeating-data.aspx

    It works fine if i have one row in the outer repeating table, but when I have more than one row in the outer repeating table, that's where I fail.  I've been playing with it and have verified that it works for just one row. 

    I'm happy to send you the xsn, but I'd prefer to not post it on the site.  Can you advise where to send it?

     Also, I believe we're actually scheduled for Thursday of this week, not Tuesday.  Please correct me if I'm wrong.

    Thanks much!

  • 08-26-2013 04:31 PM In reply to

    Re: DeleteRange keeps saying the last node is null, but it's not

    I just emailed you. You can send your XSN to support (att) Qdabra (dott) com.

    :)

    Patrick Halstead
    Project Manager at Qdabra
  • 08-29-2013 09:20 AM In reply to

    Re: DeleteRange keeps saying the last node is null, but it's not

     Hello Helen,

     You are running into this error due to one of the subtleties of XPath that trips people up from time to time. The expression:

     /my:myFields/my:gpBrands/my:gpBrandDetails/my:gpContainer/my:gpRepeating[{number}]

    means "select the {number}th gpRepeating element under its respective parent element". But in this case the {number} being used is xi.Count which is the total number of gpRepeating elements in the document. So what happns when you have gpRepeating elements split across more than one gpBrandDetails is that xi.Count is greater than any number that will find a matching node, and resultingly lastNode is null.

    This approach is also problematic because DeleteRange() can only be used to delete sibling nodes, and the nodes in this situation are not all siblings.

    An alternative, and somewhat cleaner, way to delete all the nodes with a certain path is to do this:

                 List<XPathNavigator> toDelete = new List<XPathNavigator>();
                foreach (XPathNavigator nav in CreateNavigator().Select("/my:myFields/my:gpBrands/my:gpBrandDetails/my:gpContainer/my:gpRepeating", NamespaceManager))
                {
                    toDelete.Add(nav);
                }
                foreach (XPathNavigator nav in toDelete)
                {
                    nav.DeleteSelf();
                }

    This should allow you to delete all of the gpRepeating elements as needed, but I am somewhat doubtful that this will completely fix your troubles. How precisely do you envision the sorting working in this case? Do you want to be able to sort each of the gpRepeating tables separately? Or should they all be sorted the same way at once?

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
Page 1 of 1 (7 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.