Sunday, October 18, 2009

Passing Parameters to A Crystal Report from Code Behind ~ ASP.NET

In a resent project i had to pass parameters to a crystal report depending on a user selection of a dropdownlist, this is how i achieved it in VB.NET.

The server name and DBname are stored in the web.config

Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
ReportLocation = System.IO.Path.Combine(Server.MapPath("~"), "Admin\Report1.rpt")
ServerName = ConfigurationManager.AppSettings("ServerName")
DBName = ConfigurationManager.AppSettings("DBName")
End Sub

Sub SetSource(ByVal params As String)
Dim cryRpt As New ReportDocument()
Dim crtableLogoninfos As New TableLogOnInfos()
Dim crtableLogoninfo As New TableLogOnInfo()
Dim crConnectionInfo As New ConnectionInfo()
Dim CrTables As Tables
crConnectionInfo.ServerName = ServerName
crConnectionInfo.DatabaseName = DBName
crConnectionInfo.IntegratedSecurity = True
CrTables = cryRpt.Database.Tables
For Each CrTable As CrystalDecisions.CrystalReports.Engine.Table In CrTables
crtableLogoninfo = CrTable.LogOnInfo
crtableLogoninfo.ConnectionInfo = crConnectionInfo

Dim parameters As New ParameterFields
Dim param As ParameterField
Dim paramvalue As ParameterDiscreteValue
CrystalReportViewer1.ReportSource = cryRpt

param = New ParameterField
paramvalue = New ParameterDiscreteValue
param.Name = "Param1"
paramvalue.Value = params
param.HasCurrentValue = True
CrystalReportViewer1.ParameterFieldInfo = parameters
End Sub

No comments: