ARTICLE

Building Reports from a DataSet using ReportViewer

Posted by Mahesh Chand Articles | Reports in VB.NET May 11, 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:

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.

Dim ds As DataSet = New DataSet()

ds.ReadXml("Data.xml")

EmployeeBindingSource.DataSource = ds

Me.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. 

share this article :
post comment
 

HI, Can you please help me to use image at runtime to show image for each and every record.

Thanks

Posted by Yogendra May 23, 2010

Hi. your post about report viewer helps me alot..

Posted by Jayson Dagsaan Feb 16, 2010

Hi

 Mahesh ,

    I have seen ur dynamic reports creating forum.

 But i have a problem.

  My requirement is ,in a crystal report ,there are both static and dynamic records
to be displayed.

 So i tried both table joined in a single dataset ,but dint work.

 So i tried putting two data set ,but im getting error like crystal report server failed

 

 So how to bind two tables in one dataset ,and how to give sql connections and reportserver

 Kindly guide me.

Regards

Naveen

Posted by naveen Dec 29, 2009

Look at Crystal Reports section on C# Corner. Link is in the left side of home page.

Posted by Mahesh Chand Jul 15, 2009

chand can u help me present a dynamically reportviewer datasource sample...tnx

Posted by Charlston Bajuyo Jul 07, 2009
Nevron Diagram
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. Visit DynamicPDF here
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor