水晶报表参数编程示例代码

以下实现从代码中传值给报表上的参数字段,此处假设已有CustomerOrders.rpt文件,其上放置有一个Name为CustomerID的参数字段!

    Imports CrystalDecisions.Shared
    Imports CrystalDecisions.CrystalReports.Engine

    Private Const RPT_NAME As String = "CustomerOrders.rpt"
    Private Const PARAMETER_FIELD_NAME As String = "CustomerID"

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '在此处放置初始化页的用户代码

        ' 设置报表源
        report = New ReportDocument()
        report.Load(GetReportPath(RPT_NAME))

        ' 设置参数
        crvOrders.ParameterFieldInfo = GetParameterInfo(CType(Session("list"), ListItemCollection))

        crvOrders.ReportSource = report

        ' 显示报表
        crvOrders.DataBind()

    End Sub
   
    '----------------------------------------------------------------
    ' Function GetParameterInfo:
    '   从复选框列表获取参数字段信息
    ' Returns:
    '   参数字段
    ' Parameters:
    '   [in]  list: 复选框列表
    ' Throws:
    '  
    ' PreConditions:
    '  
    '----------------------------------------------------------------
    Private Function GetParameterInfo(ByVal list As ListItemCollection) As ParameterFields

        Dim item As ListItem
        Dim isEmpty As Boolean = True
        Dim itemCount As Integer = 0

        ' 声明将参数传递给查看器控件所需的变量。
        Dim paramFields As New ParameterFields()
        Dim paramField As New ParameterField()
        Dim discreteVal As New ParameterDiscreteValue()

        ' 参数是具有多个值的离散参数。

        ' 设置参数字段的名称,它必须和报表中的参数相符。
        paramField.ParameterFieldName = PARAMETER_FIELD_NAME

        For Each item In list
            If item.Selected Then
                isEmpty = False
                itemCount += 1

                ' 设置一个离散值并将其传递给该参数
                discreteVal.Value = item.Value
                paramField.CurrentValues.Add(discreteVal)
                ' 新建一个离散值
                discreteVal = New ParameterDiscreteValue()
            End If
        Next

        If isEmpty Then
            For Each item In list
                ' 设置一个离散值并将其传递给该参数
                discreteVal.Value = item.Value
                paramField.CurrentValues.Add(discreteVal)
                ' 新建一个离散值
                discreteVal = New ParameterDiscreteValue()
            Next
        End If

        ' 将该参数添加到参数字段集合。
        paramFields.Add(paramField)
        GetParameterInfo = paramFields

    End Function

展开阅读全文

水晶报表参数丢失问题

12-21

using System;rnusing System.Collections;rnusing System.ComponentModel;rnusing System.Data;rnusing System.Drawing;rnusing System.Web;rnusing System.Web.SessionState;rnusing System.Web.UI;rnusing System.Web.UI.WebControls;rnusing System.Web.UI.HtmlControls;rnusing System.Data.SqlClient;rnusing CrystalDecisions.CrystalReports.Engine;rnusing CrystalDecisions.Shared;rnrnrnnamespace GMLCReportrnrn /// rn /// WebForm4 的摘要说明。rn /// rn public class WebForm4 : System.Web.UI.Pagern rn protected System.Web.UI.WebControls.TextBox TextBox1;rn protected System.Web.UI.WebControls.Button Button1;rn protected System.Web.UI.WebControls.Label Label1;rn protected CrystalDecisions.Web.CrystalReportViewer CrystalReportViewer1;rn rn private void Page_Load(object sender, System.EventArgs e)rn rn // 在此处放置用户代码以初始化页面rn rn string strTime = "2006-1-2";rn DateTime inputTime = Convert.ToDateTime(strTime);rnrn // 创建数据库连接rn string conStr ="server=localhost;uid=sa;pwd=sa;database=GMLC;Trusted_Connection=no";rn SqlConnection myConnection = new SqlConnection(conStr);rnrn //CrystalReport4 cr4 = new CrystalReport4();rnrn SqlCommand com = null;rn Dataset4 dataSet4 = new Dataset4();rn tryrn rn // 打开数据库连接rn myConnection.Open();rnrn // 创建查询语句rn com = new SqlCommand("SELECT * FROM LCS_Client信息表", myConnection);rn SqlDataAdapter adapter = new SqlDataAdapter(com);rnrn adapter.Fill(dataSet4, "LCS_Client信息表");rnrn rn catch (SqlException oe)rn rn Label1.Text = oe.Message;rn rn finallyrn rn // 关闭数据库连接rn if (myConnection != null)rn rn myConnection.Close();rn rn rn rn ParameterFields paramFields = new ParameterFields();rn ParameterField paramField = new ParameterField();rn ParameterDiscreteValue pdValue = new ParameterDiscreteValue();rn rn paramField.ParameterFieldName = "inputTime";rn pdValue.Value = inputTime;rn paramField.CurrentValues.Add(pdValue);rn paramFields.Add(paramField);rnrn ReportDocument rd = new ReportDocument();rn rd.Load(Server.MapPath("CrystalReport4.rpt"));rn rd.SetDataSource(dataSet4.Tables["LCS_Client信息表"]);rn rn CrystalReportViewer1.ReportSource = rd;rn CrystalReportViewer1.ParameterFieldInfo = paramFields;rnrn CrystalReportViewer1.DataBind(); rn CrystalReportViewer1.Visible = true; rn rnrn #region Web 窗体设计器生成的代码rn override protected void OnInit(EventArgs e)rn rn //rn // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。rn //rn InitializeComponent();rn base.OnInit(e);rn rn rn /// rn /// 设计器支持所需的方法 - 不要使用代码编辑器修改rn /// 此方法的内容。rn /// rn private void InitializeComponent()rn rn this.Load += new System.EventHandler(this.Page_Load);rnrn rn #endregionrnrn private void Button1_Click(object sender, System.EventArgs e)rn rnrn rnrnrnrn rn private void BuildReportViewerParameters(CrystalDecisions.Web.CrystalReportViewer rptViewer, Hashtable ParamTable)rn rn //定义报表视图的参数的方法rn ParameterFields ParamFields = new ParameterFields();rn foreach (object key in ParamTable.Keys)rn rn ParameterField ParamField = new ParameterField();rn ParameterValues ParamValues = new ParameterValues();rn ParameterDiscreteValue ParamValue = new ParameterDiscreteValue();rn ParamValue.Value = ParamTable[key];rn ParamValues.Add(ParamValue);rn ParamField.ParameterFieldName = key.ToString();rn ParamField.CurrentValues = ParamValues;rn ParamFields.Add(ParamField);rn rn rptViewer.ParameterFieldInfo = ParamFields;rn rnrn private void BuildReportClassParameters(CrystalDecisions.CrystalReports.Engine.ReportClass rpt, Hashtable ParamTable)rn rn //定义报表类的参数的方法rn foreach (object key in ParamTable.Keys)rn rn ParameterValues ParamValues = new ParameterValues();rn ParameterDiscreteValue ParamValue = new ParameterDiscreteValue();rn ParamValue.Value = ParamTable[key];rn ParamValues.Add(ParamValue);rn rpt.DataDefinition.ParameterFields[key.ToString()].ApplyCurrentValues(ParamValues);rn rn rn rn protected void BuildReportParameters(CrystalDecisions.Web.CrystalReportViewer rpt,Hashtable ParamTable)rn rn //定义报表参数的方法rn ParameterFields ParamFields = new ParameterFields();rnrn foreach(object key in ParamTable.Keys)rn rn ParameterField ParamField;rn ParameterValues ParamValues;rn ParameterDiscreteValue ParamValue;rnrn ParamField = new ParameterField();rn ParamValues = new ParameterValues();rn ParamValue = new ParameterDiscreteValue();rnrn ParamValue.Value = ParamTable[key]; rn ParamValues.Add(ParamValue);rn ParamField.ParameterFieldName=(string)key;rn ParamField.CurrentValues = ParamValues;rn ParamFields.Add(ParamField);rn rn rpt.ParameterFieldInfo = ParamFields;rn rn rn写了一段代码老说参数丢失异常详细信息: CrystalDecisions.CrystalReports.Engine.ParameterFieldCurrentValueException: 丢失参数字段当前值。rn谁帮忙给我看看啊 论坛

关于水晶报表参数默认值!!!

12-08

...rnusing CrystalDecisions.Shared;rnusing CrystalDecisions.CrystalReports.Engine;rnrnnamespace ty.documentrnrn public class ty_print : System.Web.UI.Pagern rn protected CrystalDecisions.Web.CrystalReportViewer crv1;rn public static string strConnection=ConfigurationSettings.AppSettings["db"];rn private void Page_Load(object sender, System.EventArgs e)rn rn if(!Page.IsPostBack)rn rn TableLogOnInfo logOnInfo = new TableLogOnInfo ();rn ReportDocument oRpt = new ReportDocument();rn string path;rn path=Server.MapPath ("test2.rpt");rn oRpt.Load (path);rn string a,b,c,d;rn a=System.Configuration.ConfigurationSettings.AppSettings ["servername"];rn b=System.Configuration.ConfigurationSettings.AppSettings ["database"];rn c=System.Configuration.ConfigurationSettings.AppSettings ["userid"];rn d=System.Configuration.ConfigurationSettings.AppSettings ["pass"];rn logOnInfo.ConnectionInfo.ServerName = a;rn logOnInfo.ConnectionInfo.DatabaseName = b;rn logOnInfo.ConnectionInfo.UserID = c;rn logOnInfo.ConnectionInfo.Password = d;rn oRpt.Database.Tables [0].ApplyLogOnInfo (logOnInfo);rn crv1.ReportSource = oRpt;rnrn ParameterFields paramFields = crv1.ParameterFieldInfo;rn ParameterField paramField = paramFields["sx1"];rn ParameterValues curValues = paramField.CurrentValues;rn ParameterDiscreteValue discreteValue = new ParameterDiscreteValue();rn discreteValue.Value = "项目部";rn curValues.Add(discreteValue);rnrn ParameterField paramField2 = paramFields["sx2"];rn ParameterValues curValues2 = paramField2.CurrentValues;rn ParameterDiscreteValue discreteValue2 = new ParameterDiscreteValue();rn discreteValue2.Value = "同标"; rn curValues2.Add(discreteValue2);rnrn crv1.ParameterFieldInfo = paramFields;rn crv1.DisplayGroupTree = false;rn crv1.DisplayToolbar = false;rn DataBind();rn rn rn rnrnrn我的水晶报表是从sql_2k中视图中取的数据,在水晶报表中设置了二个参数sx1,sx2,rn默认取数据是select * from ttt order by id,我现在想根据用户输入实现动态查询,select * from ttt where sxx=sx1 and sxxx=sx2 order by id,但假如sx1为“全部”那么就不包括这个条件,sx2也是一样!但根据上面的程序就成了select * from ttt where sxx=全部 and sxxx=全部,不符合我的要求.假如当全部时不传数值出现:rn丢失参数字段当前值。 rn说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 rn异常详细信息: CrystalDecisions.CrystalReports.Engine.ParameterFieldCurrentValueException: 丢失参数字段当前值。rnrnrn请大家帮帮我!谢谢!rn 论坛

没有更多推荐了,返回首页