2015-08-18 153 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();