我一直在将 VB 应用程序移植到 C#。除此之外,所有水晶报告都有效。我已经调试,验证了参数名称,并按照我在这里找到的信息。无论我尝试了什么,都会收到错误“缺少参数值”。当我尝试ExportToStream(ExportFormatType.PortableDocFormat)
保存为 PDF 时发生错误。参数都是字符串。当我调试时,6 个未链接的参数中的每一个都会在rptParams.ContainsKey(def.name)
块内命中。有 7 个子报表只有链接的参数。
我在设置参数之前设置数据源。
我努力了:
ParameterFieldDefinitions parmFields = rpt.DataDefinition.ParameterFields;
ParameterValues pvals = new ParameterValues();
foreach (ParameterFieldDefinition def in parmFields)
{
if (!def.IsLinked() && rptParams.ContainsKey(def.Name))
{
ParameterDiscreteValue pval = new ParameterDiscreteValue();
pval.Value = rptParams[def.Name];
pvals.Add(pval);
def.ApplyCurrentValues(pvals);
}
}
这是基于原始的 VB 代码:
Dim paramFieldDefs As ParameterFieldDefinitions
Dim paramFieldDef As ParameterFieldDefinition
Dim paramVal As ParameterDiscreteValue
Dim paramVals As New ParameterValues
Dim colStepRepParams As Hashtable
colStepRepParams = cStep.ReportParams
' CREATE A NEW PARAMETERS COLLECTION
paramFieldDefs = rptReport.DataDefinition.ParameterFields()
For Each paramFieldDef In paramFieldDefs
' IF EXCLUDES 'PARAMETERS' WHICH LINK MAIN REPORT TO SUB REPORTS
If Not paramFieldDef.IsLinked Then
If colStepRepParams.ContainsKey(paramFieldDef.Name) Then
paramVal = New CrystalDecisions.Shared.ParameterDiscreteValue
paramVal.Value = colStepRepParams.Item(paramFieldDef.Name)
paramVals.Add(paramVal)
paramFieldDef.ApplyCurrentValues(paramVals)
End If
End If
Next paramFieldDef
我也试过:
foreach (ParameterFieldDefinition def in parmFields)
{
if (!def.IsLinked() && rptParams.ContainsKey(def.Name))
{
rpt.SetParameterValue(def.Name, rptParams[def.Name]);
}
}
这是事情的顺序:
using (SqlCommand cmd = new SqlCommand(job.sproc, con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 600; //10 min
foreach (KeyValuePair<string, string> p in sprParams)
{
cmd.Parameters.AddWithValue(p.Key, p.Value);
}
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
ReportDocument rpt = new ReportDocument();
rpt.Load(Path.Combine(RPT_LOCATION, job.repFileName));
rpt.Database.Tables[0].SetDataSource(ds.Tables[0]);
int i = 1;
foreach (string subReport in job.subReports)
{
using (ReportDocument srpt = rpt.OpenSubreport(subReport))
{
srpt.SetDataSource(ds.Tables[i++]);
}
}
ParameterFieldDefinitions parmFields = rpt.DataDefinition.ParameterFields;
ParameterValues pvals = new ParameterValues();
foreach (ParameterFieldDefinition def in parmFields)
{
if (!def.IsLinked() && rptParams.ContainsKey(def.Name))
{
ParameterDiscreteValue pval = new ParameterDiscreteValue();
pval.Value = rptParams[def.Name];
pvals.Add(pval);
def.ApplyCurrentValues(pvals);
}
}
}