We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Retrieving Mail Merge Template for Database

Hello,

I am currently evaluating Aspose.Word for a major projrect. I am having trouble retrieving a saved Mail merge template from the database.

Error: 'The document appears to be corrupted and cannot be loaded'

We are saving the documents to a binary max field SQL2005 database and retrieving it to create documents. Are there any restrictions in regards to saving Templates to the database? It works with any other document. thanks

docTemplate = this._cmsDocDS.TCMS_DocumentNewCollection[0].binxTemplate;

MemoryStream fsTemplate = new MemoryStream(docTemplate);

Document docNew = new Document(fsTemplate);

Hello!<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for your interest in Aspose.Words.

There is no specific in loading templates from database. Technically we accept any stream. If you create the stream on the byte array everything should be fine.

Please try the Document constructor overload taking input document format:

http://www.aspose.com/documentation/file-format-components/aspose.words-for-.net-and-java/aspose.words.documentconstructor.html

Also please ensure the binary object stored in the database really contains a valid document template. You can try saving the whole stream to a file and then try opening it with MS Word and Aspose.Words.

Please let me know whether you can get right with this.

Regards,

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. I tried to save/extract document from database and all works fine on my side. Here is my code.

///

/// This example shows how to write document into the database.

///

[Test]

public void Example002()

{

//Create connction

string connectionString = "server=Web1;database=TestDB;uid=sa;pwd=pass;";

System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connectionString);

// Open the DOC file using Aspose.Words.

Document doc = new Document(@"C:\Temp\in.doc");

// ...You can merge data/manipulate document content here.

MemoryStream stream = new MemoryStream();

//Save document to memorystream

doc.Save(stream, SaveFormat.Doc);

//Create sql command

string commandString = "INSERT INTO [Documents] VALUES(@Doc)";

System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(commandString, conn);

//Add paramenter @Doc

command.Parameters.AddWithValue("Doc", stream.GetBuffer());

//Open connection

conn.Open();

//Write document to DB

command.ExecuteNonQuery();

//Close DB connection

conn.Close();

}

///

/// This example shows how to read document from the database.

///

[Test]

public void Example003()

{

//Create connction

string connectionString = "server=Web1;database=TestDB;uid=sa;pwd=pass;";

System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connectionString);

//Create DataSet

DataSet ds = new DataSet();

//Create sql command

string commandString = "SELECT Document FROM Documents WHERE ID=2";

System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(commandString, conn);

//Create data adapter

System.Data.SqlClient.SqlDataAdapter adapter = new System.Data.SqlClient.SqlDataAdapter(command);

//Open connection

conn.Open();

//Read dataset

adapter.Fill(ds);

conn.Close();

//Save document to hard disk

if (ds.Tables.Count > 0)

{

if (ds.Tables[0].Rows.Count > 0)

{

byte[] buffer = (byte[])ds.Tables[0].Rows[0][0];

MemoryStream stream = new MemoryStream(buffer);

Document doc = new Document(stream);

doc.Save(@"C:\Temp\out.doc");

}

}

}

Also could you please attach your document for testing?

Best regards.

I can't attach the document because its sensitive but it's a regular document with merge mail fields included in it.

Also, my code is similar to yours.

Thanks for the help guys.

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for additional information. Please try restoring the document using the following code and open using MS Word.

byte[] buffer = (byte[])ds.Tables[0].Rows[0][0];

MemoryStream stream = new MemoryStream(buffer);

FileStream file = new FileStream(@"C:\Temp\out.doc", FileMode.Create);

stream.WriteTo(file);

file.Close();

stream.Close();

I can’t reproduce this issue on my side so I think that problem is in your document.

Best regards.