Vb.net 如何将值传递给两个不同的参数

Vb.net 如何将值传递给两个不同的参数,vb.net,parameters,crystal-reports,crystal-reports-xi,Vb.net,Parameters,Crystal Reports,Crystal Reports Xi,我正在开发一个Windows应用程序,并在其中使用Crystal Report 我能够将值传递给Crystal Report的一个参数。现在,我在Crystal Report中又添加了一个参数,但对下面的代码中必须进行的修改感到困惑 下面的代码用于将值传递给CR的一个参数 Private Sub btnLoadBatch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadBatch.C

我正在开发一个Windows应用程序,并在其中使用Crystal Report

我能够将值传递给Crystal Report的一个参数。现在,我在Crystal Report中又添加了一个参数,但对下面的代码中必须进行的修改感到困惑

下面的代码用于将值传递给CR的一个参数

Private Sub btnLoadBatch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadBatch.Click
    Try
        Dim cr As New crMonthwiseBatch
        Dim oBatches As New Batches

        Dim Month As Integer = dtFrom.Value.Date.Month
        Dim StartDateForBatch As Date = New DateTime(dtBatchStartFromMonth.Value.Year, dtBatchStartFromMonth.Value.Month, "1")
        Dim DaysinMonths As Integer = System.DateTime.DaysInMonth(dtBatchStartFromMonth.Value.Year, dtBatchStartFromMonth.Value.Month)
        Dim EndDateForBatch = StartDateForBatch.AddDays(DaysinMonths)

        oBatches.LoadByQuery("CreatedDate >= #" + StartDateForBatch + "# and CreatedDate <= #" + EndDateForBatch + "#")
        cr.SetDataSource(oBatches)

        Dim crParameterFieldDefinitions As ParameterFieldDefinitions
        Dim crParameterFieldDefinition As ParameterFieldDefinition
        Dim crParameterValues As New ParameterValues
        Dim crParameterDiscreteValue As New ParameterDiscreteValue

        crParameterDiscreteValue.Value = "Batch List of Month - " + MonthName(dtBatchStartFromMonth.Value.Month) + " " + dtBatchStartFromMonth.Value.Year.ToString
        crParameterFieldDefinitions = cr.DataDefinition.ParameterFields
        crParameterFieldDefinition = crParameterFieldDefinitions.Item("MonthName")
        crParameterValues = crParameterFieldDefinition.CurrentValues

        crParameterValues.Clear()
        crParameterValues.Add(crParameterDiscreteValue)
        crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)

        CrystalReportViewerMonthwiseBatch.ReportSource = cr
        CrystalReportViewerMonthwiseBatch.Refresh()
    Catch ex As Exception

    End Try
End Sub
Private Sub btnLoadBatch_Click(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理btnLoadBatch。单击
尝试
Dim cr作为新的crMonthwiseBatch
Dim oBatches作为新批次
整数形式的月数=dtFrom.Value.Date.Month
Dim StartDateForBatch As Date=新日期时间(dtBatchStartFromMonth.Value.Year,dtBatchStartFromMonth.Value.Month,“1”)
Dim DaysinMonths为整数=System.DateTime.DaysInMonth(dtBatchStartFromMonth.Value.Year,dtBatchStartFromMonth.Value.Month)
Dim EndDateForBatch=StartDateForBatch.ADDDYS(DaysinMonths)

LoadByQuery(“CreatedDate>=#“+StartDateForBatch+”#和CreatedDate您可以通过这种方式实现它(查找上述语句的注释以获取说明

方法1:

”。。。
'每个参数字段的对象集合
Dim crParameterFieldDefinitions作为ParameterFieldDefinitions
'表示参数字段
Dim crParameterFieldDefinition作为ParameterFieldDefinition
'每个参数字段的ParameterValue对象集合
Dim crParameterValues作为新的ParameterValues()
'用于检索和设置离散值参数
Dim crParameterDiscreteValue作为新的ParameterDiscreteValue()
'获取当前crystal报表中的参数集合
crParameterFieldDefinitions=cr.DataDefinition.ParameterFields
'添加第一个参数
'获取当前报表中的参数并指定一个值
crParameterFieldDefinition=crParameterFieldDefinition(“参数名称”)
crParameterDiscreteValue.Value=“参数1\u值”
crParameterValues=crParameterFieldDefinition.CurrentValues
'清除分配给当前参数的旧值/默认值,添加并应用新值
crParameterValues.Clear()文件
crParameterValues.Add(crParameterDiscreteValue)
crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)
'已完成添加第一个参数
'添加第二个和后续参数
'重置集合
crParameterDiscreteValue=新参数DiscreteValue()
crParameterValues=新的ParameterValues()
'获取参数、赋值、清除旧值、添加到集合并应用
crParameterFieldDefinition=crParameterFieldDefinition(“参数名称”)
crParameterDiscreteValue.Value=“参数2\u值”
crParameterValues=crParameterFieldDefinition.CurrentValues
crParameterValues.Clear()文件
crParameterValues.Add(crParameterDiscreteValue)
crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)
'已完成添加第二个参数
'...
'只需显示报告即可
方法2:
如果您有多个参数,也可以通过使用单独的过程来实现,例如

Private子SetParams(crRpt作为CrystalDecisions.CrystalReports.Engine.ReportDocument,strParamName作为字符串,strParamValue作为字符串)
对于crRpt.DataDefinition.ParameterFields中作为CrystalDecisions.CrystalReports.Engine.ParameterFieldDefinition的每个pfField
如果pfField.Name.ToString().ToLower()=strParamName.ToLower(),则
尝试
Dim crParameterValues作为新的CrystalDecisions.Shared.ParameterValues()
Dim crParameterDiscreteValue作为新的CrystalDecisions.Shared.ParameterDiscreteValue()
crParameterDiscreteValue.Value=strParamValue
crParameterValues=pfField.CurrentValues
crParameterValues.Clear()文件
crParameterValues.Add(crParameterDiscreteValue)
pfField.ApplyCurrentValues(crParameterValues)
特例
'在此处添加您的异常处理机制
MessageBox.Show(例如Message)
结束尝试
如果结束
下一个
端接头
可以为任何报表对象调用上述例程(在下面的示例中为cr),以向报表添加任意数量的参数

SetParams(cr,“@Param_1_Name”,“Param_1_Value”)
SetParams(cr,“@Param_2_Name”,“Param_2_Value”)
'...
SetParams(cr,“@Param\u n\u Name”,“Param\u n\u Value”)

刚刚注意到您在过程结束时调用了
CrystalReportViewerMonthwiseBack.Refresh()
。我怀疑它可能会重置您的参数值并删除您上面添加的新参数值。