I am trying to create multiple documents all of the same Word template file. I have logic that goes through a loop to create each one individuall and them combine them into a single document. The problem is that the single creation does not work. It creates the same document again and again. Therefore, the combine logic does not work either. I am sure I am doing something wrong, I just don’t know what. Can someone please advise. Code snippets follow. Btw, I am passing parameters to a query but I checked to make sure the retrieval arguments are unique.
This is the main logic:
private void buildDocument(Boolean batchMode)
{
destinationDocument = new Document();
//batch or single?
if (batchMode == true)
{
//traverse XML tree
XmlNodeList nodelist = xmlDocument.GetElementsByTagName("Parameter");
foreach (XmlNode node in nodelist)
{
//open the source document
string sourceFileName = "C:\\LANOV3.DOC";
sourceDocument = new Document(sourceFileName);
//build it
string novID = getValue(node, "novid");
generateNOV(novID);
appendDoc(destinationDocument, sourceDocument);
}
destinationDocument.Save("C:\\FINISHED2.DOC");
}
This is the function that builds the individual reports:
This is the code to build a single document.
private void generateNOV(string novID)
{
//dynamimc where clause
string hdrWhereClause, detailWhereClause;
//datareader
IDataReader dataReader = null;
//massage where
hdrWhereClause = "WHERE NOTICE_OF_VIOLATION.NOTICE_OF_VIOLATION_ID =" + "'" + novID + "'";
//build header query
OleDbCommand headerCmd = new OleDbCommand(
"SELECT NOTICE_OF_VIOLATION.NOTICE_OF_VIOLATION_ID AS NOVID, NOTICE_OF_VIOLATION.NOTICE_OF_VIOLATION_DATE AS VIOLATIONDATE, " +
"NOTICE_OF_VIOLATION.SERVED_TO_NAME AS SERVEDTONAME, NOTICE_OF_VIOLATION.SERVED_TO_TITLE AS TITLE, " +
"NOTICE_OF_VIOLATION.SERVED_TO_DRIVERS_LICENSE AS LICENSENUMBER, NOTICE_OF_VIOLATION.COMMENTS, " +
"NOTICE_OF_VIOLATION.RESPONSE_DUE_DATE, NOTICE_OF_VIOLATION.PERMIT_S, NOTICE_OF_VIOLATION.IND_ID, " +
"NOTICE_OF_VIOLATION.INSPECTOR_L_S, FACILITY.FACILITY_ID AS FACILITYID, PERMIT.PERMIT_NUM AS PERMITNUM, " +
"FACILITY.IND_NAME AS FACILITYNAME, INSPECTOR_L.INSPECTOR AS INSPECTOR, INSPECTOR_L.PHONE AS INSPECTORPHONE, " +
"ADDRESS.ADDRESS_L_S, ADDRESS.STREET_NAME AS STREETNAME, ADDRESS.STREET_NUM AS STREETNUM, ADDRESS.STREET_PREFIX AS STREETPREFIX, ADDRESS.STREET_SUFFIX AS STREETSUFFIX, " +
"ADDRESS.STREET_UNIT_NUM STREETUNITNUM, ADDRESS.CITY AS CITY, ADDRESS.STATE_L_S, ADDRESS.ZIP AS ZIP, STATE_L.STATE AS STATE " +
"FROM ADDRESS INNER JOIN " +
"FACILITY ON ADDRESS.IND_ID = FACILITY.IND_ID LEFT OUTER JOIN " +
"STATE_L ON ADDRESS.STATE_L_S = STATE_L.STATE_L_S RIGHT OUTER JOIN " +
"NOTICE_OF_VIOLATION INNER JOIN " +
"PERMIT ON NOTICE_OF_VIOLATION.PERMIT_S = PERMIT.PERMIT_S ON FACILITY.IND_ID = PERMIT.IND_ID AND " +
"FACILITY.IND_ID = NOTICE_OF_VIOLATION.IND_ID FULL OUTER JOIN " +
"INSPECTOR_L ON NOTICE_OF_VIOLATION.INSPECTOR_L_S = INSPECTOR_L.INSPECTOR_L_S AND " +
"PERMIT.INSPECTOR_L_S = INSPECTOR_L.INSPECTOR_L_S AND FACILITY.SENIOR_INSPECTOR_L_S = INSPECTOR_L.INSPECTOR_L_S AND " +
"FACILITY.INSPECTOR_INSPECTOR_L_S = INSPECTOR_L.INSPECTOR_L_S " +
"WHERE (NOTICE_OF_VIOLATION.NOTICE_OF_VIOLATION_ID = '2KOOL4U') AND (ADDRESS.ADDRESS_L_S = 1)", conn);
//get data
dataReader = headerCmd.ExecuteReader();
//populate doc
sourceDocument.MailMerge.ExecuteWithRegions(dataReader, "NOVHeader");
//massage where
detailWhereClause = "WHERE NOTICE_OF_VIOLTN__VIOLTN.NOTICE_OF_VIOLATION_ID =" + "'" + novID + "'";
//build detail querty
OleDbCommand detailCmd = new OleDbCommand(
"SELECT VIOLATION.VIOLATION_DATE AS ViolationDate, SNC_CATEGORY_L.SNC_CATEGORY_DESC AS SNCDescription, VIOLATION.VIOLATION_DESC AS ViolationDesc, " +
"SNC_CATEGORY_DETAIL_L.SNC_CATEGORY_DETAIL AS SNCDetail, SNC_CATEGORY_DETAIL_L.SNC_CATEGORY_DETAIL_DESC AS SNCDetailDesc, " +
"NOTICE_OF_VIOLTN__VIOLTN.NOTICE_OF_VIOLATION_ID AS ViolationID " +
"FROM VIOLATION INNER JOIN " +
"SNC_CATEGORY_DETAIL_L ON VIOLATION.SNC_CATEGORY_DETAIL_L_S = SNC_CATEGORY_DETAIL_L.SNC_CATEGORY_DETAIL_L_S INNER JOIN " +
"NOTICE_OF_VIOLTN__VIOLTN ON VIOLATION.VIO_ID = NOTICE_OF_VIOLTN__VIOLTN.VIO_ID LEFT OUTER JOIN " +
"SNC_CATEGORY_L ON SNC_CATEGORY_DETAIL_L.SNC_CATEGORY_L_S = SNC_CATEGORY_L.SNC_CATEGORY_L_S AND " +
"VIOLATION.VIO_OR_SNC_CAT_L_S = SNC_CATEGORY_L.SNC_CATEGORY_L_S " + detailWhereClause, conn);
//get data
dataReader = detailCmd.ExecuteReader();
//populate doc
sourceDocument.MailMerge.ExecuteWithRegions(dataReader, "NOVDetails");
sourceDocument.Save("C:\\SINGLE.DOC");
//cleanup
dataReader.Close();
}
Why does SINGLE.DOC keep showing the same report over and over again?