c#将参数传递给crystal报告错误“缺少参数值”
发布时间:2020-12-15 21:56:33 所属栏目:百科 来源:网络整理
导读:我一直在将一个VB应用移植到C#.除了这个之外,所有水晶报告都有效我已经调试,验证了参数名称,并按照我在这里找到的信息.无论我尝试过什么,我都会收到错误“缺少参数值”.当我尝试ExportToStream(ExportFormatType.PortableDocFormat)并另存为PDF时,会发生错误
我一直在将一个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,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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |