Adding checkbox into repeating table programmatically (C#) - InfoPath Dev
in

InfoPath Dev

Adding checkbox into repeating table programmatically (C#)

Last post 11-19-2015 11:49 AM by Hilary Stoupa. 4 replies.
Page 1 of 1 (5 items)
Sort Posts: Previous Next
  • 11-19-2015 10:18 AM

    Adding checkbox into repeating table programmatically (C#)

    Hey Everyone,

    I have a form with a button that I want use to programmatically retrieve sql data, fill it into a sqldatareader object, which I use to populate a stringbuilder string, to finally append as a child to a repeating table element in infopath. The repeating table has five fields in the schema, four of which are text, and one of which is a boolean value. My code seems to work, but unfortunately, when I go to place a check in one of the checkboxes that populates due to this code, the box receives a red highlight telling me "Only 'true' or 'false' allowed".

    My code looks like the following:

    	    //reset all rows on the repeating table
                XPathNavigator mainDS = MainDataSource.CreateNavigator();
                StringBuilder sb = new StringBuilder();
                XPathNavigator firstRow = mainDS.SelectSingleNode("/my:myFields/my:updatePartyInfo/my:partyTable/my:group8/my:group9", NamespaceManager);
                XPathNavigator lastRow = mainDS.SelectSingleNode("/my:myFields/my:updatePartyInfo/my:partyTable/my:group8/my:group9[position() = last()]", NamespaceManager);
                if (firstRow != null && lastRow != null)
                    firstRow.DeleteRange(lastRow); //Make try/catch to ignore exception if called on a table that has already been cleared (i.e. first run retrieved no records)
    
                using (SqlCommand command = connection.CreateCommand())
                {
                    //command.CommandText = queryString;
                    command.CommandText = sqlQuery;
    
                    connection.Open();
    
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        int count = 1;
                        string my = NamespaceManager.LookupNamespace("my");
                        string xpath = "/my:myFields/my:updatePartyInfo/my:partyTable/my:group8/my:group9";
                        XPathNavigator group = MainDataSource.CreateNavigator().SelectSingleNode(xpath, NamespaceManager);
                        while (reader.Read())
                        {
                            string partyCode = reader["party_code"].ToString();
                            string rowId = count.ToString();
                            string contID = reader["event_code"].ToString();
                            string address = reader["full_addr"].ToString();
                            sb.Append("");
                            sb.Append("" + count.ToString() + "");
                            sb.Append("" + contID + "");
                            sb.Append("" + partyCode + "");
                            sb.Append("" + address + "");
                            sb.Append("true");
                            sb.Append("");
                            count++;
                        }
                    }
                    connection.Close();
    
                    sb = sb.Replace("&", "ampersand;"); //fields with ampersands throw schema validation
                    MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:updatePartyInfo/my:partyTable/my:group8", NamespaceManager).AppendChild(sb.ToString());

    I'll also attach as a text file, as I always have trouble getting the formatting to look good on this forum. The selectThisParty field is the checkbox field. In the example above, I'm giving it a value of true. I've tried "TRUE", "false", "FALSE" and leaving the value blank, which all produce errors - the uppercase and blank examples making the field surrounded by the red invalid markers right upon population, whereas the lowercase looks good, until you try and actually populate a check into one of the boxes - then that individual box is surrounded with the red markers.

    So my question is - is there something special I have to be doing here when inserting a checkbox versus a text field? I use the sb object compile all of the elements because I noted that large datasets were significantly faster this way - one appendchild() call, rather than multiple appendchildelements(). Does anyone have any ideas what I'm screwing up here? The frustrating thing is if I compare the InnerXML of the table when created by default, the selectThisParty is represented like so:

    "false"
    . My code produces the same thing, so it seems like it should work. I'd love to see if anyone has any pointers on what I'm doing wrong, or how I might fix. Thank you in advance for your time.
  • 11-19-2015 10:46 AM In reply to

    Re: Adding checkbox into repeating table programmatically (C#)

    Sorry - tried updating the post - the code stripped out some locations, and the innerxml I show also got stripped, and my edits weren't fixing it and I didn't want to keep editing the post unsuccessfully. I put the code in the attached txt file, as well as the innerxml example. Thank you again for any help you may have to offer.
  • 11-19-2015 10:55 AM In reply to

    Re: Adding checkbox into repeating table programmatically (C#)

    In your form, what is the data type for the field bound to the checkbox? If you right click the checkbox, what are the checked and unchecked values?
    Hilary Stoupa

  • 11-19-2015 11:24 AM In reply to

    Re: Adding checkbox into repeating table programmatically (C#)

    Nice, that was it. There was nothing in the checked/unchecked values on the control, and that was what was causing the issue. I'm not sure how it came to be that there was no value in there, but as soon as I filled those dropboxes in, the code now works. Thank you Hilary!
  • 11-19-2015 11:49 AM In reply to

    Re: Adding checkbox into repeating table programmatically (C#)

    Oh wow - didn't expect it to be that easy. :) Glad it is working for you!
    Hilary Stoupa

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