4

我一直在将 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);
        }
    }
}
4

2 回答 2

1

这是我部分基于你的解决方案......在我的情况下,它在 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

于 2013-10-07T18:41:00.487 回答
0

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
于 2016-06-14T08:33:55.577 回答