Wednesday, December 2, 2009

using report parameters in SSRS 2005

In this post we'll use report parameters to center an image in a reportviewer control.

In the report project create the file Picture.rdl (SSRS 2008 don't support any reportviewer add in with reference to report parameters, this is a bug). To create a file rdl connected to firbird ODBC let see: Getting started with Express Reporting Services 2005
In the rdl file create two controls: image control and textbox control.
From the menu choose Report > Report Parameters command. Press Add to add a parameter.The value of a parameter can be used in expressions as Parameters!Foo.Value where Foo is the name of the parameter.
To center an image in the report we'll make use of the image control "padding" property, because it lets use expressions.
The "source" property have to be set: external
The "sizing" property have to be set: fitProportional
In order to select and center the image in the image control we'll make use of three parameters:
P (text type)
PadLeft (Integer type in points)
PadRight (Integer type in points)

As value of the left padding we have: =Parameters!PadLeft.value & "pt"
As value of right padding we have: =Parameters!PadRight.value & "pt"
As value of Value property we have: "file:///" & Parameters!P.Value

In order to write a caption we'll add to the report a large textbox control
In order to write a caption in the textbox control we'll make use of a parameter:
As value of Value property we have: =Parameters!Caption.Value
to show the report in Visual Basic we'll make use of a form with a reportviewer control in it.

In the Fprincipale form the code to show the image and caption in a reportviewer control is:
Imports FirebirdSql.Data.FirebirdClient
Imports Microsoft.Reporting.WinForms

Private Sub Report(ByVal filename As String)
ShowReport.ReportViewer1.LocalReport.EnableExternalImages = True
Dim strImage, strContesto As String, strPadleft, strPadRight As Integer

Dim params(3) As ReportParameter

strImage = Me.DataGridView1.CurrentRow.Cells("Percorso").Value
strContesto = Me.DataGridView1.CurrentRow.Cells("Contesto").Value

image1 = New Bitmap(strImage, True)
If (image1.Width / 72) * 2.54 > 17 Or (image1.Height / 72) * 2.54 > 12.25 Then

strPadleft = (17 / 2.54 - 12.25 / 2.54 * (image1.Width / image1.HorizontalResolution) / (image1.Height / image1.VerticalResolution)) * 72 / 2

in this case the left and right padding have to be set so that the new image.width is resized by the ratio
(12.25 / 2.54)/(image1.Height / image1.VerticalResolution).
strPadRight = strPadleft


strPadleft = (17 / 2.54 - (image1.Width / 96)) * 72 / 2

in this case the image is resized with reference to screen resolution 96 DPI that is set by Windows OS. See Computer monitor DPI standards.
When th DPI image resolution is lower than the computer monitor DPI standard, the image appears smaller with reference to its printer sizes. Then, since the image control fits proportionally an image with a given DPI resolution, we, to see the real image size in reportviewer, need to refer to computer monitor DPI standard. Then say, 72*96/72=96, that is the number that apperars in the strPadLeft expression.

strPadRight = strPadleft

End If

params(0) = New ReportParameter("P", strImage, False)

params(1) = New ReportParameter("Didascalia", strContesto, False)

params(2) = New ReportParameter("PadLeft", strPadleft, False)

params(3) = New ReportParameter("PadRight", strPadRight, False)
ShowReport.ReportViewer1.LocalReport.ReportPath = filename ShowReport.ReportViewer1.LocalReport.SetParameters(params)
Dim RepDS As New ReportDataSource

Dim dv As New System.Data.DataView

dv = DataSet1.Tables("Immobili").DefaultView

RepDS.Name = "DataSource1"
RepDS.Value = dv
ShowReport.ReportViewer1.LocalReport.DataSources.Add(RepDS) ShowReport.ReportViewer1.LocalReport.ReportPath = filename
End Sub

Private Sub btnReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReport.Click


Report(My.Application.Info.DirectoryPath & "\Picture.rdl")


MsgBox("Select a record")

Exit Sub
End Try


End Sub
The related rdl report ( can be dowlaoded here

No comments:

Post a Comment