This post is about some of the problems I ran into when I wanted one instance of a ReportViewer control hosted on a web page to render more than one type of report dynamically where the decision about which report to host came from a user selection, a filter or possibly some other method available on the web form.
The code snippet given below is the minimum required to get this working and assumes a datasource is available from a prepopulated dataset…
<snip>…
Dim reportStream As System.IO.Stream = _
New System.IO.FileStream(Server.MapPath("ProjectSummary.rdlc"), _
IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
Try
With ReportViewer1
.Reset()
.LocalReport.LoadReportDefinition(reportStream)
'Set any parameters required
Dim params(0) As ReportParameter
params(0) = New ReportParameter("RunParams", paramValue.ToString, False) 'Non visible param
.LocalReport.SetParameters(params)
.LocalReport.DataSources.Clear()
.LocalReport.DataSources.Add(datasource)
.LocalReport.Refresh()
End With
Finally
reportStream.Close()
End Try
Some of the key points about the above code are as follows:-
- To facilitate multiple worker processes having access to the report definition file at the same time it is important to do two things. The first is to set the correct FileAccess and FileShare attributes when opening the report definition and the second thing is to close the stream as soon as it has been used, which in turn should release the file handles by calling Dispose() internally. Experiment and see but this code works well for me!
- The .Reset method call is required to reset the report viewer internal controls
Hope it helps you out of a snag!



Recent Comments