Blue Theme Orange Theme Green Theme Red Theme
 
Nevron Chart
Home | Forums | Videos | Photos | Blogs | Beginners
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » Reporting Services » Building Reports from a DataSet using ReportViewer

Building Reports from a DataSet using ReportViewer


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.

Author Rank:
Total page views :  52866
Total downloads : 
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Become a Sponsor

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. 


Login to add your contents and source code to this article
 Article Extensions
Contents added by Atif on Jun 04, 2010
I have understood every thing in the tutorial but only one line of code where you say

ds.ReadXml("Data.xml")

I don't understand where this data.xml file has come from. Please help

Contents added by naveen on Dec 29, 2009
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
 About the author
 
Mahesh Chand
Mahesh is a software developer with over 13 years of experience building systems for Financial and Banking, Engineering & Architectural, Imaging, Construction, Biological & Pharmaceuticals, Healthcare and Education industries. His expertise is Windows Forms, ASP.NET, Silverlight, WPF, WCF, Visual Studio 2010, SQL Server, and Oracle. If you are looking for a Sharepoint, Windows Forms, ASP.NET, WPF, Silverlight, C#, VB.NET, Oracle, and SQL Server Consultant in Philadelphia area or remote location, drop me a line at MAHESH [AT] C-SHARPCORNER [DOT] COM.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
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.
SQL and .NET performance profiling in one place
Investigate SQL and .NET code side-by-side with ANTS Performance Profiler 6, so you can see which is causing the problem without switching tools.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
60 FREE UI Controls from DevExpress
Register for your FREE copy on over 60 free presentation controls from DevExpress - Absolutely Free-of-Charge without any royalties or distribution costs. Visit Devexpress.com/60 today. Free controls include advanced lists box, dropdown calendar, rich text edit, spin edit, tab control and so much more!

DevExpress engineers feature rich presentation controls and reporting tools for WinForms, ASP.NET, WPF, and Silverlight. Our technologies help you build your best, see complex software with greater clarity and deliver compelling business solutions for Windows and the web in the shortest possible time.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010
Visualize your workspace with new multiple monitor support, powerful Web development, new SharePoint support with tons of templates and Web parts, and more accurate targeting of any version of the .NET Framework. Get set to unleash your creativity.
Nevron Chart for .NET 2010.1 Now Available
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.
Developer-Ready ASP.NET 2.0 Web Hosting with 3 MONTHS FREE
Now supporting .NET 3.0 Framework with Windows Workflow Foundation, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), windows CardSpace (WCS)! Providing more flexibility for Developers with Web Services Support and a User/Permission Manger. Also supporting MS SQL 2005/2000 with Real-Time Backups, FREE Automated Attach .MDF Tool, FREE SQL Restore and Shrink SQL DB Tools, and SQL
Read the Top 10 Books for Microsoft Developers, 15 Days FREE
Read the Top 10 Books for Microsoft Developers, 15 Days FREE
Try Safari Books Online - 15 Days FREE + 15% Off for 1 Year
Try Safari Books Online - 15 Days FREE + 15% Off for 1 Year
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
ASP.Net 4 Hosting is here
Become a Sponsor
 Comments
Dynamic Column selection by Srivatsa On June 4, 2007
Hi Mahesh Is there any way to get the columns dynamically during runtime using Reports. Say i want Name and Age initially. I select Name and Age in the form ... i generate a report out of it ... Again next time i want Name,Age and Phone... Is this supported in the ReportViewer in .NET Thanx in advance for the info Srivatsa
Reply | Email | Delete | Modify | 
Re: Dynamic Column selection by Mahesh On June 4, 2007

Srivatsa,
To do this, I would suggest you create a report with all possible columns in your database and show/hide columns dynamically. So you may have to pass parameters to the report for each column as True or False and column will be displayed only when the parameter value if True. So if you want Age and Name columns to be displayed only, pass True for these two columns and False for rest. On Suppress property of the Reports column, check if Parameter value is False then return True else return False.

Reply | Email | Delete | Modify | 
Re: Dynamic Column selection by Vikash On December 18, 2007

Thanks for such a nice tutorial.

I have a small question.

Can we have name, age and phone no in 3 rows and the corresponding value in column i.e. I want to transpose the rows and columns and want to generate dynamic column(for every user 1 column will be added).

Can we do this?

Reply | Email | Delete | Modify | 
Re: Re: Dynamic Column selection by Yogendra On May 23, 2010
HI, Can you please help me to use image at runtime to show image for each and every record.

Thanks
Reply | Email | Delete | Modify | 
Parameters by Thabang On July 16, 2007
How do i pass parameter values on reportviewer using textboxes and a button to query
Reply | Email | Delete | Modify | 
cant format the report by mahmoud On July 25, 2007
hello mahesh , the tutorial is very nice but when i creat the report in the datasource conf wizard i didnt find the reportxml file in the list but all the steps was exist and working expect that step (imusing mysql database and and ODBC V 3.51.12 ) , when i was trying to creat and format the report , i didnt find the table i need in the toolbox it was empty please help ..............
Reply | Email | Delete | Modify | 
Re: cant format the report by Mahesh On July 31, 2007

The file is .XSD.

You need to create a typed DataSet from the database table. If it was not created using the connection wizard, you can add a new item to the project and select DataSet query. It will add a .XSD file. Now, simply drag database table from Database Explorer.

Alternatively, you can create a DataAdapter by dragging OdbcDataAdapter from the Toolbox and connect it with your database and get data from tables using the Wizard. Once OdbcDataAdapter is created, right click on it and select Generate DataSet. This will generate a typed DataSet (.XSD) file.

Reply | Email | Delete | Modify | 
Step by step tutorial did not work by Edward On November 5, 2007

Tried to do getting started methodology with Web page.  However, Web page shows error message no datasource has been instantiated for datasource1_tblComments.

Then tried to follow this tutorial for Web page.  However data menu for WebForm does not show Add New datasource and there is no way to bring up Data Source Configuration Wizard. 

In addition, initially the building datasource wizard would not allow me to select the stored procedure I desired to show the data, instead reporting that it did not exist.   I had to created another datasource for the same database and the sa user in order to see the stored proc I desired to use. 

I then created a windows Forms project to test out the methodology in this article.  However, after dragging the table onto the report visual interface, I was unable to see any columns in the stored Procedure I desired to use to drag into the Report Table. 

At no point was I able to get to the point of displaying data from my stored procedure.

I then returned to my web application and created the report object using a table in the  database vice the real resultset that I need.  It was my intent to create the report then change the dataset to that I need the way it was done it the above example.  (I was not and am not sure how this change of data set is going to update the columns established in the table in the report object interface designer, but I decided that was an issue I could tackle later.)  After adding the report to the  reportviewer, I tried to change the datasource of the the objectdatasource that appeared on the page.  This was not permitted.

I then tried to create a local report object programmatically as shown in the MSDN localreport class example.  I was able to successfully add the new datasource to the localreport object instantiated, but then was not able to programmatically add the localreport thus created to the reportview as the localreport property of the viewer is readonly.

Perhaps you could redo the above step by step example as a web page using a stored procedure with parameters (all with default values) as the data source?  Thanks

 

Reply | Email | Delete | Modify | 
Report Generation without using database connection by santo On December 31, 2007
Hi, Thanks for the article.It works fine if i specify databasec connection and create a dataset with table from dataset.But wht iam trying to do is iam getting a string of values which i need to convert it to dataset by declaring columns in code.Then i need to display the report with the data in dataset.Currently i created a xsd and able to drag columns to the table in rlc file.DataSet ds1 = new DataSet(); ds1 = GetVlaues(); int count = ds1.Tables[0].Rows.Count; DataTable1BindingSource.DataSource = ds1; this.reportViewer1.RefreshReport(); THis is the code iam using in form load event.Iam getting runtime error as a datasource instance as not been supplied for datasource. Let me know if you can help with this issue. Thank you
Reply | Email | Delete | Modify | 
Allow report user to change dataset by Dan On February 11, 2008
I want to allow the user to change which query(dataset) they are running by selecting a value from a report parameter. Any Ideas how this could be done?
Reply | Email | Delete | Modify | 
I need to use a single rdlc file in many projects in windows application by logesh On February 29, 2008
I need to create a single rdlc file and use that in many projects, i have written the rdlc file in Class library, i a windows application.can you please help me out. Thanks in advance
Reply | Email | Delete | Modify | 
Re: I need to use a single rdlc file in many projects in windows application by Mahesh On April 7, 2009
There are two ways to do it.
If you are using Crystal Enterprise, you can reference a server report in multiple applications. Otherwise. you will have to copy your report to each project individually.
Reply | Email | Delete | Modify | 
Can i make the column in dynamic order by Eng Wei On March 16, 2008
Hi Mahesh I want to move the order of column, let say by default the column is Name, Age, Phone. I want to make it dynamic for user to let them change the order of column to Age, Name, Phone. Name, Age, Phone and so on. Is this possible to do that? Thanks
Reply | Email | Delete | Modify | 
Using codes by Martin On October 12, 2008
Great tutorial. but what if I want to do all this using codes, how do I go about it. I mean using my own sql query
Reply | Email | Delete | Modify | 
Re: Using codes by Mahesh On October 12, 2008
I would probably execute SQL query and fill in a DataSet and then go from there.
Reply | Email | Delete | Modify | 
Build report by Anthony On October 21, 2008
Hi Mahesh I saw some of your articles and are very interesting. I am learning visual basic 2005 and i did a table in SQL 2005, using Visual Studio 2005 and attached to a form. Everyday a post in it some transactions and i specify them using the date i posted it. So at the end of the month i want to pull data to the report using a date between 09/01/08 to 09/30/08, but i can't do that. Could you please help me how to do that? Thanks Tony
Reply | Email | Delete | Modify | 
Re: Build report by Mahesh On April 7, 2009
You need to pass start date and end date parameters to your report OR you can pass these parameters to your database and get data for that date range. In the Crystal Reports section, I have any article on how to pass parameters to a crystal report. Go to Crystal Reports section of this site.
Reply | Email | Delete | Modify | 
many tnx by Charlston On July 7, 2009
chand can u help me present a dynamically reportviewer datasource sample...tnx
Reply | Email | Delete | Modify | 
Re: many tnx by Mahesh On July 15, 2009
Look at Crystal Reports section on C# Corner. Link is in the left side of home page.
Reply | Email | Delete | Modify | 
Problem with Crystal Report with dynamic and static datas in vb.net by naveen On December 29, 2009
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
Reply | Email | Delete | Modify | 
Report Viewer in VS 2005 by Jayson On February 16, 2010
Hi. your post about report viewer helps me alot..
Reply | Email | Delete | Modify | 
ASP.Net 4 Hosting is here
 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2010.8.14
 © 2010  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.