Deborah's Developer MindScape






         Tips and Techniques for Web and .NET developers.

Archive for Reporting

December 19, 2010

Reporting in .NET Part II

Filed under: C#,Reporting,VB.NET @ 12:40 am

This prior post covers the steps for building and displaying a report in .NET. But how often do you need just one report? Normally, you need to build several if not many reports. That brings up several issues:

  • How do you set a style that you can reuse for every report?
    • It would be nice to set fonts, styles, colors, and so on to ensure that the reports all look similar.
  • How do you set up reusable text for the report?
    • For example, a standard header or standard end of report text.
  • How do you ensure that the reports are the same standard size?
    • You may want two standard sizes: one for portrait and one for landscape.

Seems like a reasonable set of questions. The disappointing part is that there are no great answers when using Visual Studio’s reporting services.

Here are some options:

  1. Build a template.
  2. Use expressions.
  3. Update the XML at design time.
  4. Update the XML at run time.

There is no style sheet for a report like in ASP.NET, WPF, or Silverlight. There is no ability to write code to work with the controls on the report beyond the capabilities of expressions. None of the above options is perfect, but they each provide some features to help you reuse parts of your reports.

Enjoy!

December 15, 2010

Tie a Report to Data from Business Objects

Filed under: C#,Reporting,VB.NET @ 3:49 pm

This prior post provides an overview of reporting in your WinForms or WebForms application. This post details how to tie your report to data defined within your business objects.

See this prior post if you want to tie your report directly to data in the database.

In this example, the report built in this prior post is tied to a list of Customer business objects. The list of customer objects is defined in this prior post and summarized below:

In C#:

Customer class:

public class Customer
{
    public int CustomerId { get; set; }
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public string EmailAddress { get; set; }
}

List of customers:

List<Customer> custList = new List<Customer>
                    {new Customer()
                          { CustomerId = 1,
                            FirstName="Bilbo",
                            LastName = "Baggins",
                            EmailAddress = "bb@hob.me"},
                    new Customer()
                          { CustomerId = 2,
                            FirstName="Frodo",
                            LastName = "Baggins",
                            EmailAddress = "fb@hob.me"},
                    new Customer()
                          { CustomerId = 3,
                            FirstName="Samwise",
                            LastName = "Gamgee",
                            EmailAddress = "sg@hob.me"},
                    new Customer()
                          { CustomerId = 4,
                            FirstName="Rosie",
                            LastName = "Cotton",
                            EmailAddress = "
rc@hob.me"}};

In VB:

Customer class:

Public Class Customer
Public Property CustomerId As Integer
    Public Property FirstName() As String
    Public Property LastName() As String
    Public Property EmailAddress() As String
End Class

List of customers:

Dim custList As New List(Of Customer) From
            {New Customer() With
                  {.CustomerId = 1,
                    .FirstName = "Bilbo",
                    .LastName = "Baggins",
                    .EmailAddress = "bb@hob.me"},
            New Customer() With
                  {.CustomerId = 2,
                    .FirstName = "Frodo",
                    .LastName = "Baggins",
                    .EmailAddress = "fb@hob.me"},
            New Customer() With
                  {.CustomerId = 3,
                    .FirstName = "Samwise",
                    .LastName = "Gamgee",
                    .EmailAddress = "sg@hob.me"},
            New Customer() With
                  {.CustomerId = 4,
                    .FirstName = "Rosie",
                    .LastName = "Cotton",
                    .EmailAddress =
rc@hob.me}}

To Build a Data Source for a Report:

1) Display the report designer by double-clicking on the .rdlc file in the Solution Explorer.

2) Display the Data Sources Window (Data | Show Data Sources).

3) Click on the Add New Data Source button in the Data Sources Window toolbar.

4) Select to get the data from an Object and click Next.

image

5) Select the business object that defines the fields for the report and click Finish.

image

Your data source is then created and shown in the Data Sources Window.

To Build a Dataset for a Report:

A report is tied to a data source through a dataset. A dataset defines the data fields available to the report. You define a dataset based on a data source (created in the steps above).

This term "dataset" should not be confused with an ADO.NET dataset. This is not the same thing. In this context, a "dataset" is simply a set of data that can be used on a report.

1) Display the report in the designer by double-clicking on the .rdlc file in the Solution Explorer.

2) Display the Report Data Window (View | Report Data).

NOTE: If you do not see the Report Data menu option, ensure that the report designer is the active window.

3) Select New | Dataset from the Report Data window toolbar.

This creates a dataset that is accessible from the report.

4) Enter the name of the dataset, select the data source to use as the source of data for the dataset (defined in the prior set of steps), and select the data object within the data source to tie to the report.

The fields available to the report are then shown:

image

NOTE: By my programming convention, I name the report’s dataset "XXXDataSource" so as not to be confused with any typed dataset in the project.

The fields also appear in the Report Data window:

image

NOTE: To view the Report Data window, ensure that a report is the active Window in design mode. Then select View | Report Data.

To Display Data from the Data Source on the Report:

1) Display the report in the designer by double-clicking on the .rdlc file in the Solution Explorer.

2) Hover over a cell in the data row, click on the field list (circled in red in the image), and select the field to display in the cell.

image

3) Repeat for each cell in the table.

4) To add fields from the dataset that are not already shown in the table, drag the field from the Report Data Window and drop it in the table.

5) Write the code to populate the data source.

When you tie a report directly to a database, the data is automatically retrieved when you run the report. When you tie a report to a set of business objects, you need to populate the underlying data source.

In C#:

private void ReportWin_Load(object sender, EventArgs e)
    this.customerBindingSource.DataSource = Customers.RetrieveList();
    this.reportViewer1.RefreshReport();
End Sub

In VB:

Private Sub ReportWin_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
    Me.CustomerBindingSource.DataSource = Customers.RetrieveList
    Me.ReportViewer1.RefreshReport()
End Sub

This code assumes that a static/shared method in the Customers class called RetrieveList retrieves the list shown at the top of this post.

Use this technique any time you want your report to pull its data from your business objects.

Enjoy!

EDIT 2/18/11: Added a note with details on viewing the Report Data window.

Tie a Report to Data from the Database

Filed under: C#,Reporting,VB.NET @ 2:33 pm

This prior post provided an overview of reporting in your WinForms or WebForms application. This post details how to tie your report to data directly from a database.

In this example, the report built in this prior post is tied directly to the data in the Customer table.

To Build a Data Source for a Report:

1) Display the report designer by double-clicking on the .rdlc file in the Solution Explorer.

2) Display the Data Sources Window (Data | Show Data Sources).

3) Click on the Add New Data Source button in the Data Sources Window toolbar.

4) Select to get the data from a Database and click Next.

image

5) Select Dataset (or Entity Data Model if you are using Entity Framework) and click Next.

NOTE: If you pick Entity Data Model, the additional dialogs will be different than shown below.

image

6) Select a connection or create a new connection and click Next.

image

7) Select to save the connection string and click Next.

image

8) Select which database objects to include in your dataset and click Finish. For this example, only the Customer data is required.

image

This adds a typed dataset (CustomerDataSet.xsd in this example) to the project.

To Build a Dataset for a Report:

A report is tied to a data source through a dataset. A dataset defines the data fields available to the report. You define a dataset based on a data source (created in the steps above).

This term "dataset" should not be confused with an ADO.NET dataset. This is not the same thing. In this context, a "dataset" is simply a set of data that can be used on a report.

NOTE: The "dataset" can instead be a list of business objects as detailed in this post.

1) Display the report in the designer by double-clicking on the .rdlc file in the Solution Explorer.

2) Display the Report Data Window (View | Report Data).

NOTE: If you do not see the Report Data menu option, ensure that the report designer is the active window.

3) Select New | Dataset from the Report Data window toolbar.

This creates a dataset that is accessible from the report.

4) Enter the name of the dataset, select the data source to use as the source of data for the dataset (defined in the prior set of steps), and select the data object within the data source to tie to the report.

The fields available to the report are then shown:

image

NOTE: By my programming convention, I name the report’s dataset "XXXDataSource" so as not to be confused with any typed dataset in the project.

The fields also appear in the Report Data window:

image

To Display Data from the Data Source on the Report:

1) Display the report in the designer by double-clicking on the .rdlc file in the Solution Explorer.

2) Hover over a cell in the data row, click on the field list (circled in red in the image), and select the field to display in the cell.

image

3) Repeat for each cell in the table.

4) To add fields from the dataset that are not already shown in the table, drag the field from the Report Data Window and drop it in the table.

Use this technique any time you want your report to pull its data directly from the database.

Enjoy!

Building the Layout for a Report

Filed under: C#,Reporting,VB.NET @ 2:19 pm

This prior post provides an overview on how to include reporting in your WinForms or WebForms applications. This post details how to build the layout for a simple report.

For this simple example, the report includes a header, footer, and the list of all current customers.

The first step is to add a report file (.rdlc) to your project as defined in this prior post. Then add report items to the designer to lay out your report as detailed below.

To Add a Header:

1) Select Report | Add Header OR right-click on the designer and select Insert | Page Header.

2) Drag a Text Box or other controls from the Toolbox to the header in the designer. (View | Toolbox)

This example uses a Text Box for the report title.

3) Right-click on each control in the header and select Properties OR click on the control and use the Properties Window to set fonts, styles, and other attributes as desired.

To Add a Footer:

1) Select Report | Add Footer OR right-click on the designer and select Insert | Page Footer.

2) Drag a Text Box or other controls from the Toolbox to the footer in the designer. (View | Toolbox)

This example uses a Text Box for the print date in the bottom left corner and a second Text Box for the page number in the lower right corner.

3) Right-click on each control in the footer and select Properties OR click on the control and use the Properties Window to set fonts, styles, and other attributes as desired.

4) If you want to use an expression to populate the Text Box, right-click on a control and select Expression.

An expression allows you to define a single line of "code" that you can use to specify the contents or properties of a control. For example, you could define the current print date as an expression. Or you could set the font size as an expression.

To define a print date on the report:

image

Use Common Functions | Date & Time | Now. Double-click on Now to add it to the expression in the top pane. Be sure to close the parentheses pair.

To define a page number on the report:

image

Use Built-in Fields | PageNumber. Double-click on PageNumber to add it to the expression in the top pane. Be sure to close the parentheses pair.

To Add a Table for the Body of the Report:

1) Drag the Table control from the Toolbox to the body of the report. (View | Toolbox)

This displays a set of dialogs for setting up a data source.

  • To tie the report directly to the database, see this post.
  • To tie the report to business objects, see this post.

To skip tying the report to the data at this point, click Cancel and continue setting up the layout of the table.

2) Size and position the table (called a Tablix) as desired.

NOTE: Regardless of the number of rows that you need to display in the table, you only have to format the optional header, optional footer, and one data row. The report will repeat your styles for every data row.

3) Right-click on any cell within the table and select Properties OR click on the control and use the Properties Window to set fonts, styles, formatting and other attributes as desired.

The resulting report appears in the designer as shown below:

image

Use this technique to build the layout for any simple reports required by your application.

Enjoy!

Reporting in .NET

Filed under: C#,Reporting,VB.NET @ 2:02 pm

Most business applications require some type of reporting. Why bother with all of the CRUD (create, review, update, delete) if there is no reporting on all that data?

With .NET, there are several reporting options and features. This set of posts focuses on two features:

  • Report file: An installed template in Visual Studio for building the layout for a report.
  • ReportViewer: A control in the toolbox that can be added to a form for displaying the report to the user.

The example below provides an overview of building the "world’s simplest report". In building the "simplest" report, there are still a very large number of steps. So this post provides an overview and other referenced posts provide detail.

To Create a Report:

1) Add a report file to your project.

Select Project | Add New Item, click on Reporting under Installed Templates on the left and then select Report (or Report Wizard if you want to use the Wizard) in the middle.

NOTE: Though using the Report Wizard could arguably be the best approach when building the "world’s simplest report", when I first starting building reports with these tools I found the Wizard to be confusing (DataSource, DataSet, Row group, Column group, etc) . So for clarity, these posts walk through the manual steps.

image

Clicking Add adds the .rdlc file to your project. In this example, the report file is named Customers.rdlc.

2) Build the report layout using the tools in the toolbox. (View | Toolbox)

See this link for more information on building the layout for a report.

3) Tie the report layout to your data using the tools in the Report Data window. (View | Report Data)

When you build reports within Visual Studio, there is no mechanism for previewing the report with its data. You have to build your own user interface for displaying the report.

To Display a Report:

1) Add a form to your project.

NOTE: You can add a Windows Form or a Web form. This example uses a Windows Form.

2) Add the Report Viewer control from the toolbox to the form. (View | Toolbox)

image

You may want to dock the Report Viewer control within the parent container so the report fills the entire form.

3) Tie your report to the Report Viewer control.

The easiest way to tie your report to the Report Viewer is to use the Report Viewer’s smart tag:

image

Select the name of the report from the Choose Report drop down list.

4) Tie your dataset to the Report Viewer control.

The easiest way to tie your report dataset to the Report Viewer is to use the Report Viewer’s smart tag and select Choose Data Sources. Then select the data source defined in Step 3 of "To Create a Report".

image

5) Set the new form as your startup form if you want to run the report directly when launching your application for testing purposes.

See this post for instructions on setting a form as a startup form.

Otherwise, add navigation in your application that allows the user to navigate to the report either through a link, menu option, toolbar icon, or other mechanism.

The resulting report looks something like this:

image

Use this technique any time you need to add reporting to your application.

Enjoy!

© 2023 Deborah's Developer MindScape   Provided by WPMU DEV -The WordPress Experts   Hosted by Microsoft MVPs