Loading

Load RDLC Report Using Report Viewer Programmatically

Submitted by: 
Visitors have accessed this post 11331 times.




This tutorial will guide you on how to programmatically load the RDLC Report into Report Viewer Control.

I search the internet on how to do this but I can’t find one. There are answers in some forum site but the solution is not clear and don’t have a sample source code.

In my previous tutorial I discuss the solution for the error “The definition of the report 'Main Report' is invalid”. This time let’s talk about on how we can load the RDLC report programmatically using its own DataSet.

The source code I implemented here is very simple but it’s the best solution. Unlike with other samples on the internet, they use a lot of code to do a very simple task.

Here’s a sample of the code to display the RDLC Report programmatically.

  1. Private Sub frmReport_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  2. Dim rptDataSource As ReportDataSource
  3.  
  4. Try
  5. With Me.ReportViewer1.LocalReport
  6. .ReportPath = "Reports\" & strReport & ".rdlc"
  7. .DataSources.Clear()
  8. End With
  9.  
  10. Select strReport
  11. Case "Employees"
  12. Dim ds As New Load_RDLC_Report_Programmatically.EmployeesDataSet
  13. Dim da As New Load_RDLC_Report_Programmatically.EmployeesDataSetTableAdapters.EmployeesTableAdapter
  14.  
  15. da.Fill(ds.Employees)
  16.  
  17. ' Use the same name as defined in the report Data Source Definition
  18. rptDataSource = New ReportDataSource("EmployeesDataSet", ds.Tables("Employees"))
  19. Case "Customers"
  20. Dim ds As New Load_RDLC_Report_Programmatically.CustomersDataSet
  21. Dim da As New Load_RDLC_Report_Programmatically.CustomersDataSetTableAdapters.CustomersTableAdapter
  22.  
  23. da.Fill(ds.Customers)
  24.  
  25. ' Use the same name as defined in the report Data Source Definition
  26. rptDataSource = New ReportDataSource("CustomersDataSet", ds.Tables("Customers"))
  27. End Select
  28.  
  29. Me.ReportViewer1.LocalReport.DataSources.Add(rptDataSource)
  30.  
  31. Me.ReportViewer1.SetDisplayMode(Microsoft.Reporting.WinForms.DisplayMode.PrintLayout)
  32. Catch ex As Exception
  33. MessageBox.Show(ex.Message, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
  34. End Try
  35. End Sub

Download the source code and try for yourself. Don’t hesitate to ask questions by leaving a message below.


Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Comments

Please can you help me?
I used SQL 2008 R2 for a file but if I try using data source to put it on a windows form application there is an error message I don't have permission to open the file.
I am the only person using the computer.

Please help.

my email is robbyde02@hotmail.com

Hi,
can you send the same code in c# please ??
please help me, i need it for my project.
my email : wayde.van.dyk@gmail.com

Thank you very much. This is a work around I needed for working in asp.net RDLC. It was easy to convert to C# asp.net web page.

Add new comment