将参数从c#传递到Crystal Reports

本文关键字:Crystal Reports 参数 | 更新日期: 2023-09-27 18:08:31

我有麻烦从c#表单传递参数到我的报告。我在sql server数据库MoM中有3个存储过程,需要@MoM_ID。首先,当我设置我的报告时,我将这些过程添加到其中并给它们值339 (@MoM_ID = 339)。只有两个选项是添加值或将其设置为NULL以便继续。

设置我的报告后,我添加了以下代码到我的c#表单,这发生在一个按钮点击。代码应该将参数传递给存储过程,并以pdf文件的形式输出报告,报告中包含新值。然而,当我这样做时,我总是在报告中得到相同的默认值,其中@MoM_ID等于339。

代码如下:

ReportDocument reportDocument = new ReportDocument();
ParameterField paramField = new ParameterField();
ParameterFields paramFields = new ParameterFields();
ParameterDiscreteValue paramDiscreteValue = new ParameterDiscreteValue();
//Set instances for input parameter 1 -  @Dept
paramField.Name = "@MoM_ID(Action_Items)";
            //*Remember to reconstruct the paramDiscreteValue and paramField objects
paramDiscreteValue.Value = "337";
paramField.CurrentValues.Add(paramDiscreteValue);
//Add the paramField to paramFields
paramFields.Add(paramField);
crystalReportViewer1.ParameterFieldInfo = paramFields;
reportDocument.Load(@"C:'Users'nbousaba'Documents'Visual Studio 2013'Projects'WindowsFormsApplication4'WindowsFormsApplication4'Report1.rpt");
//Load the report by setting the report source
crystalReportViewer1.ReportSource = reportDocument;
crystalReportViewer1.Refresh();
//set the database loggon information. 
reportDocument.SetDatabaseLogon("//USERNAME", "//PW", "//IP", "MoM");
//Creating a PDF file from the Crystal Report
try
{
    ExportOptions CrExportOptions ;
    DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions();
    PdfRtfWordFormatOptions CrFormatTypeOptions = new PdfRtfWordFormatOptions();
    CrDiskFileDestinationOptions.DiskFileName = "c:''testing123.pdf";
    CrExportOptions = reportDocument.ExportOptions;
    {
        CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
        CrExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
        CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions;
        CrExportOptions.FormatOptions = CrFormatTypeOptions;
    }
    reportDocument.Export();
}
catch (Exception ex)
{
    MessageBox.Show(ex.ToString());
}

谢谢

将参数从c#传递到Crystal Reports

使用下面的代码解决了问题,不要忘记将子报表链接到要更新参数的主报表。

    ReportDocument cryRpt = new ReportDocument();
    TableLogOnInfos crtableLogoninfos = new TableLogOnInfos();
    TableLogOnInfo crtableLogoninfo = new TableLogOnInfo();
    ConnectionInfo crConnectionInfo = new ConnectionInfo();
    Tables CrTables;
    string path = "C:/Users/nbousaba/Documents/Visual Studio 2013/Projects/WindowsFormsApplication4/WindowsFormsApplication4/Report1.rpt";
    cryRpt.Load(path);
    cryRpt.SetParameterValue("@MoM_ID", x);
    crConnectionInfo.ServerName = ".";
    crConnectionInfo.DatabaseName = "MoM";
    crConnectionInfo.UserID = "sa";
    crConnectionInfo.Password = "123456";
    CrTables = cryRpt.Database.Tables;
    foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in CrTables)
    {
        crtableLogoninfo = CrTable.LogOnInfo;
        crtableLogoninfo.ConnectionInfo = crConnectionInfo;
        CrTable.ApplyLogOnInfo(crtableLogoninfo);
    }
    crystalReportViewer1.ReportSource = cryRpt;
    crystalReportViewer1.Refresh();

    //Creating a PDF file from Crystal Report
    try
    {
        ExportOptions CrExportOptions;
        DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions();
        PdfRtfWordFormatOptions CrFormatTypeOptions = new PdfRtfWordFormatOptions();
        CrDiskFileDestinationOptions.DiskFileName = "c:''MoM_Form"+x+".pdf";
        CrExportOptions = cryRpt.ExportOptions;
        {
            CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
            CrExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
            CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions;
            CrExportOptions.FormatOptions = CrFormatTypeOptions;
        }
        cryRpt.Export();