Click here to Skip to main content
11,487,401 members (69,699 online)
Click here to Skip to main content

Dynamic reports with Microsoft local report engine

, 23 Aug 2006 159K 7.5K 84
Rate this:
Please Sign up or sign in to vote.
An article for local report engine incorporate in MS VS 2005 and generation of dynamic reports

Demo Dynamic's Report

Introduction

In this article I will try to show you the magnificent functionality of the local report engine, which the Microsoft Corporation incorporated in the new version of MS Visual Studio 2005.
Every developer knows very well how important it is to have at one’s disposal a powerful, flexible and user friendly report engine.  It doesn’t matter what type of project we develop, because in 95% of all possible projects, we will need to implement some kind of report functionality. Knowing this fact, Microsoft incorporated in their new version of theMS Visual Studio v.2005 this functionality. The local reports are similar to the Microsoft Reporting Services under MS SQL 2005. A few months ago I wrote an article on  this topic. " The new reporting horizons with Microsoft Reporting Services 2005"  The best options of this local report engine are that when you make local reports you may easily switch to and convert them to "remote" reports and run them as part of the Microsoft Reporting Services engine. Another powerful functionality of the local reports is the possibility to use many data sources in a handy and flexible way. And last, but not least, all this power is free. You do not need to buy or install any additional licenses to support many users or applications. Fantastic, right?

Creating an report's dataset


The test/demo table in design mode   The demo dataset in design mode

Our first task is to create a dataset with all tables and relations, which will be the basis for building our report. We may skip this stage and provide directly our dataset to the report engine, but we need this dataset for our next steps. When we create and design a report, we need to see visually all available tables, their fields and data typeof the tables’ fields. This will make our life as developers easier. For the purpose of the current demo, I will create a simple dataset with only one simple table.  Please forgive me!

Creating an report

Report in design mode

Our next step and task is to create and design for our needs of this demo two simple reports. This can be done by going to the project's folder and by right clicking of the mouse choose "Add a new item" andfrom the list menu selecting an empty report file for creating a report using Microsoft reporting technology. You have to set the name of this new report and location. In our case I will give the name "demoReport.rdlc" and location the "Rdl" folder from my demo project root folder. Also by same way we will create and the second one report. The main purpose is to create two similar reports with a few difference as graphic's types and title for an example. You may see them in subfolder "Rdl/Tamplates".Actually the generated reports from Microsoft report designer are an XML files and you may open them with Notepad. The new in this article unlike the artilce for static reports is " The new reporting horizons with Microsoft reporting technology" that we will use generated reports for tamplates. And next action is to create a new C# method which will generate on fly every time the necessary report. The diffrence is small but very important. Below, I will show you a few important things in my code. If you keep them I promise you that your life will be easier working with Microsoft reporting technology.

//
//Here is code behind Init event of report viewer 
//Initialize the report options always in "Init" event of 
//Report Viewer .............
//
protected void rptViewer_Init(object sender, EventArgs e)
{
    rptViewer.ProcessingMode = ProcessingMode.Local;
    rptViewer.LocalReport.EnableExternalImages = false;
    rptViewer.LocalReport.EnableHyperlinks     = false;
    rptViewer.Visible                          = true;
    rptViewer.ShowBackButton                   = true;
    rptViewer.ShowDocumentMapButton            = true;
    rptViewer.ShowExportControls               = true;
    rptViewer.ShowFindControls                 = true;
    rptViewer.ShowParameterPrompts             = false;
    rptViewer.ShowReportBody                   = true;
    rptViewer.SizeToReportContent              = true;
    rptViewer.ShowToolBar                      = true;
    rptViewer.ShowZoomControl                  = true;
    rptViewer.DocumentMapCollapsed             = true;
}

//
//Here is code behind binding of report
//
protected void rptViewer_DataBinding(object sender, EventArgs e)
{
   string fD            = fromDate.VisibleDate.ToString("dd/MM/yyyy").Trim();
   string tD            = toDate.VisibleDate.ToString("dd/MM/yyyy").Trim();
   DataTable dt         = new DataTable();
   dt                   = GetData();
   //
   // Please notice that ReportDataSet is combination
   // from name of dataset and table/tables
   // in thie dataset "dsReports_myReport" !!!
   //
   string bindDs_name                = "dsReports_myReport";
   string reportName                 = "demoReport.rdlc";
   rptViewer.LocalReport.DisplayName = "Demo Report";
   rptViewer.LocalReport.ReportPath  = "Rdl/" + reportName;
   ReportParameter from_Date         = new ReportParameter();
   ReportParameter to_Date           = new ReportParameter();
   ReportParameter filterFromDate    = new ReportParameter();
   ReportParameter filterToDate      = new ReportParameter();
   from_Date.Name                    = "pFromDate";
   to_Date.Name                      = "pToDate";
   filterFromDate.Name               = "filterFromDate";
   filterToDate.Name                 = "filterToDate";
   from_Date.Values.Add(fD);
   to_Date.Values.Add(tD);
   filterFromDate.Values.Add(fromDate.SelectedDate.ToString());
   filterToDate.Values.Add(toDate.SelectedDate.ToString());
   ReportParameter[] myReportParams  = 
    new ReportParameter[] {from_Date, to_Date, filterFromDate, filterToDate};
   rptViewer.LocalReport.SetParameters(myReportParams);
   if (dt != null)
   {
    rptViewer.LocalReport.DataSources.Clear();
    rptViewer.LocalReport.DataSources.Add(new 
              ReportDataSource(bindDs_name.Trim(), dt));
    dt.Dispose();
   }    
}

//
// Do not forget to include and next name space in using clause
//
using Microsoft.Reporting.WebForms;

Conclusion

As final words, I would like to thank you for your patience and I hope that this article will be really useful to you and will give you the right direction when you try to implement Microsoft reporting technology to build an dynamic report and compare which approach is better for you, to use local reports as you use them as static or dynamic or to use Microsoft Repoting Services. Please download the sources from this article, and take a look.<!----------------------------- Article Ends ----------------------------->

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Dimitar Madjarov
Software Developer (Senior) http://www.uplandsoftware.com/
Canada Canada
Canada, Quebec, Laval,
Currnet position: Senior ASP.NET Developer
Company: Uplandsoftware Inc
Certificates: MCITP, MCP, MCTS

Comments and Discussions

 
QuestionReport Display Name Pin
Member 1042401124-Nov-13 23:22
memberMember 1042401124-Nov-13 23:22 
GeneralMy vote of 5 Pin
AhmadReza 202016-May-13 9:42
memberAhmadReza 202016-May-13 9:42 
QuestionError! Pin
muhtarqong27-Mar-13 11:54
membermuhtarqong27-Mar-13 11:54 
QuestionIs this limited to VS 2005 and later? Pin
nolesce24-Sep-09 11:37
membernolesce24-Sep-09 11:37 
Generaldeploying Pin
Dasiths31-May-09 6:50
memberDasiths31-May-09 6:50 
GeneralThank you Pin
geealin31-Jan-09 3:06
membergeealin31-Jan-09 3:06 
Questionhosting Pin
kkp10020-Jan-09 4:33
memberkkp10020-Jan-09 4:33 
Questionhelp : ReportGenerator Pin
Member 73210620-Mar-08 18:29
memberMember 73210620-Mar-08 18:29 
QuestionCan it be even more dynamic (with unbound fields)? Pin
strogg18-Oct-07 11:27
memberstrogg18-Oct-07 11:27 
AnswerRe: Can it be even more dynamic (with unbound fields)? Pin
R V Mishra5-Jan-09 7:45
memberR V Mishra5-Jan-09 7:45 
GeneralDynamically generate RDLC from DataSet Pin
nbohr99a8-Sep-07 17:15
membernbohr99a8-Sep-07 17:15 
QuestionCalendar Style Pin
prometei27-Aug-07 6:18
memberprometei27-Aug-07 6:18 
Generaldataset question Pin
aguidas25-Jun-07 7:40
memberaguidas25-Jun-07 7:40 
GeneralRe: dataset question Pin
Dimitar Madjarov25-Jun-07 22:13
memberDimitar Madjarov25-Jun-07 22:13 
GeneralThank you very much Pin
Ron Icard23-May-07 3:55
memberRon Icard23-May-07 3:55 
GeneralRe: Thank you very much Pin
Dimitar Madjarov27-May-07 23:48
memberDimitar Madjarov27-May-07 23:48 
QuestionHow???? Pin
femi ojemuyiwa1-Mar-07 3:51
memberfemi ojemuyiwa1-Mar-07 3:51 
QuestionHow do i hide or show columns in Table embeded in rdlc file? Pin
Raghav20041-Mar-07 0:35
memberRaghav20041-Mar-07 0:35 
GeneralUrgent Pin
Sarfaraj Ahmed15-Feb-07 5:57
memberSarfaraj Ahmed15-Feb-07 5:57 
GeneralRe: Urgent Pin
Dimitar Madjarov15-Feb-07 21:06
memberDimitar Madjarov15-Feb-07 21:06 
QuestionCustom Business Entity Pin
mrmaki27-Jan-07 13:39
membermrmaki27-Jan-07 13:39 
GeneralUrgent Pin
Member #192833728-Dec-06 12:57
memberMember #192833728-Dec-06 12:57 
GeneralRe: Urgent Pin
Dimitar Madjarov28-Dec-06 22:25
memberDimitar Madjarov28-Dec-06 22:25 
Generalmsg Pin
vijitha2818-Dec-06 17:55
membervijitha2818-Dec-06 17:55 
GeneralRe: msg Pin
Dimitar Madjarov18-Dec-06 21:14
memberDimitar Madjarov18-Dec-06 21:14 
QuestionDoes the local report work without session? Pin
x2bzhang12-Dec-06 12:24
memberx2bzhang12-Dec-06 12:24 
AnswerRe: Does the local report work without session? [modified] Pin
Dimitar Madjarov12-Dec-06 22:08
memberDimitar Madjarov12-Dec-06 22:08 
Questionhow group by records at dataset Pin
jafar_mahmoudi23-Sep-06 7:38
memberjafar_mahmoudi23-Sep-06 7:38 
AnswerRe: how group by records at dataset Pin
varatha27-Aug-09 12:20
membervaratha27-Aug-09 12:20 
QuestionHow it works? Pin
William Ng30-Aug-06 6:53
memberWilliam Ng30-Aug-06 6:53 
AnswerRe: How it works? Pin
Dimitar Madjarov30-Aug-06 9:41
memberDimitar Madjarov30-Aug-06 9:41 
GeneralRe: How it works? Pin
William Ng30-Aug-06 14:33
memberWilliam Ng30-Aug-06 14:33 
GeneralRe: How it works? Pin
Dimitar Madjarov30-Aug-06 22:13
memberDimitar Madjarov30-Aug-06 22:13 
QuestionThis version of report engine allow embeded html? Pin
carloscmv28-Aug-06 12:23
membercarloscmv28-Aug-06 12:23 
AnswerRe: This version of report engine allow embeded html? Pin
Dimitar Madjarov28-Aug-06 22:29
memberDimitar Madjarov28-Aug-06 22:29 
GeneralRe: This version of report engine allow embeded html? Pin
carloscmv29-Aug-06 6:31
membercarloscmv29-Aug-06 6:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150520.1 | Last Updated 23 Aug 2006
Article Copyright 2006 by Dimitar Madjarov
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid