This small tutorial will guide you on how to programmatically load the RDLC Report into ReportViewer Control. Lets talk about on how we can load the RDLC report programmatically using its own DataSet's. The source code implemented here is very simple and I think it’s the best solution. Unlike with other samples on the internet, they use lot of code to do this task. First Let us start from the beginning. The below one shows the UI for Reports.
Let us First write the code for filling up all the table
names into your combobox for which a report to be generated from a Sql Server
Database. For this I have taken pubs database.
public partial class frmReport : Form
{
private SqlConnection con;
private SqlCommand cmd;
private SqlDataReader dr;
public frmReport()
{
InitializeComponent();
//Connection String coming from app.config file.
string conString = ConfigurationManager.ConnectionStrings["pubsDB"].ConnectionString ;
//Query for loading the tables.
string sql = "Select Table_Name From INFORMATION_SCHEMA.Tables";
con = new SqlConnection(conString);
cmd = new SqlCommand(sql, con);
}
private void frmReport_Load(object sender, EventArgs e)
{
//Checking if the connection is closed
if (con.State==ConnectionState.Closed )
{
con.Open();
}
dr = cmd.ExecuteReader();
//Reading the records
while( dr.Read())
{
cboTables.Items.Add(dr[0]);
}
dr.Close();
con.Close();
this.reportViewer1.RefreshReport();
}
}
Now I am going to show you how to display the report for authors. Before I write the code to display programmatically you to need to create datasets for each table and design your report by adding the file to your Reports folder in your solution explorer. Following are the screen shots for adding dataSet, Report.rdlc and display of the project structure.
Adding DataSet
Adding Report
Project Structure
The following is the code for displaying report
programmatically in ReportViewer control.
private void cboTables_SelectedIndexChanged(object sender, EventArgs e)
{
ReportDataSource rptDataSource;
try
{
string exefolder = Path.GetDirectoryName(Application.StartupPath);
reportViewer1.LocalReport.ReportPath = Path.Combine(exefolder,@"Reports\") + cboTables.SelectedItem.ToString() + ".rdlc";
reportViewer1.LocalReport.DataSources.Clear();
if (cboTables.SelectedItem.ToString() == "authors")
{
ReportsDemo.DataSets.authorsDataSet ds = new ReportsDemo.DataSets.authorsDataSet();
ReportsDemo.DataSets.authorsDataSetTableAdapters.authorsTableAdapter da = new ReportsDemo.DataSets.authorsDataSetTableAdapters.authorsTableAdapter();
da.Fill(ds.authors);
rptDataSource = new ReportDataSource("authorsDataSet", ds.Tables["authors"]);
this.reportViewer1.LocalReport.DataSources.Add(rptDataSource);
this.reportViewer1.SetDisplayMode(Microsoft.Reporting.WinForms.DisplayMode.Normal);
}
if (cboTables.SelectedItem.ToString() == "publishers")
{
ReportsDemo.DataSets.publishersDataSet ds = new ReportsDemo.DataSets.publishersDataSet();
ReportsDemo.DataSets.publishersDataSetTableAdapters.publishersTableAdapter da = new ReportsDemo.DataSets.publishersDataSetTableAdapters.publishersTableAdapter();
da.Fill(ds.publishers);
rptDataSource = new ReportDataSource("publishersDataSet", ds.Tables["publishers"]);
this.reportViewer1.LocalReport.DataSources.Add(rptDataSource);
this.reportViewer1.SetDisplayMode(Microsoft.Reporting.WinForms.DisplayMode.Normal);
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
The authors report is shown below:-