水晶报表链接SOA数据源实例

时间:2021-10-23 14:51:50

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
using AusLIMS.Framework.Common.Entities;
using AusLIMS.SOAClient;
using AusLIMS.EntitiesModel;
using System.Web.Mvc;

namespace AusLIMS.Web.Report
{
    public partial class WebFormReport : System.Web.UI.Page
    {
         protected void Page_Load(object sender, EventArgs e)
        {
           
            CrystalReportSource1.ReportDocument.Load(Server.MapPath("CrystalReport1.rpt"));
            //注意此处必需指明Dataset中的表的名称,否则会提示“您请求的报表需要更多信息.”
            CrystalReportSource1.ReportDocument.SetDataSource(packageDataSet);
           
            //{?}中的参数可以不用赋值,即使赋了值也不起作用。
            // CrystalReportSource1.ReportDocument.ParameterFields["Parm"].CurrentValues.AddValue("1234567");
            //CrystalReportSource1.ReportDocument.ParameterFields["title"].CurrentValues.AddValue("这时推模式的报表样例!");
            CrystalReportSource1.DataBind();
            CrystalReportViewer1.ReportSource = CrystalReportSource1;
            CrystalReportViewer1.DataBind();
            //引用参数函数
            SetParameterFields();
        }
            PackageDataSet packageDataSet = new PackageDataSet();
        public void ShowReport()
        {
            try
            {
                UserSecurity userSecurity = new UserSecurity { UserGUID = "B0A011F3-8F4B-4E88-B528-FFF45A1E4F3D", SessionToken = "debugging" };

                IList<Tb_Package> packages = SOARequestor.LoadData<IList<Tb_Package>>(SOAHelper.FormatLimsSOAUrl("packages"), userSecurity);
                ;
                //DataFilll(ds, "Package");

                foreach (var item in packages)
                {
                    PackageDataSet.PackageRow row = packageDataSet.Package.NewPackageRow();
                    row.PackageGUID = item.PackageGUID;
                    row.PackageNo = item.PackageNo;
                    row.PackageContentType = item.PackageContentType;
                    row.PackagedOn = item.PackagedOn;
                    row.PackageType = item.PackageType;
                    row.SequenceNo = item.SequenceNo;
                    row.StatusGUID = item.StatusGUID;
                    row.SubmitterGUID = item.SubmitterGUID;
                    row.ReceiptConditionCode = item.ReceiptConditionCode;
                    row.CreatedBy = item.CreatedBy;
                    row.CreatedOn = item.CreatedOn;
                    row.IsDeleted = item.IsDeleted;
                    row.HazardCode = item.HazardCode;
                    packageDataSet.Package.AddPackageRow(row);
                }
            }
           
            catch (Exception ex)
            {
                //ControllerExceptionManager.HandleException(ex, ServerConstants.ExceptionPolicyForGeneralUI);
                throw;
            }
                      
        }
        //执行初始化,加载容器控件 ,完成页面初始化
        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
            DataBind();
            SetParameterFields();
        }
       
        //窗体构造函数调用的窗体初始化函数,主要由Visual Studio .Net自动生成。
        private void InitializeComponent()
        {
            this.Button1.Click += new System.EventHandler(this.Button1_Click);//为button1对象的Click事件添加响应方法button1_Click()
            this.Load += new System.EventHandler(this.Page_Load);
        }

        //设置参数
        private void SetParameterFields()
        {
            ParameterFields paramFields = new ParameterFields();
            ParameterField paramField1 = new ParameterField();
            paramField1.ParameterFieldName = "PackageNo";
            ParameterDiscreteValue paramDis1 = new ParameterDiscreteValue();
            paramDis1.Value = TextBox1.Text;
            paramField1.CurrentValues.Add(paramDis1);
            paramFields.Add(paramField1);

            ParameterField paramField2 = new ParameterField();
            paramField2.ParameterFieldName = "PackageGUID";
            ParameterDiscreteValue paramDis2 = new ParameterDiscreteValue();
            paramDis2.Value = TextBox2.Text;
            paramField2.CurrentValues.Add(paramDis2);
            paramFields.Add(paramField2);

            CrystalReportViewer1.ParameterFieldInfo = paramFields;
        }

        protected void Button1_Click(object sender, System.EventArgs e)
        {
            //引用调用数据的函数
            ShowReport();
        }

        protected void TextBox2_TextChanged(object sender, EventArgs e)
        {

        }
   }
}