LoadDocumentFromBLOBField

Hello, I’ve seen this code in the forum



void DoMerge
{

Document doc = new Document(“template.doc”);

doc.MailMerge.MergeField +=
new MergeFieldEventHandler(MailMerge_MergeField);

doc.MailMerge.Execute(new string[] {“FieldName1”, “FieldName2” },
new object[] {0, 0});

doc.Save(“result.doc”);

}


void AppendDoc(Document dstDoc, Document srcDoc)

{

while (srcDoc.Sections.Count > 0)

{

Section section = srcDoc.Sections[0];

srcDoc.Sections.RemoveAt(0);

dstDoc.Sections.Add(section);

}

}


void MailMerge_MergeField(object sender, MergeFieldEventArgs e)

{

Document doc = LoadDocumentFromBLOBField(e.FieldName);

AppendDoc(e.Document, doc);

e.Text = String.Empty;

}





But I’m interessed with your LoadDocumentFromBLOBField function,
So how can store document in blob field?

And second how can we write this function ?



I’ve to manipulate documents in my project.

Thank you for help.






You can find here a good code sample written by Roman Korchagin. It shows how to store a document in a database and then load it:

https://forum.aspose.com/t/130200

Hello,
I’m testing the code you showed me but it generates errors at :

[TestFixture] ,[TestFixture], Assertion and Word.

To continue, I’ve catch this lines :




//—Assertion.AssertEquals(“Blank.doc”, row[“Name”]);

/– Word word = new Word();

Document doc = word.Open(srcDocStream);

Assertion.AssertEquals(“\r\n”, doc.Range.Text); –/




What does that mean [TestFixture] and [TestFixture] ?
what must I include to compile Assertion and Word ?

And finally to acces to my document I rewrite AddDocumentToTable like this:


private void AddDocumentToTable(DataTable table, string fileName)

{

DataRow row = table.NewRow();

table.Rows.Add(row);

row[“Name”] = fileName;

string path=Server.MapPath(“”) + “\Documents\Testing”;

//string fullFileName = System.IO.Path.Combine(@“X:\Aspose\Aspose.Word\Aspose.Word.Test\Testing”, fileName);

string fullFileName = System.IO.Path.Combine(path, fileName);

using (Stream stream = System.IO.File.OpenRead(fullFileName))

{

BinaryReader reader = new BinaryReader(stream);

row[“Data”] = reader.ReadBytes((int)stream.Length);

}

}



And then I execute the TestSaveLoadDb() function, it add a line in my table
Blank.doc in Name field but Nothing (any document ) is inserted in Data field.

Blank.doc is located in Documents\Testing in my server.

I’m using ASP.net and C#


Please help,



You have done correctly when commented those lines, they’re for testing purposes only. But it is strange that you haven’t achieved the appropriate result. I’ve just tested this sample and it has worked fine (put the document into the database and extracted it).

Please make sure that you have created a table named Documents with two fields Name (string) and Data (OLE Object in MS Access).

As additional steps, check again the paths to the document and to the db file. Check the connection string. Trace the code, make sure that the byte array is properly filled when inserting the document into the db.

Are there any exceptions thrown?

Thanks, I have created a table named Documents with two fields Name (string) and Data (OLE Object in MS Access).

But I’m Visual Studio.net and it cannot compile Assertion.
What must I include example (using …) to compile Assertion

Hi Matar, I’ve the same problem.
I think that if you are using ASP.net and C# you cannot compile Assertion (work for console application).

If you solve your problem, let me know the solution.

No. The Assertion class is a part of the NUnit testing framework and was included for testing purposes only. Therefore the solution is just removing or commenting it as I have already said.

So have you got the application working properly?

No because this lines shows error of compilation.

Word word = new Word();

Document doc = word.Open(srcDocStream);

Assertion.AssertEquals("\r\n", doc.Range.Text);


Word word = new Word();

Document doc = word.Open(srcDocStream);

// This will solve it.
// Assertion.AssertEquals("\r\n", doc.Range.Text);

If you solve this, please let us know solution

// This will solve it.
// Assertion.AssertEquals("\r\n", doc.Range.Text);


Have you books in this area of aspose.word ? we’ve soo many problems with

Sorry that the code example we posted in the forums did not compile for you straight away. This code example was copied and pasted from production code without proper preparation to make it friendly to all users.

We use NUnit for unit testing and Assertion is a class in that library. You do not need this to run the code example and you were simply expected to delete or comment out the lines that do not compile (there is only one or two lines like this in the example).

Please note that some of the code examples posted in the forums previously may contain references to Assertion class and all these lines should be deleted or commented out.

Sorry it caused you confusion, but we considered it is such a simple issue that does not warrant any attention and we never had any questions from other users about this.