Microsoft Report Viewer in a Windows Application – Programmatically change a Local Report

What I need to do is this:
1.      On tab 5 on a Windows form I want a selection of 15 or so Radio Buttons.
2.      On tab 6 on the same windows form, I want a full screen Microsoft Report Viewer.
3.      I want to have the 15 or so Local Reports individually defined and saved in the project. (Each one would be linked to a table or tables in a typed data set which is also a part of the project.)
4.      When the user clicks on of the radio buttons, I want to have that report show up in the Report Viewer.
5.      So far, I have had no problems creating Test Reports using the Report Wizard.
6.       I can have the first report show up on the Report Viewer by using the smart tag on the top right hand side of the Report Viewer.  The Report Viewer Smart Tag allows you to select one of the reports in a drop down box and this all works fine when you run the project.
7.      However the process of programmatically changing the report over to another report seems to be very complicated and I need some help please.  The best I can come up with so far seems to change the Report Headings OK, but then the report shows with no data!
8.      What I have tried to do is to make up a button click procedure for each button change event, and get this to pass three variables down to a JPChangeReport sub procedure
9.      My code follows.  I have copied most of it from the code which was automatically generated from the smart tag, and some from code suggestions on the web or in Experts Exchange, but I still can’t seem to get it to work!
10.      I am also a little puzzled why the Report definition data source shows a table in the data set, but the auto code generated by the smart tag calls up a related Binding Source?
11.      Any help would be greatly appreciated.

Private Sub tab5OptTest_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles tab5OptTest.Click
        JPChangeReport("ExamPlannerDataSet1_Prices", "Me.PricesBindingSource", "ExamPlannerV2.Test for Prices.rdlc")
    End Sub

    Sub JPChangeReport(ByVal DataSourceName As String, ByVal DataSourceValue As String, ByVal ReportName As String)
        Dim ReportDataSource As Microsoft.Reporting.WinForms.ReportDataSource = New Microsoft.Reporting.WinForms.ReportDataSource
        Me.ReportViewer1.Reset() 'Clears previous report information
        Me.ReportViewer1.BorderStyle = System.Windows.Forms.BorderStyle.None
        Me.ReportViewer1.Dock = System.Windows.Forms.DockStyle.Fill
        ReportDataSource.Name = DataSourceName
        ReportDataSource.Value = DataSourceValue
        Me.ReportViewer1.LocalReport.DataSources.Add(ReportDataSource)
        Me.ReportViewer1.LocalReport.ReportEmbeddedResource = ReportName
        Me.ReportViewer1.LocalReport.ReportPath = ""
        Me.ReportViewer1.Location = New System.Drawing.Point(3, 3)
        Me.ReportViewer1.Name = "ReportViewer1"
        Me.ReportViewer1.Size = New System.Drawing.Size(987, 513)
        Me.ReportViewer1.TabIndex = 1
        Me.ReportViewer1.RefreshReport()
    End Sub

Open in new window

PolyviewAsked:
Who is Participating?
 
arunjeyachandranCommented:
I cant clearly understand from your question.Here is a piece of code for connect the crystal report source by locally .If any queries contact me .
CrystalDecisions.CrystalReports.Engine.ReportDocument report = new ReportDocument();
            string repName = "";
            repName = "C:\\CrystalReport1.rpt";
            report.Load(repName);

Open in new window

0
 
PolyviewAuthor Commented:
I was trying to use the Micorsoft Report Viewer with  Microsoft Reports as generated by the Report Wizard Template in Visual Studio.  This option does not use Crystal Reports.  I guess I can use Crystal Reports if I have too, but I would like to try using the Microsoft option first, if it is possible, and I think it is.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
13598Commented:
Here is an example
  ReportViewer1.LocalReport.ReportEmbeddedResource = "Yoursolutionname.yourreportname.rdlc"
                                       ReportViewer1.LocalReport.DataSources.Add(New ReportDataSource("yourdatsource", yourtable))
                                       ReportViewer1.LocalReport.Refresh()
0
 
13598Commented:
With your code:
Sub JPChangeReport(ByVal DataSourceName As String, ByVal DataSourceValue As String, ByVal ReportName As String)
        Dim ReportDataSource As Microsoft.Reporting.WinForms.ReportDataSource = New Microsoft.Reporting.WinForms.ReportDataSource
        Me.ReportViewer1.Reset() 'Clears previous report information
        Me.ReportViewer1.BorderStyle = System.Windows.Forms.BorderStyle.None
        Me.ReportViewer1.Dock = System.Windows.Forms.DockStyle.Fill
        ReportDataSource.Name = DataSourceName
        ReportDataSource.Value = DataSourceValue
         Me.ReportViewer1.LocalReport.ReportEmbeddedResource = ReportName
        Me.ReportViewer1.LocalReport.DataSources.Add(New ReportDataSource(ReportDataSource))
        Me.ReportViewer1.Location = New System.Drawing.Point(3, 3)
        Me.ReportViewer1.Name = "ReportViewer1"
        Me.ReportViewer1.Size = New System.Drawing.Size(987, 513)
        Me.ReportViewer1.TabIndex = 1
        Me.ReportViewer1.LocalReport.Refresh()
    End Sub

The key here is what exactly is the value of your Reportname variable? It needs to be yourvbsolutionname.yourreportname.rdlc.
What exactly is the value of your ReportDataSource variable.
0
 
PolyviewAuthor Commented:
Working on your first example only (Because it is simpler):
1.  the code does not recognise "ReportDataSource" so I have to Re-write the second line as:
ReportViewer1.LocalReport.DataSources.Add(New Microsoft.Reporting.WinForms.ReportDataSource("yourdatsource", yourtable))
2.  I can then use "ExamPlannerDataSet1" instead of "yourdatsource", and possibly the tablename "AvailExams", or possibly the binding source "Me.AvailExamsBindingSource" instead of your table
3.  When I do that, the originally loaded Report stays om the report viewer unchanged.
4.  So I have to add the lfollowing ine in front of the code:
 Me.ReportViewer1.Reset()
5.  When I do that, the Report viewer screen goes blank and nothing shows?
0
 
PolyviewAuthor Commented:
I added an "Imports Microsoft.Reporting.WinForms" to the start of the code for the form so that it was not necessary to add it to your second line of code, but it made no difference.
0
 
PolyviewAuthor Commented:
I tried using the line "Me.ReportViewer1.LocalReport.Dispose()" in lieu of "Me.ReportViewer1.Reset".
When I do that, the originally loaded Report stays on the report viewer unchanged.
0
 
13598Commented:
To refresh the report with the changes you need the line:
  Me.ReportViewer1.LocalReport.Refresh()
0
 
13598Commented:
Don't use .dispose that is not what dispose is for.
Dispose is intended to release resources, in this case your report.
0
 
PolyviewAuthor Commented:
I did have Me.ReportViewer1.LocalReport.Refresh() at the end of the code, because you had it in your original code.  If I just use this, the originally loaded Report stays on the report viewer unchanged.
If I add the code Me.ReportViewer1.Reset() before your code, the report viewer screen goes blank and nothing shows.
0
 
PolyviewAuthor Commented:
I still can't seem to make it work with the Microsoft Report Viewer.
Today I tried switching to Crystal Reports with the Crystal Reports Viewer, and everything worked as it should.
So I guess I will use the Crystal Reports option.
However I would still be interested to know what I am doing wrong with the Microsoft Report Viewer.
Thank you
0
 
13598Commented:
Let's try this if you want to try with MS:
Add this on top of your project:
Imports Microsoft.Reporting.WinForms

then try this:
 ReportViewer1.Reset()
                ReportViewer1.ProcessingMode = ProcessingMode.Local
                ReportViewer1.LocalReport.DataSources.Clear()
ReportViewer1.LocalReport.DataSources.Add(New ReportDataSource("nameofyourdataset_nameofyourtablel", "nameofyourdataset_nameofyourbindingsource"))
                                      ReportViewer1.LocalReport.Refresh()

Please pay closely attention to this line:
ReportViewer1.LocalReport.DataSources.Add(New ReportDataSource("nameofyourdataset_nameofyourtablel", "nameofyourdataset_nameofyourbindingsource"))
Notice: "nameofyourdataset_nameofyourtablel" NOT ("nameofyourdataset.nameofyourtablel",
It is vital that you use the underscore and not a dot in this particular construct.
Try it and see if it works.
0
 
PolyviewAuthor Commented:
Sorry! I get exactly the same result - the screen goes blank.  I tried your code both with and without an extra line:
ReportViewer1.LocalReport.ReportEmbeddedResource = "ExamPlannerV2.TestforPrices.rdlc"
and I paid close attention the the underscore, (not a dot)
I am more convinced than ever to abandon the MS viewer and use the Crystal Reports option.
Thank you very much for your efforts.
0
 
13598Commented:
@Polyview can you post your final working code so others can benefit from it?
0
 
PolyviewAuthor Commented:
 Private Sub tab5OptExamTimeAllocation_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles tab5OptExamTimeAllocation.Click
        Try
            Me.ReportViewer1.Reset()
            Me.ReportViewer1.LocalReport.ReportPath = Application.StartupPath & "\TestReportForTimes.rdlc"
            Dim rptDataSource As New ReportDataSource _
            ("ExamPlannerDataSet1_AvailExams", Me.ExamPlannerDataSet1.Tables("AvailExams"))
            Me.ReportViewer1.LocalReport.DataSources.Add(rptDataSource)
            Me.ReportViewer1.RefreshReport()
        Catch ex As Exception
            MessageBox.Show(ex.Message, My.Application.Info.Title, _
                            MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
        'Notes
        'line 1. Me.ReportViewer1.LocalReport.DataSources.Clear() did not seem to work
        '        and I had to use Me.ReportViewer1.Reset() as the starting line of code
        'Line 2. This directed me to the debug bin folder where I needed a copy of the report.
        'Line 3. The "ExamPlannerDataSet1_AvailExams" was copied from the Data Sources Dialog
        '        Box in the report design file
        '        The Me.ExamPlannerDataSet1.Tables("AvailExams") was taken from the
        '        Microsoft Reporting Services article, and it was this coding
        '        that seemed to be one of my main problems.

    End Sub
0
 
PolyviewAuthor Commented:
I forgot to say that I also had an  "Imports Microsoft.Reporting.WinForms" at the start of the form.
0
All Courses

From novice to tech pro — start learning today.