Click here to Skip to main content
11,639,861 members (62,632 online)
Click here to Skip to main content

Dynamic Binding Of RDLC To ReportViewer

, 17 Dec 2008 CPOL 210.4K 7.3K 60
Rate this:
Please Sign up or sign in to vote.
This article demonstrates how to bind reportviewer control dynamically with Datasource at runtime.

Introduction

I was struggling to find the solution to bind rdlc dynamically to reportviewer .We had a scenario were we migrated our application from .NET 1.1 to .NET 2.0 .After migration embedding reportviewer with explicit objectdatasource was creating a major issue. Whenever we try to assign dataset reference to objectdatasource the IDE shuts down abruptly for no reason. This was one of the bug we encountered while developing. We searched a lot but this unique problem was faced by very few hence the correct solution was not available. We then planned to bind the reportviewer at runtime without taking objectdatasource. Below is the procedure to go about it.

Design Architecture

Sample Image - maximum width is 600 pixels

Production Deployment Issues And Its Resolution

During deployment if the source code is published and precompiled is created, one has to be extra careful to do below activity.

  • Create precompiled for deployment.
  • Replace the .RDLC file from the precompiled source code with the original *.rdlc file. The reason behind this is precompiled corrupt the .rdlc internal code constructs. In order to maintain its schema and design we suggest you to replace the precompiled rdlc with original one.

Prerequisite

Note: In production web server we have light weight .net framework installed so report viewer is not available. Due to which it will throw report viewer dlls not exist.

Click here to install Reportviewer.exe.

Problem Statement

There is customer table with two field customerid and customerName. We need to generate report for this datatable in reportviewer without using objectdatasource.

Create Dataset Schema

Follow the step given below to define dataset schema without connecting to any datasource. This is manual process. This is require each column of the table to added one by one as shown below.

Sample Image - maximum width is 600 pixels

Click on the toolbox icon to proceed further.

Sample Image - maximum width is 600 pixels

Add column to the schema as given below.

Sample Image - maximum width is 600 pixels

Create Report RDLC With Parameter

One needs to drag table from toolbox to panel to draw table section as body part of report. Once done that click on Show Data Source one will view the dataset schema section as given below. Drag columns into table row just below header section. Select Reportparameter from main menu and add one to report. This parameter value can be passed from aspx's form to Reportviewer's RDLC panel.

Sample Image - maximum width is 600 pixels

Drag textbox in report screen and right click to select property of it.

Sample Image - maximum width is 600 pixels

Click on Fx button of value field to associate this textbox field value to parameter set field variable.

Sample Image - maximum width is 600 pixels

Sample Image - maximum width is 600 pixels

Embed Report Viewer in Aspx

Drag Reportviewer into aspx design page and add localpath of the RDLC file. One can invoke storeprocedure and get the database resultset and can assign it to reportviewer. For running a version of this demo, I included hardcoded resultset.

 private void BindReportViewer()
    {
        ReportViewer1.Visible = true;
               
        //Invoke Stored procedure With Input parameter to it.
        //DataSet dsReport = objSP.GetTable(storedProcedure,txtParameter.Text));
        //Hardcoded Values.
        IList >Customer< customerList = new List>Customer<();
        customerList.Add(new Customer(1,"Santosh Poojari"));
        customerList.Add(new Customer(2, "Santosh Poojari1"));
        customerList.Add(new Customer(3, "Santosh Poojari2"));
       
        ReportParameter[] param = new ReportParameter[1];
        param[0] = new ReportParameter("Report_Parameter_0",txtParameter.Text);
        ReportViewer1.LocalReport.SetParameters(param);

        ReportDataSource rds = new ReportDataSource
			("DataSet1_Customers_DataTable1", customerList);
        ReportViewer1.LocalReport.DataSources.Clear();
        ReportViewer1.LocalReport.DataSources.Add(rds);
        ReportViewer1.LocalReport.Refresh();
    }

Below is the expected output.

Sample Image - maximum width is 600 pixels

Conclusion

Hope this article will help those who are working on similar problem statement. Any suggestion and advice will help me to improve the quality of this article.

History

  • First Cut-Initial Draft: 11-Dec-2008
  • Updated Production Deployment Issues And Its Resolution: 19-Dec-2008

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

santosh poojari
Technical Lead
India India
Whatsup-->Exploring--> MVC/HTML5/Javascript & Virtualization.......!
www.santoshpoojari.blogspot.com

You may also be interested in...

Comments and Discussions

 
QuestionRDLC reports can also be created by passing Dataset values to it Pin
Member 1096996515-Oct-14 20:39
memberMember 1096996515-Oct-14 20:39 
GeneralMy vote of 3 Pin
Ibrahim Islam4-Jun-14 9:21
memberIbrahim Islam4-Jun-14 9:21 
Questionmore than one table Pin
LedCobain23-Aug-13 9:53
memberLedCobain23-Aug-13 9:53 
AnswerRe: more than one table Pin
santosh poojari25-Aug-13 23:04
membersantosh poojari25-Aug-13 23:04 
QuestionNice one Pin
faisalabdulelahi12-Mar-13 23:04
memberfaisalabdulelahi12-Mar-13 23:04 
GeneralMy vote of 5 Pin
gaga blues7-Nov-12 20:24
membergaga blues7-Nov-12 20:24 
QuestionConnect to sql database? Pin
nht200723-Sep-11 15:39
membernht200723-Sep-11 15:39 
GeneralMy vote of 5 Pin
Reza Samadabadi4-Jul-11 10:09
memberReza Samadabadi4-Jul-11 10:09 
GeneralWinForm App Pin
Member 83208626-Aug-10 4:39
memberMember 83208626-Aug-10 4:39 
GeneralWin Form Project Pin
Member 83208626-Aug-10 4:20
memberMember 83208626-Aug-10 4:20 
GeneralExcellent Pin
sdking2-May-10 14:27
membersdking2-May-10 14:27 
GeneralRe: Excellent Pin
santosh poojari8-Jun-10 22:30
membersantosh poojari8-Jun-10 22:30 
QuestionConnect to database? Pin
aspnoobie19-Mar-10 5:27
memberaspnoobie19-Mar-10 5:27 
AnswerRe: Connect to database? Pin
santosh poojari31-Mar-10 1:54
membersantosh poojari31-Mar-10 1:54 
This is straight forword
Connect to database using ADO object and fetch result in dataset.
Like

IList <Customer> customerList = new List<Customer>();

OleDbConnection aConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\sample.mdb");

//create the command object and store the sql query
OleDbCommand aCommand = new OleDbCommand("select * from sampleTable", aConnection);
try
{
aConnection.Open();

OleDbDataReader aReader = aCommand.ExecuteReader();


//Iterate throuth the database
while(aReader.Read())
{

customerList.Add(new Customer(aReader.GetInt32(0),aReader.GetString(1)));
}

//close the reader
aReader.Close();

//close the connection Its important.
aConnection.Close();

Rest follows same........
ReportParameter[] param = new ReportParameter[1];
param[0] = new ReportParameter("Report_Parameter_0",txtParameter.Text);
ReportViewer1.LocalReport.SetParameters(param);

ReportDataSource rds = new ReportDataSource
("DataSet1_Customers_DataTable1", customerList);
Happy Coding
"San"




GeneralVery well explained Pin
Gandalf - The White20-Nov-09 0:29
memberGandalf - The White20-Nov-09 0:29 
GeneralRe: Very well explained Pin
santosh poojari24-Nov-09 20:05
membersantosh poojari24-Nov-09 20:05 
GeneralRe: Very well explained Pin
kukawalkar30-Dec-11 0:03
memberkukawalkar30-Dec-11 0:03 
Generali vote 5!!! Pin
Oslec15-Jan-09 16:26
memberOslec15-Jan-09 16:26 
GeneralRe: i vote 5!!! Pin
santosh poojari19-Jan-09 0:45
membersantosh poojari19-Jan-09 0:45 
GeneralVery Good Artical Pin
Dhillon24-Dec-08 6:58
memberDhillon24-Dec-08 6:58 
GeneralRe: Very Good Artical Pin
santosh poojari28-Dec-08 23:36
membersantosh poojari28-Dec-08 23:36 
GeneralMy vote of 1 Pin
Samer Abu Rabie18-Dec-08 2:37
memberSamer Abu Rabie18-Dec-08 2:37 
GeneralRe: My vote of 1 Pin
santosh poojari18-Dec-08 18:00
membersantosh poojari18-Dec-08 18:00 

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
Web03 | 2.8.150731.1 | Last Updated 18 Dec 2008
Article Copyright 2008 by santosh poojari
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid