Insert text in cell- first entry outside of table

Hi, I am using the following code to solve a problem. I have an unpredictable number of FieldSites. The tables are made of 7 rows of address and contact data. I can only have 5 columns per table so that they look good on a page.

Because of the way in which I have to access the data I’ve decided to prebuild the table. I’ve stripped out some of the complexity of whay I’m trying to do to illustrate the problem.

I make the table fine but when I go to insert the text in the first field using docbuilder.movetocell, the first value is written outside of the table. It goes underneath. The next and all subsequent write attempts go to the correct cell.

I tried making it go to cell one first and then at the end of the loop go back to cell 0. This wrote to cell 0 but the first cell write attempt still wrote outside the table.
I’ve attached the template I’m using. I do a docbuilder.MoveToSection(3). There are no other tables in that section. There is another table in the section immediately after, in section 4.

Any thoughts? Thanks for your help,
Jerry

for (int j = 0; j < 7; j++) //need to loop through for seven rows.

{

for (int i = 0; i < numberOfFieldSites; i++) // need to create numberOfFieldSites columns. I’ve //written code to split this to a new table after 5 //columns.

{

docbuilder.InsertCell();

}

docbuilder.EndRow();

}

}

if (fieldSitesDTOS != null)

{

int col = 0;
foreach (FieldSiteDTO dto in fieldSitesDTOS)

{

docbuilder.MoveToCell(0,0,col,0); //Move to col 0. col is iterated one each time

if (dto.Market != null)

{

docbuilder.Write(dto.Market.ToString()); // writing the first time, no matter what col index, //text written outside of the cell.

col++;

}

else //if there’s no value in current dto, write blank line to preserve address formatting.

{

docbuilder.Write("");

col++;

}

} // End foreach

}

Please give me some more time for research. I will be able to answer you tomorrow.

Insertion of docbuilder.EndTable() seems to fix the problem:

// Need to loop through for seven rows.
for (int j = 0; j < 7; j++)
{
// Need to create numberOfFieldSites columns.
// I've written code to split this to a new table after 5 columns.
for (int i = 0; i < numberOfFieldSites; i++)
{
builder.InsertCell();
}

builder.EndRow();
}

builder.EndTable();