Example: Additional data sources |
Docentric Toolkit Help
Send Feedback |
In this report, we want to write out some data of one particular order (e.g. shipping address, shipping fee, date of the order etc.) and some additional data of one particular customer (e.g. customer’s name, company, job title, address etc.) that are not related to the order. Data model used for all examples is exposed here.
Initially, we are creating a report template. Follow the common preparation steps to start building the template from scratch:
- Open a new blank Word document and enable it for templating
- Turn on Data Sources and Elements Explorers
- Define default data source as the Order class
After that, you should be at the following point:
Clearly, now we are able to put Field elements on the template binding them
to properties of the class Order but we do not have any
possibility to introduce an Docentric element bound to the Customer’s
property. Here comes into play Docentric feature Additional data sources. Additional
data source can be defined in the following way:
Define a name for additional data source (the name is mandatory and will be later
used in code, at report generation time):
Define a .NET data type representing additional data source:
Select as .NET data type Customer class:
Buttons Edit and Remove can be used in purposes of renaming of additional
data sources and removing them from the template:
Add Docentric Field elements from Docentric Ribbon tab (about inserting Field elements
you can read here) to the template
that are bound to some properties of the Order class by
setting their Value property which representing their binding.
The properties Binding Source and Binding Path of the Value property should be set to the
default data source and appropriate member properties of the class Order respectively:
Add other Docentric Field elements to the template that are bound to some properties of
the Customer class (our additional data source) by setting
their Value property in following way:
Click on the Binding control of the Value property:
For the (Binding) Source property select the Customer and not default data source.
By picking Customer data source for (Binding) Source property, a list of its class property members
show up in other part of the Binding Control. Choose the Company property since
we want to bind the inserted Field element to the Company of the Customer:
Inserting and binding of Docentric elements at the same time is possible to bring out through Element Builder pane
and in that way achieve drastic improvement of report design rapidity. Instructions how to use Element Builder you can
find here.
Finally, the template with Field elements bound to additional data source Customer
looks like:
Next, save the template and generate the report.
Report generation takes place programmatically, from your application that is using
(has a reference to) the Docentric Report Engine dll. This code looks like following:
// Instancing report engine, by assigning the data source
DocumentGenerator dg = new DocumentGenerator(DataAccess.GetOrderById(7));
// Generating report by specifying the report template and the resulting
report (as file paths)
DocumentGenerationResult result = dg.GenerateDocument("example3.docx", "example3_output.docx");
// Examining potentially errors
if (result.HasErrors)
{
foreach (Error
error in result.Errors) Console.Out.WriteLine(error.Message);
}
Generated report looks like:
Obviously, data related to the Customer have not been
populated. This is because we have not specify additional data source in the code.
On the first page of generated report you can inspect the list of errors occurred
during rendering:
You can certainly turn off this feature (it is turned on by default) and relay on the same list of errors you can reach from the code as well, giving you a chance to react properly:
// Examining potentially errors
if (result.HasErrors)
{
foreach (Error
error in result.Errors) Console.Out.WriteLine(error.Message);
}
Turning off the feature of writing out all errors and warnings occurred during report generation on the generated report:
dg.WriteErrorsAndWarningsToDocument = false;
So as to accomplish populating with data the Fields elements in the template bound
to Customer's properties, we need to add additional data
source named exactly as in the template - Customer :
// Instancing report engine, by assigning the data source
Order orderDataSource = DataAccess.GetOrderById(7);
DocumentGenerator dg = new DocumentGenerator(orderDataSource);
// Adding additional data source named exactly as in the template
Customer customerDataSource = DataAccess.GetCustomerById(15);
dg.AddDataSource("Customer", customerDataSource);
// Generating report by specifying the report template and the resulting
report (as file paths)
DocumentGenerationResult result = dg.GenerateDocument("example3.docx", "example3_output.docx");