Using one report viewer for many reports

Problems refreshing the selected report

Page 1 of 1

3 Replies - 24844 Views - Last Post: 19 March 2011 - 05:51 AM Rate Topic: -----

#1 BenScar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 13-August 09

Using one report viewer for many reports

Posted 13 August 2009 - 02:23 PM

Hello there,

I've been reading this forum for a while now and found it to be very good at giving advice. I'm hoping someone can tell me where I'm going wrong!

I have a "MicrosoftReportViewer" on a form and a Combo box that lists the available reports (which will end up being dynamic in the end).
When the use selects an item from the list, the system loads up the correct report (populating any datasets needed) and then displaying it.

Thing is, I can't get the viewer to change the displayed report, it just refreshes the first loaded report.

As you can see from the code below, there are two reports set up. I can get either one to be displayed if you select it first, sadly you're then stuck with that one!

Currently they both use the same datasouce, so no need to mess about with that.

	Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
		Me.viw_ShowAllRepairsTableAdapter.Fill(Me.RepairsSystemDataSet.viw_ShowAllRepairs)

		Select Case CType(ComboBox1.SelectedItem, String).ToLower
			Case "rpttest"
				rptView.LocalReport.ReportPath = Path.Combine(Application.StartupPath, "rptTest.rdlc")
			Case "rptallrepairs"
				rptView.LocalReport.ReportPath = Path.Combine(Application.StartupPath, "rptAllRepairs.rdlc")
		End Select

		rptView.LocalReport.Refresh()
		rptView.RefreshReport()
	End Sub


I've tried using the "rptView.LocalReport.ReportEmbeddedResource" property and I get the same results.

Tried the following as well:
	Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
		Me.viw_ShowAllRepairsTableAdapter.Fill(Me.RepairsSystemDataSet.viw_ShowAllRepairs)

		Dim strFile As String = ""

		Select Case CType(ComboBox1.SelectedItem, String).ToLower
			Case "rpttest"
				strFile = Path.Combine(Application.StartupPath, "rptTest.rdlc")
			Case "rptallrepairs"
				strFile = Path.Combine(Application.StartupPath, "rptAllRepairs.rdlc")
		End Select

		If strFile<>"" then
		   Dim strIn As New StreamReader(strFile)

		   rptView.LocalReport.LoadReportDefinition(strIn)
		   rptView.LocalReport.Refresh()

		   rptView.RefreshReport()

		   strIn.Close()
		   strIn = Nothing
		End If
	End Sub


This just loads up the report that the viewer has set as the default, reguardless of the report selected!

What am I missing? There should be a way to change the report being viewed and I don't really want to be having multiple viewers just to house multiple reports :(

Thanks in advance.
Ben
-----

Is This A Good Question/Topic? 0
  • +

#3 motcom  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Re: Using one report viewer for many reports

Posted 19 August 2009 - 04:35 AM

Hi BenScar,

First of all i use report viewer 2008 but its the same for 2005

You might need to import microsoft.reporting

Imports Microsoft.Reporting


If it is a local report and the report is in your project, you need to use

ReportViewer1.LocalReport.ReportEmbeddedResource


The report is in your your project you can call it like this:

ReportViewer1.LocalReport.ReportEmbeddedResource = "YourProjectRootNamespace.rptAllRepairs.rdlc" 


BIG NOTE: The RootNamespace and report name must be case sensitive this applies to the datasources names aswell.

This is how i do it:

First Reset the ReportViewer with

ReportViewer1.Reset()


Then Dim the required datasources. For each table in your report you need a seperate ReportDatasource
I have 3 in my example

Dim ReportDataSource1 As Microsoft.Reporting.WinForms.ReportDataSource = New Microsoft.Reporting.WinForms.ReportDataSource
		  Dim ReportDataSource2 As Microsoft.Reporting.WinForms.ReportDataSource = New Microsoft.Reporting.WinForms.ReportDataSource
		  Dim ReportDataSource3 As Microsoft.Reporting.WinForms.ReportDataSource = New Microsoft.Reporting.WinForms.ReportDataSource


Now you need to give your datasources names and set the specific datatables.

NOTE: 1. In the "reportdatasource.name" you need to use a "_" instead of a "." (its also case sensitive, I always check in my report designer under report -> datasources. Click on a Report Datasource and at the bottom of the window it will show you the name.)
2. The "reportdatasource.value" is your dataset & table (use a ".")
			ReportDataSource1.Name = "YourDatasetName_YourDatasetTableName"
			ReportDataSource1.Value = Me.YourDatasetName.YourDatasetTableName
			ReportDataSource2.Name = "YourDatasetName_YourDatasetTableName"
			ReportDataSource2.Value = Me.YourDatasetName.YourDatasetTableName
			ReportDataSource3.Name = "YourDatasetName_YourDatasetTableName"
			ReportDataSource3.Value = Me.YourDatasetName.YourDatasetTableName



Now you need to clear the datasources in the report and add the new ones
			Me.ReportViewer1.LocalReport.DataSources.Clear()
			Me.ReportViewer1.LocalReport.DataSources.Add(ReportDataSource1)
			Me.ReportViewer1.LocalReport.DataSources.Add(ReportDataSource2)
			Me.ReportViewer1.LocalReport.DataSources.Add(ReportDataSource3)



Finnaly you call the "ReportEmbeddedResource" with your new report and refresh the report viewer (note-> case sensitive)
			ReportViewer1.LocalReport.ReportEmbeddedResource = "YourProjectRootNamespace.rptAllRepairs.rdlc"
			ReportViewer1.RefreshReport()



Hope this helps...

(good luck with drilldown as this is a little bit different)
Was This Post Helpful? 1

#4 BenScar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 13-August 09

Re: Using one report viewer for many reports

Posted 19 August 2009 - 11:01 AM

Thanks Motcom, that sorted it right out :)

Added this in before trying to select the report and it's all working.
		rptView.Reset()

		Dim repDef As Microsoft.Reporting.WinForms.ReportDataSource = New Microsoft.Reporting.WinForms.ReportDataSource
		repDef.Name = "RepairsSystemDataSet_viw_ShowAllRepairs"
		repDef.Value = Me.RepairsSystemDataSet.viw_ShowAllRepairs


Thanks for taking the time to post.

Ben
-----
Was This Post Helpful? 0
  • +
  • -

#5 srahmeh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 19-March 11

Re: Using one report viewer for many reports

Posted 19 March 2011 - 05:51 AM

BenScar

can i have the code how u solved this issue am still stucking into it.

thanks in advance
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1