SIGN UP MEMBER LOGIN:    
ARTICLE

Building Reports from a DataSet using ReportViewer

Posted by Mahesh Chand Articles | Reports using C# April 27, 2007
In this step-by-step tutorial, you will learn how to create reports from a Database using a DataSet, the ReportViewer control and Visual Studio 2005.
Reader Level:
Download Files:
 

In this step-by-step tutorial, you will learn how to create reports from a Database using a DataSet and the ReportViewer control and Visual Studio 2005.

Step 1. Generate a Typed DataSet 

First of all, we need a typed DataSet that will represent the data types. You can generate a typed DataSet from a database table, stored procedure, and a view or from a SQL query. After generating the typed DataSet, report generation is same.

Right click on the project in Solution Explorer, select Add >> New Item and select DataSet from the list.

Change DataSet1.xsd to Company.xsd. After adding the DataSet, you will see the designer. On the designer, click Server Explorer link, which will let you create a Database connection and can view database objects including tables, views, and stored procedures.

Drag a database table, or view, or stored procedure where you want to display the data from and it will create a DataSet schema for you. Once you have a typed DataSet, just follow these steps.

Before that, make sure you Rebuild the project.

Step 2. Create a Report

Now we will add a new report file to the project. Right click on the project in Solution Explorer and select Add >> New Item and select Report from the Items list. It will add Report1.rdlc file to the project.

Once the report is added, our next step is to add a data source. First double click on the Form and select Data Menu item from the Main Menu. Click on Data Menu item and select Add New Data Source item.

It will launch Data Source Configuration Wizard. Select Object from the list and click the Next button on the Wizard. See Figure 1.


Figure 1.

On next dialog, you should see all namespaces and classes in your project. Expand your namespace and you will see class Company. See Figure 2.


Figure 2.

Select Company class and click the Next button. On next dialog, you will see a confirmation message. Select Finish there and get out of the wizard.

Now double click on the Report1.rdlc file and you should see Figure 3 in your Data Sources window.


Figure 3.

Now let's create and format the report.

Drag a Table from the Toolbox and drag Name, Age, and Phone columns from the Data Sources to the report's middle row. As you can see from Figure 4, the name of the column is automatically added to the header (first) row of the report.

Figure 4.

Step 3. Create a Report Viewer and Bind the Report

Now open the Form1 again and drag a ReportViewer control from the Toolbox to the Form. Click on the smart tag and select Report1.rdlc from the Choose Report drop down list. See Figure 5.

 
Figure 5.

By doing so, you will see an EmployeeBindingSource control is added at the bottom of the Form. See Figure 6. The BindingSource control provides connection between the data and the ReportViewer control.

 
Figure 6.

Step 4. Fill Data

Now write the code listed in Listing 2 on the Form's load event handler. This code creates a DataSet, loads the data from Data.xml file and sets EmployeeBindingSource.DataSource as DataSet. The last line is added by you by the designer.

DataSet ds = new DataSet();

ds.ReadXml("Data.xml");

EmployeeBindingSource.DataSource = ds;
this.reportViewer1.RefreshReport();

 

Listing 2.

Step 5. Build and Run

That's all. Build and run the application. The output looks like Figure 7.

Figure 7.

Summary

In this article, I talked about how we can generate reports from a DataSet object using the ReportViewer control available in Visual Studio 2005. 

Login to add your contents and source code to this article
share this article :
post comment
 

Hello, Can i get a VB./net code for this as the 4 lines code at the bottom does not work and it gives errors for dataset and so on rgds hari vaag

Posted by hari vaag Jan 08, 2012

Hi guys. I'm posting a question quite late I realise that but hoping someone will pick this. I'd like to add two datetextboxes and filter the report on them e.g. startDate, endDate. Any clue how to achieve this?

Posted by Jakub Jurcisin May 03, 2011

http://www.packtpub.com/article/creating-report-with-visual-studio-2008

  go through this link  , this will help u a lot. enjoy

Posted by madu babu Aug 11, 2010

Hi I m using sqlCe 3.5 & C# .Net compact edition + microsoft ReportViewer.

I have one report having 1 master & 3 detail sections.
these details sections & master details are stored in one dataset with four tables (including separate table for master records).

I m not able to configure the report with this dataset as datasource.

I Fill dataset with individual dataTablw with master-detail records. & adding multiple datasources to reportviewer as follows.


cmd = DataAccess.CreateCommand(query,1 queryparamtable, conn);
                adapter = new SqlCeDataAdapter(cmd);
                adapter.Fill(dataset, "Table1");
cmd = DataAccess.CreateCommand(query2, queryparamtable, conn);
                adapter = new SqlCeDataAdapter(cmd);
                adapter.Fill(dataset, "Table2");
cmd = DataAccess.CreateCommand(query3, queryparamtable, conn);
                adapter = new SqlCeDataAdapter(cmd);
                adapter.Fill(dataset, "Table3");
cmd = DataAccess.CreateCommand(query4, queryparamtable, conn);
                adapter = new SqlCeDataAdapter(cmd);
                adapter.Fill(dataset, "Table4");


then ,

BindingSource dataSource = new BindingSource();
                dataSource.DataSource = dataset;
                dataSource.DataMember = "Table1";
ReportDataSource reportDataSource = new ReportDataSource();
                reportDataSource.Name = dataSource.DataMember;
                reportDataSource.Value = dataSource;

BindingSource dataSource = new BindingSource();
                dataSource.DataSource = dataset;
                dataSource.DataMember = "Table2";
ReportDataSource reportDataSource = new ReportDataSource();
                reportDataSource.Name = dataSource.DataMember;
                reportDataSource.Value = dataSource;


BindingSource dataSource = new BindingSource();
                dataSource.DataSource = dataset;
                dataSource.DataMember = "Table3";
ReportDataSource reportDataSource = new ReportDataSource();
                reportDataSource.Name = dataSource.DataMember;
                reportDataSource.Value = dataSource;


BindingSource dataSource = new BindingSource();
                dataSource.DataSource = dataset;
                dataSource.DataMember = "Table4";
ReportDataSource reportDataSource = new ReportDataSource();
                reportDataSource.Name = dataSource.DataMember;
                reportDataSource.Value = dataSource;

My .rdlc file having One list component . In thet i have 5 fiels & two table component.
These table components have details fields.
but somehow its not working.
please can anyone tell me whats going wrong there.

Posted by Yogesh Morankar Aug 05, 2010

Hi Mahesh,

I gone through the links you provided. But I found that they are specific to crystal report , its not helping me with Microsoft ReportViewer. Would you like to suggest something which will help me with MS ReportViewer specifically as I m getting same problem.

Posted by Yogesh Morankar Aug 02, 2010
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Become a Sponsor