C# 将参数传递给 Crystal 报告错误“缺少参数值”
本文关键字:参数 缺少参数值 错误 参数传递 Crystal 报告 | 更新日期: 2023-09-27 18:32:50
我一直在将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);
}
}
}
这是我
的部分基于您的解决方案...就我而言,经过 8 小时的战斗,它终于起作用了......!
///BEFORE setting DB connection
ParameterFieldDefinitions parmFields = cr.DataDefinition.ParameterFields;
foreach (ParameterFieldDefinition def in parmFields)
{
if (!def.IsLinked() )
{
switch (def.ValueType)
{
case CrystalDecisions.Shared.FieldValueType.StringField:
cr.SetParameterValue(def.Name, "", def.ReportName);
break;
case CrystalDecisions.Shared.FieldValueType.NumberField:
cr.SetParameterValue(def.Name, 0, def.ReportName);
break;
default:
cr.SetParameterValue(def.Name, null, def.ReportName);
break;
}
}
}
///Now set DB connections
...
///Now set all parameters which INDEED HAVE to be set ...
...
迎接SK
skalka:s 解决方案对我有用,但我没有任何子报告
foreach (ParameterFieldDefinition def in doc.DataDefinition.ParameterFields)
{
if (!def.IsLinked())
{
switch (def.ValueType)
{
case CrystalDecisions.Shared.FieldValueType.StringField:
doc.SetParameterValue(def.Name, "");
break;
case CrystalDecisions.Shared.FieldValueType.NumberField:
doc.SetParameterValue(def.Name, 0);
break;
default:
doc.SetParameterValue(def.Name, null);
break;
}
}
}
doc.SetDataSource(table);
// Set parameters as I wanted from the beginning