c#将参数传递给crystal报告错误“缺少参数值”

时间:2013-09-25 16:13:40

标签: c# crystal-reports

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

skalka:s解决方案适用于我,但我没有任何SubReports

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