Setting Field Values in a New Row of a Repeating Table - InfoPath Dev
in

InfoPath Dev

Setting Field Values in a New Row of a Repeating Table

Last post 11-13-2011 07:04 AM by Jimmy. 6 replies.
Page 1 of 1 (7 items)
Sort Posts: Previous Next
  • 11-11-2011 06:37 PM

    Setting Field Values in a New Row of a Repeating Table

    Purpose: User clicks Button "Augmentin"; code then populates 4 fields in a new row of repeating table.

    Paradigm: 1. Add a new row.; then count the number of rows and position navigator on last (new) row. Then add field values

    Here is the code:

    Public Sub btnAugmentin_Clicked(ByVal sender As Object, ByVal e As ClickedEventArgs)
                ' Write your code here.

    'add a new row....
                CurrentView.ExecuteAction(ActionType.XCollectionInsert, "group96_294")
             
                Dim i As Integer
                Dim RXnav As XPathNavigator = MainDataSource.CreateNavigator()
                'create a navigator to iterate through the table
                Dim RxRows As XPathNodeIterator = RXnav.Select("/my:myFields/my:Exam/my:grpRXHeader/my:grpRX", NamespaceManager)
                'get a count of rows
                i = 0
                While RxRows.MoveNext()
                    i = i + 1
                End While
             
                'position on the last row of the table
                Dim RxRow As XPathNavigator = RXnav.SelectSingleNode("/my:myFields/my:Exam/my:grpRXHeader/my:grpRX[ " & i.ToString & "]", NamespaceManager)
                'get a reference to the field "cboDrug"
                Dim Rxfield As XPathNavigator = RxRow.SelectSingleNode("./my:cboDrug", NamespaceManager)
                'set the value of the field
                Rxfield.SetValue("Augmentin")

    '..............so far so good..........................
    'now set the value of field "cboRxquantity" to 20
                Rxfield = RxRow.SelectSingleNode("./my:cboRXQuantity", NamespaceManager)

    'code fails at next line....reference is invalid
                Rxfield.SetValue("20")

    Issues are: how to all the other fields on the new row

    other issue: how to use the  "addchild" method to add a row rather than the XcollectionInsert

    Thanks in advance to anyone who answers!!

    Adam

  • 11-12-2011 03:09 AM In reply to

    Re: Setting Field Values in a New Row of a Repeating Table

    First, this is not related to your issue, but this is completely unnecessary:

                'create a navigator to iterate through the table
                Dim RxRows As XPathNodeIterator = RXnav.Select("/my:myFields/my:Exam/my:grpRXHeader/my:grpRX", NamespaceManager)
                'get a count of rows
                i = 0
                While RxRows.MoveNext()
                    i = i + 1
                End While

    Just use [last()] in your XPath instead of all that.

     As to your issue, there are two possible reasons you would be getting a null reference exception: either the field is missing for some reason, or you spelled its name wrong.  The latter is far more likely in this case and since you spelled it cboRxquantity in your comment, and then cboRXQuantity in the code, I'm going to guess that's almost definitely the issue.

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 11-12-2011 06:56 AM In reply to

    Re: Setting Field Values in a New Row of a Repeating Table

     Thanks for the reply and suggestion

    the spelling/case issue you pointed out is NOT the source of the problem.

     --

  • 11-12-2011 07:26 AM In reply to

    Re: Setting Field Values in a New Row of a Repeating Table

     So I solved the problem (partly)

    the Combobox was set to type "integer" rather than "text".

    Once I changed that all is good.

    Now my next question is...can you set the value of a type combbox where input is restricted to 'integer' values.

    the 'setvalue' method only accepts strings.

    There must be a logical way to set the value of a field type integer.

     

    Thanks in advance ....

  • 11-12-2011 08:11 PM In reply to

    Re: Setting Field Values in a New Row of a Repeating Table

    If changing the datatype from integer to text fixed the issue, then most likely the problem was that there was an xsi:nil attribute on the field that needed to be removed first.  Searching this forum for xsi:nil will get you a lot of info on that.  The only thing strange about that is that I wouldn't expect that to cause a "reference is invalid" error.  Is that really what the error said?

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 11-13-2011 05:28 AM In reply to

    Re: Setting Field Values in a New Row of a Repeating Table

     You are correct.

    Rxfield = RxRow.SelectSingleNode("./my:cboRXQuantity", NamespaceManager)
                Rxfield.MoveToAttribute("nil", "http://www.w3.org/2001/XMLSchema-instance")
                Rxfield.DeleteSelf()
                Rxfield.SetValue("20")

    ...that fixed the issue.( I can't believe MSFT let this kind of glitch slide by in infopath..this really shouldn't require all this code to set a value).

     

    Thanks 

    Adam

  • 11-13-2011 07:04 AM In reply to

    Re: Setting Field Values in a New Row of a Repeating Table

    It's not really a glitch at all.  The xsi:nil attribute has to be there when the field is created in order for it to be valid.  It's a schema violation for a numeric field to be blank unless it has an xsi:nil attribute.  But it's also a schema violation for a field to have a value and an xsi:nil attribute at the same time, hence that error.

     At any rate, we discourage the practice of writing custom code for InfoPath forms, and I think you're starting to find out why.  If you'd like an easier time, try out qRules.  It can do almost everything people want InfoPath code to do, and won't require you to write a single line of code.

    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.