How to import rows to a repeating table in InfoPath

Currently i am developing an medical software. Therefor i use InfoPath form template with code behind c# and MS SQL or Infomix databse was a very comfortable solution. As the complexety of the system grows i had to broaden my horizon and find some tricky solutions to achieve the surreal requirements of the client. If you wish to import data from database (like MS SQL or Informix) to an InfoPath Form this method will help you to get the data in a repeating table. People always want everyhing flexibel, fast and automatically, properties which are very hard to reach within a software. So someday i encountered a very hard problem which took me a long time to solve it (although, afterwards it wasn't so difficult). If someone has programmed a little bit with c# before, its not that hard to get content of a database within c#, but its not so easy to process this data in a repeating table in InfoPath. So a solution had to be found to show the data from the database in InfoPath. After hours of internet surfing and testing many methods i found the best way to solve this problem.
1 answer

How to import rows to a repeating table in InfoPath

First we start to prepare the form:

1) Create a new infopath form
2) Rename the top element to “Root”
3) Add a “Repeating table” element with three columns to the infopath form
4) Name the group element “Details”
5) Name the repeating table element “RepTable”
6) Type a name for each element (”FieldA”, “FieldB”, “FieldC”)
7) In the repeating table properties within the tab “Data” do not allow the user to insert and delete rows. You must uncheck the checkbox (default is checked). We do that because we want to add automatically rows to the repeating table.

We want to load data to the repeating table when you click a button. It is your choice to make an on load event or react on some input to fill the table

1) Add a button to the form
2) On right click go to “Button properties”
3) Write on the label “Load”
4) Write as ID “LoadRepData”
5) Click edit form code. When you do that InfoPath will automatically add the necessary reference to the code behind file to react on the button
6) click event.

At this point we can start to code the InfoPath form. If you close the editor you can reach it if you click on the “Tools” menu “Programming” -> “Microsoft Visual Tools for Applications”.

Bind the navigator to the elements. Iterate the loop and fill the fields

XPathNavigator xPathNavi = MainDataSource.CreateNavigator();
XPathNavigator details = xPathNavi.SelectSingleNode(”/my:Root/my:Details”, NamespaceManager);
XPathNavigator repTable = details.SelectSingleNode(”my:RepTable”, NamespaceManager);
XPathNavigator node;
int counter = 0;
// iterate loop to fill table
for (int i=0; i<1; i++)
{
node = repTable.Clone();
node.SelectSingleNode(”my:FieldA”, NamespaceManager).SetValue(”FieldA - Row ” + i.ToString());
node.SelectSingleNode(”my:FieldB”, NamespaceManager).SetValue(”FieldB - Row ” + i.ToString());
node.SelectSingleNode(”my:FieldC”, NamespaceManager).SetValue(”FieldC - Row ” + i.ToString());
details.AppendChild(node);
counter += 1;
}

Because that we clone the row we need to remove the last row:

// delete a specific range of nodes
XPathNodeIterator rows = details.Select(”my:RepTable”, NamespaceManager);
XPathNavigator first = details.SelectSingleNode(”my:RepTable[1]”, NamespaceManager);
XPathNavigator second = details.SelectSingleNode(”my:RepTable[” + Convert.ToString(rows.Count - counter) + “]”, NamespaceManager);
first.DeleteRange(second);