Viewing SSRS Reports from a Windows Forms Application

Visual Studio .NET provides a report viewer control that allows for two ways to view a SQL Server Reporting Services (SSRS) report. One is a local source where the report is defined in a local rdlc file embedded in the application. The other method is to use a remote source and access the report from a remote SSRS server. This second method is what we are going to focus on in this article.

The simplest way to display an SSRS report on a windows form is to place a report_viewer control on the form and set the properties to display the report. The minimum properties that can be set through the graphical properties interface are:

report server url
the http path to the reportserver service on SSRS server for example: http:/reportservername/reportserver

report path
virtual path where the report is stored on the report server

report name
name of the report (note this is case sensitive)

Below is an example of how to make the report viewer more dynamic by setting the properties in vb code and using variables for the properties. We are assuming that a report_viewer control named ReportViewer1 has been placed on a windows form.

Public v_report_name As String = "My Report"
Public v_report_server as string = "http://ssrs_server/reportserver"
Public v_report_path as string = "/Reports/HR/"

'form load procedure
Private Sub report_viewer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'use the serverreport property of the report viewer to select a report from a remote SSRS server
Me.ReportViewer1.ServerReport.ReportServerUrl = New System.Uri(v_report_server)
Me.ReportViewer1.ServerReport.ReportPath = v_report_path & v_report_name

'select where the report should be generated with the report viewer control or on the report server using the SSRS service.
Me.ReportViewer1.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Remote

'refresh the display of the report viewer control

End Sub

There may be times that you want use the report viewer control to show the report, but instead show the report in a different format such as PDF or Excel. In these cases you can still use a report_viewer control, just define everything programmatically.

First we use a variable to create an instance of the report viewer control in memory.

'create report viewer control
Dim rptviewer As New Microsoft.Reporting.WinForms.ReportViewer

If the report requires parameters then the parameters need to be passed to the report before generating the output. To do this we create a report parameter list as a parameter array.

'create parameter array
Dim paramlist As New List(Of Microsoft.Reporting.WinForms.ReportParameter)

Next define required parameters for the report and add them to the list. Note that the parameter names are case sensitive.

'create a specific parameter required by the report
Dim param1 As New Microsoft.Reporting.WinForms.ReportParameter("eventid")

Since a parameter can be multi-select the parameter values is an array that allows you to add one or more values for the parameter.

'add values to the parameter here we use a variable that holds the parameter value

'add parameter to array

Next we pass the 3 required properties for the report viewer control and the parameters list.

'set the path to the report server
rptviewer.ServerReport.ReportServerUrl = New System.Uri(v_report_server)

'set the path to the report
rptviewer.ServerReport.ReportPath = v_report_path & v_report_name

'select where the report should be generated with the report viewer control or on the report server using the SSRS service.
rptviewer.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Remote

'add the parameterlist to the viewer

The last step is to export the report to a specific file and file format and then create a process to display the report.

'generate report as pdf file
'define variables to generate exported report
Dim v_mimetype As String = ""
Dim v_encoding As String = ""
Dim v_filename_extension As String = ""
Dim v_streamids() As String
Dim o_warnings() As Microsoft.Reporting.WinForms.Warning

'set new filename as report name plus current date and pdf extension
Dim v_filename As String = "c:\temp" & v_report_name & Today.Year.ToString & Today.Month.ToString & Today.Day.ToString & ".pdf"

'create byte array to hold generated report
Dim content() As Byte

'use the render command to create a byte array of the specified export file type
content = rptviewer.ServerReport.Render("PDF", Nothing, v_mimetype, v_encoding, v_filename_extension, v_streamids, o_warnings)

'use filestream to save byte array to a file
Dim o_fs As New IO.FileStream(v_filename, IO.FileMode.Create)
o_fs.Write(content, 0, content.Length)
o_fs = Nothing

'run pdf file
'create a windows process to run the application for a specific filename
Dim v_process As Process = Process.Start(v_filename)

'wait for process to complete or close before allowing application to continue

'remove temp file

With either of the methods listed above the SSRS server uses Windows authentication to verify whether the user has permission to run the report. By default the current Windows logon authentication credentials (username and password) are passed to the report server to check the permissions.

If you need to run with different permissions then you will need to set the following property of the report viewer control to define the user trying to run the report.


Note that in order to export a report to a specific file format that required parameters, knowledge of the how the report is defined is required to pass the appropriate parameters to the report.

Using the above code as an example you should be able to create functions to make a report viewer that is dynamic enough for your applications needs.

, , ,

Comments are closed.