Execute Merge Help for Dummy.NET

Howdy all:

Just migrated to a provider that allows .NET functionality, and feel like a kid in a candy store (albeit a very, VERY dumb kid).

What I have is a DB with two fields; the first field is the name of the mergefield in the document, and the second field is the insertion.

I am a complete idiot with C# – just trying to reverse engineer the sample code into something that can work for me. My problem is in the new ADO.

I am still in ASP recordsetland. It seems as though all of the Execute or ExecuteWithRegions deal with tables, and I need to figure out a workaround for that, because the mergefield needs to be determined from a field in the DB as opposed to a header.

The closest I have so far is using Execute(string[], object[]), but I don’t know how to iterate through a dataset, or how to form an object from colum(2)row(x)

Please excuse the ignorance, and any help would be highly appreciated. My head is throbbing just from entering the realm.

Thanks in advance (God I can’t wait to get this running – it is LIGHT YEARS ahead of what I was doing in the past!) THANK YOU FOR AN AWESOME PRODUCT

DataTable in .NET is the same concept as ADO recordset, but of course much better.

This is code/pseudocode that should give you and idea what you need to do:

DataTable myTable = get my table;

string fieldNames[] = new string[myTable.Rows.Count];
object fieldValues[] = new string[myTable.Rows.Count];

for (int i = 0; i < myTable.Rows.Count; i++)
{
DataRow row = myTable.Rows[ i ];
fieldNames[ i ] = row[“FieldName”]);
fieldValues[ i ] = row[“FieldValue”]);
}

doc.MailMerge.Execute(fieldNames, fieldValues);

Awesome. Thanks for your help. We use mailmerge in kind of a different way – like document generation. Only one file, but like 600 different fields. The fields are populated programatically and then stored in a DB for the merge. So really, the mergefields are just placeholders for text insertion.

That was the “best” way to do it prior to finding your program, and now we will be re-working everything as soon as I grow a brain. This is really, really cool stuff!

Well, I guess I am too stupid to translate your psuedocode into pagecode, so I turn to you again (ARRGH, I am so close)...

Sorry to bug you again, and thanks for your help (again)

compiler output followed by page code:

---------------------------

Microsoft (R) Visual C# .NET Compiler version 7.10.3052.4
for Microsoft (R) .NET Framework version 1.1.4322
Copyright (C) Microsoft Corporation 2001-2002. All rights reserved.

shan.aspx.cs(121,30): error CS0650: Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier
shan.aspx.cs(121,31): error CS1525: Invalid expression term ']'
shan.aspx.cs(121,65): error CS1003: Syntax error, ']' expected
shan.aspx.cs(122,31): error CS0650: Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier
shan.aspx.cs(122,32): error CS1525: Invalid expression term ']'
shan.aspx.cs(122,66): error CS1003: Syntax error, ']' expected
shan.aspx.cs(127,49): error CS1002: ; expected
shan.aspx.cs(127,49): error CS1525: Invalid expression term ')'
shan.aspx.cs(128,51): error CS1002: ; expected
shan.aspx.cs(128,51): error CS1525: Invalid expression term ')'


----------------------------




DataTable myTable = ExecuteDataTable("merge","SELECT * FROM merge");

121. string fieldNames[] = new string[myTable.Rows.Count];
122. object fieldValues[] = new string[myTable.Rows.Count];

for (int i = 0; i < myTable.Rows.Count; i++)
{
DataRow row = myTable.Rows[ i ];
127. fieldNames[ i ] = row["FieldName"]);
128. fieldValues[ i ] = row["FieldValue"]);
}

doc.MailMerge.Execute(fieldNames, fieldValues);
doc.Save(outputFile,SaveFormat.FormatDocument);
Response.End();
}

private DataTable ExecuteDataTable(string tableName, string commandText)
{
OleDbConnection conn = null;
try
{
conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbPath + "\\Merge.mdb");
OleDbCommand cmd = new OleDbCommand(commandText, conn);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataTable table = new DataTable(tableName);
da.Fill(table);
return table;
}
finally
{
if (conn != null)
conn.Close();
}
}

Okay -- Got that figured to a point, but still running into the problem of the object.

DataTable myTable = ExecuteDataTable("merge","SELECT * FROM merge");

string[] fieldNames = new string[myTable.Rows.Count];
object[] fieldValues = new string[myTable.Rows.Count];

for (int i = 0; i < myTable.Rows.Count; i++)
{
DataRow row = myTable.Rows[ i ];
fieldNames[ i ] = row["FieldName"];
fieldValues[ i ] = row["FieldValue"];
}


throws a "cannot implicitly convert type 'object' to 'string'"

Thanks

fieldNames[ i ] = (string)row[“FieldName”];
or
fieldNames[ i ] = row[“FieldName”] as string;