my_lou Posted June 18, 2004 Posted June 18, 2004 ok, i have the following situation: Crystal Report .NET untyped report with ReportDocument using strongly typed ADO.NET dataset for data access. I am opening the report in a Windows Form CrystalReportViewer. I want to pass parameters to it at runtime, but i can't figure out should i use the ParameterFieldInfo property of the CrystalReportViewer (in which case i'd have to use ParameterFields) or the DataDefintion.ParameterFields property of the ReportDocument class (in which case i'd have to use ParameterFieldDefinitions). To illustrate my point, here is some code. I use the following to pass parameters to Crystal Report using regular database, i.e. not DataSet, for data access and it works fine: Imports System Imports System.Collections Imports System.Collections.Specialized Imports CrystalDecisions.CrystalReports.Engine Imports CrystalDecisions.Shared Friend WithEvents Generic_CrystalReportViewer As CrystalDecisions.Windows.Forms.CrystalReportViewer Friend WithEvents GenericComponent_reportDocument As CrystalDecisions.CrystalReports.Engine.ReportDocument <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.Generic_CrystalReportViewer = New CrystalDecisions.Windows.Forms.CrystalReportViewer Me.GenericComponent_reportDocument = New CrystalDecisions.CrystalReports.Engine.ReportDocument .... End Sub Private Sub GenericCRViewer_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load _FileName = "myReport.rpt" Me.GenericComponent_reportDocument.Load(_FileName) Call Me.InitializeLogonCredentials() Call Me.SetParameters() Me.Generic_CrystalReportViewer.ParameterFieldInfo = crParamFields Me.Generic_CrystalReportViewer.ReportSource = Me.GenericComponent_reportDocument End Sub Private Sub SetParameters() 'CrystalReports report's parameter fields to the viewer control. Dim crParamField As New ParameterField Dim crDiscreteVal As New ParameterDiscreteValue crParamField.ParameterFieldName = CRREPORT_PARAMETER_SUFFIX & crParamName crDiscreteVal.Value = DirectCast(Me.crParameters(crParamName), Integer) 'Pass discrete value to parameter. crParamField.CurrentValues.Add(crDiscreteVal) 'Next add parameter to parameter fields collection. crParamFields.Add(crParamField) End Sub However, that same code does not work if my report uses DataSet for data access. I tried the following, without success: (this code added to the sub GenericCRViewer_Load) Dim reportData As New DataSet FillDataTable(True, "STUDENTTYPEID", reportData) Me.GenericComponent_reportDocument.SetDataSource(reportData) crPrmFieldDefinitions = Me.GenericComponent_reportDocument.DataDefinition.ParameterFields Call Me.SetParameters() For pfCount As Integer = 0 To crParamFields.Count - 1 crPrmFieldDefinitions(crParamFields(pfCount).ParameterFieldName).ApplyCurrentValues(crParamFields(pfCount).CurrentValues) Next 'End For In other words, i tried to populate the ParameterFieldDefintions collection with the members of the ParameterFields collection, with no luck. What's the difference between ParameterFields and ParameterFieldDefintions anyway and when should each one be used? Thanks all. Quote
my_lou Posted June 18, 2004 Author Posted June 18, 2004 oops, never mind that, it was a dumb mistake as are 99.99% of all coding errors. Since I am a nice person and I do not wish the anguish I went through upon anybody else, I'll clarify what the problem was. It was in the sub-routine SetParameters(). In it I had something like this: Private Sub SetParameters() Dim crDiscreteVal As New ParameterDiscreteValue Dim crParamField As New ParameterField ................ ................ For Each crParam As String In crReportParams Dim crParamName As String crParamName = ............. 'code to build name If crParamName <> "" Then 'Set the name of the parameter field. It must match a 'parameter field in the report. crParamField.ParameterFieldName = crParamName 'Set discrete value. crDiscreteVal.Value = ............ 'set discrete value 'Pass discrete value to parameter. crParamField.CurrentValues.Add(crDiscreteVal) 'Next add parameter to parameter fields collection. crParamFields.Add(crParamField) End If Next End Sub Do you see the problem with this code?...Well, here it is after the first time the ParameterField crParamField is inseretd in the collection crParamFields, it is never initialized so for the rest of the execution of the loop i just kep overwritting crParamField, hence inserting the same value in crParamFields over and over again. Dumb, I know! Anyway, here is how to make this work: Private Sub SetParameters() Dim crDiscreteVal As New ParameterDiscreteValue Dim crParamField As New ParameterField ................ ................ For Each crParam As String In crReportParams '*********************************************** ' NEW code here * '*********************************************** 'Initialize ParameterField and ParameterDiscreteValue variables to 'new instances of their respective classes so the values already 'inserted in the ParameterFields collection would not be overwritten. crDiscreteVal = New ParameterDiscreteValue crParamField = New ParameterField '*********************************************** ' NEW code here * '*********************************************** Dim crParamName As String crParamName = ............. 'code to build name If crParamName <> "" Then 'Set the name of the parameter field. It must match a 'parameter field in the report. crParamField.ParameterFieldName = crParamName 'Set discrete value. crDiscreteVal.Value = ............ 'set discrete value 'Pass discrete value to parameter. crParamField.CurrentValues.Add(crDiscreteVal) 'Next add parameter to parameter fields collection. crParamFields.Add(crParamField) End If Next End Sub So that took care of it. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.