RSS

Tag Archives: fyiReporting

fyiReporting reports tutorial 2/2 (Parameters)

Part 1/2 (Intro)
Part 2/2 (Paramters)

If you want print in WPF please view my new WPF-Printing Library

In addition to the first part of the tutorial I will explain in this second part the usage of parameters.

The text that should appear in the body and the header can be made static or dynamic. To create a bill it is smarter to create it dynamic, because the recipient of the bill is every times another person. Such data is loaded into the report via parameter. To create a parameter you should double click in the footer section. In the parameter section you can add the parameter you need. Give it a name and a data-type. Now parameters can be used in textFields. To use this parameters you should write into the textBox following string “=Parameters!nameOfParameter.Value”.

As you can see,after opening the .rld file with fyiReporting Designer, the page header part of the report contains some labels that contains some strange strings like: =Parameters!Firma.Value.
Because it happens that e.g an invoice is send every times to another company the label containing the company name has to be different.
The parameter in the example above is used to display the Company name (Firma).

In order to display the value of the parameter at runtime, you have to write some lines of code:

ReportViewer reportViewer = new ReportViewer();
reportViewer.LocalReport.SetParameters(loadReportParameter());

private ReportParameter loadReportParameter()
{
    ReportParameter parameters = new ReportParameter[1];
    parameters[0] = new ReportParameter("Firma", "String containing the name of the company");
    return parameters;
}

The tricky thing of using parameters is, that you have to initialize every single parameter of the report with a corresponding value, otherwise you will receive an error when you try to load the report. Unfortunately the error message that appears is not so clear as expected. Therefore, if more than one parameter is missing you will not exactly now which parameter you have not initalized. A hint is to add one parameter with the report designer and add the corresponding parameter to the ReportParamter array in the loadReportParameter() method.

Very useful is if you use following line of code into your C# class:

ReportParameterInfoCollection reic = reportViewer.LocalReport.GetParameters();

here you can see at debugging time the number of parameters that the .rld file expects.

 

 
3 Comments

Posted by on May 14, 2010 in C-Sharp, Printing, Reporting

 

Tags:

fyiReporting reports tutorial 1/2

Part 1/2 (Intro)
Part 2/2 (Paramters)

If you want print in WPF please view my new WPF-Printing Library

I will upload some pictures for better understanding next week

In consideration to my administration program, I was forced to choose between several reporting engines. Finally, the decision was to use FyiReporting. Its an open source solution to generate rdl-Templates.

Beginning with the designer

The first step should be to use the designer to design the report as is should be. You can download it from the project homepage. The main window of the designer is divided into three parts. The page header, body and the footer.
The header and the footer is used for information of that e.g a bill is consisting: the recipient of the bill on the top of the page, the sender ont the bottom. The body is used to list the items of the bill.

DataSet – Containing all information that is shown in the body of the report

The major point is the DataSet in which you can load the corresponding data from outside. It contains the information about what kind of data is loaded from the final XML-file.  Before creating a DataSet you have to add a Data Source by clicking Data=>Data Sources. There you can select the database you want to use, etc.  To not use a database you select one of your choice and enter following connection string data source=localhost;initial catalog=Name. A new DataSet is created by selecting the Data=>Data Sets=>New menu item. There you should select the created DataSource. By adding items in the “Fields” box you can add this columns you want to be shown in the report. Be aware to scroll to the right to set the “TypeName”. Each TypeName should be in the form of  System.String, System.Int32, System.Decimal.

After creating the DataSet you should make a table in the body. The table can be created by choosing the corresponding table icon in the tool-bar of the program. A new window opens. There you can select the DataSet you want to use.  Select all appearing columns that you want to use in the report.

After that the table is shown in the body. Some of the cells of the table contains values like “=Fields!ColumnName.Value”. The “Fields” are the values in the dataset.

Now some basic steps of the report are done.

The next steps were realized via C#

private void loadReport()
{
    try
    {
        ReportViewer reportViewer = new ReportViewer();
        reportViewer.ProcessingMode = ProcessingMode.Local;
        reportViewer.LocalReport.ReportPath = @"PathToReport\report.rdl";
        ReportParameterInfoCollection re = reportViewer.LocalReport.GetParameters();
        reportViewer.LocalReport.SetParameters(loadReportParameter());
        reportViewer.LocalReport.DataSources.Add(new ReportDataSource("DataSetName", loadXML()));
        windowsFormsHost.Child = reportViewer;
        reportViewer.RefreshReport();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

private DataTable loadXML()
{
    // Load data from XML file
    DataSet dataSet = new DataSet();
    dataSet.ReadXml(new XmlNodeReader(/*Takes Some XML-File*/));
    return dataSet.Tables[0];
}

private ReportParameter loadReportParameter()
{
    ReportParameter parameters = new ReportParameter[2];
    parameters[0] = new ReportParameter("ParameterName1", "VALUE");
    parameters[1] = new ReportParameter("ParameterName2", "VALUE");
    return parameters;
}

To generate the corresponding XML-File following code can be used

private void GenerateXML( BillItem[] billItem )
{
    //BillItem is a class that I have creaded. You can use your own class to store the bill-items into your C# code
    XmlDocument xmlBill = new XmlDocument();
    XmlNode docNode = xmlBill.CreateXmlDeclaration("1.0", "UTF-8", null);
    bill.AppendChild(docNode);
    XmlNode productsNode = xmlBill.CreateElement("SomeName1");
    productAttribute.Value = "http://someURL/SomeName1.xsd";
    productsNode.Attributes.Append(productAttribute);
    bill.AppendChild(productsNode);
    foreach (BillItem item in billItem)
    {
        if (item == null) break;
        XmlNode productNode = xmlBill.CreateElement("DataSetName");
        productsNode.AppendChild(productNode);
        XmlNode nameNode = xmlBill.CreateElement("ColumnName1");
        nameNode.AppendChild(xmlBill.CreateTextNode(item.ItemNo));
        productNode.AppendChild(nameNode);
        nameNode = xmlBill.CreateElement("ColumnName2");
        nameNode.AppendChild(xmlBill.CreateTextNode(item.Price));
        productNode.AppendChild(nameNode);
    }
}

An example of a fyiReporting Designer report file can be downloaded here
Note: change the file extension of the file from .doc to .rdl to be able to view the file with fyiReporting Designer.

 
11 Comments

Posted by on January 16, 2010 in C-Sharp, Printing, Reporting

 

Tags:

 
Follow

Get every new post delivered to your Inbox.