2015-08-18 121 views
0

我无法从C#表单向我的报告传递参数时遇到问题。 我有3个存储过程在sql服务器数据库MoM,它需要@MoM_ID。 起初,当我设置我的报告时,我将这些程序添加到它中,并给它们赋值339(@MoM_ID = 339)。唯一的两个选择是添加值或将其设置为NULL以继续。将参数从C#传递到Crystal Reports

设置我的报告后,我将下面的代码添加到我的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()); 
} 

谢谢

回答

0

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

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();