2013-04-01 110 views
4

我有一個本地.rdlc報告用於單擊按鈕顯示,但由於某種原因報告僅顯示在第二次按鈕單擊事件中。我不知道爲什麼報告沒有顯示在第一次點擊按鈕上......這是我在按鈕的點擊事件上調用的函數。SQL Reporting Services報告僅在第二次點擊時加載

private void ShowReport(string accountingCompanyId, string companyId, string approvalUnitId, DateTime startDate, DateTime finishDate, string supplierId, 
        string documentNumber, string documentType, string documentState, string costCenterId, string chargingKeyId, 
        string dim1Value, string dim1Description, string dim1Id, string dim2Value, string dim2Description, string dim2Id, 
        string dim3Value, string dim3Description, string dim3Id, bool showDetails) { 

     //this.ReportViewer1.Reset(); 

     //Set report mode for local processing. 
     this.ReportViewer1.ProcessingMode = ProcessingMode.Local; 

     ISettingsReader settingsReader = SettingsReaderFactory.Instance.CreateSettingsReader(); 
     this.ReportViewer1.LocalReport.ReportPath = settingsReader.GetSetting("ReportViewer", "FinancialReportPath" + (showDetails ? "" : "Small"), true, null); 

     ReportsBL reports = new ReportsBL(); 

     // Clear out any previous datasources. 
     this.ReportViewer1.LocalReport.DataSources.Clear(); 

     // Load the company dataSource. 
     DataTable company = reports.GetCompanyDataSet(accountingCompanyId).Tables[0]; 
     ReportDataSource dataSourceCompany = new ReportDataSource(company.TableName, company); 
     this.ReportViewer1.LocalReport.DataSources.Add(dataSourceCompany); 

     // Load the dataSource. 
     DataTable report = reports.GetReportFinanceiroSmallDataSet(companyId, startDate, finishDate, chargingKeyId, costCenterId, documentNumber, documentType, dim1Value, dim2Value, dim3Value, dim1Id, dim2Id, dim3Id, supplierId, approvalUnitId, documentState, accountingCompanyId).Tables[0]; 
     ReportDataSource dataSourceReport = new ReportDataSource(report.TableName, report); 
     this.ReportViewer1.LocalReport.DataSources.Add(dataSourceReport); 

     this.ReportViewer1.LocalReport.Refresh(); 

     this.pnlReport.Visible = true; 
    } 

奇怪的是,如果我取消註釋行this.ReportViewer.Reset();那麼無論我生成的點擊次數如何,報告都將永遠不會顯示出來......是否有人知道這是否正常?如何解決這個問題? 在此先感謝,

回答

2

經過大量的試驗和錯誤,我通過調用pageload事件的databind()方法來實現它。在頁面加載數據綁定(未設置數據源)後,後續的按鈕點擊按鈕開始按預期工作。

我被包含在其他人遇到此錯誤的情況下。 (很想知道爲什麼我需要在頁面加載數據綁定,雖然...)

更新2

我終於想通了這個問題...原來,.rdlc是從一箇舊2005年遷移.rdl報告,並且新的.rdlc包含舊的報告參數+ sql,它以某種方式搞亂了報告加載。之後我刪除未使用的報告parameteres + SQL一切都開始完美的工作......我已經更新了代碼波紋管,以反映我現在用在我的項目...

protected void Page_Load(object sender, System.EventArgs e) { 

} 

protected void btGenStats_Click(object sender, System.EventArgs e) { 

    ... 

    this.ShowReport(accountingCompanyId, companyId, approvalUnitId, startDate, finishDate, supplierId, documentNumber, documentType, documentState, 
        costCenterId, chargingKeyId, dim1, dim1Desc, dim1Id, dim2, dim2Desc, dim2Id, dim3, dim3Desc, dim3Id, showDetails); 
} 

private void ShowReport(string accountingCompanyId, string companyId, string approvalUnitId, DateTime startDate, DateTime finishDate, string supplierId, string documentNumber, string documentType, string documentState, string costCenterId, string chargingKeyId, string dim1Value, string dim1Description, string dim1Id, string dim2Value, string dim2Description, string dim2Id, string dim3Value, string dim3Description, string dim3Id, bool showDetails) { 

    this.ReportViewer1.Reset(); 

    //Set report mode for local processing. 
    this.ReportViewer1.ProcessingMode = ProcessingMode.Local; 

    ISettingsReader settingsReader = SettingsReaderFactory.Instance.CreateSettingsReader(); 
    string reportPath = settingsReader.GetSetting("ReportViewer", "FinancialReportPath"+(showDetails? "" : "Small"), true, null); 
    this.ReportViewer1.LocalReport.ReportPath = Server.MapPath(reportPath); 

    ReportsBL reports = new ReportsBL(); 

    // Clear out any previous datasources. 
    this.ReportViewer1.LocalReport.DataSources.Clear(); 

    // Load the company dataSource. 
    DataTable company = reports.GetCompanyDataSet(accountingCompanyId).Tables[0]; 
    ReportDataSource dataSourceCompany = new ReportDataSource(company.TableName, company); 
    this.ReportViewer1.LocalReport.DataSources.Add(dataSourceCompany); 

    if (showDetails) 
    { 
     // Load the dataSource. 
     DataTable report = reports.GetReportFinanceiroDataSet(companyId, approvalUnitId, startDate, finishDate, chargingKeyId, costCenterId, documentNumber, documentType, dim1Value, dim2Value, dim3Value, dim1Id, dim2Id, dim3Id, supplierId, documentState, accountingCompanyId).Tables[0]; 
     ReportDataSource dataSourceReport = new ReportDataSource(report.TableName, report); 
     this.ReportViewer1.LocalReport.DataSources.Add(dataSourceReport); 
    } 
    else 
    { 
     // Load the dataSource. 
     DataTable report = reports.GetReportFinanceiroSmallDataSet(companyId, approvalUnitId, startDate, finishDate, chargingKeyId, costCenterId, documentNumber, documentType, dim1Value, dim2Value, dim3Value, dim1Id, dim2Id, dim3Id, supplierId, documentState, accountingCompanyId).Tables[0]; 
     ReportDataSource dataSourceReport = new ReportDataSource(report.TableName, report); 
     this.ReportViewer1.LocalReport.DataSources.Add(dataSourceReport); 
    } 

    this.pnlReport.Visible = true; 
} 
3

我想問題可能是在呈現頁面後點擊事件。嘗試調用Page_Load事件中的方法。

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (IsCallback) 
    { 
     ShowReport(
      // params 
     ); 
    } 
} 

如果這有效,你知道它與執行順序有關。

1
protected void btnSubmit_Click(object sender, EventArgs e) 
     { 
      string pstrType; 
      pstrType = Request.QueryString["Type"];   
      LoadReport(); 
     } 

     public class CustomReportCredentials : Microsoft.Reporting.WebForms.IReportServerCredentials 
     { 
      // local variable for network credential. 
      private string _UserName; 
      private string _PassWord; 
      private string _DomainName; 
      public CustomReportCredentials(string UserName, string PassWord, string DomainName) 
      { 
       _UserName = UserName; 
       _PassWord = PassWord; 
       _DomainName = DomainName; 
      } 
      public System.Security.Principal.WindowsIdentity ImpersonationUser 
      { 
       get 
       { 
        return null; // not use ImpersonationUser 
       } 
      } 
      public System.Net.ICredentials NetworkCredentials 
      { 
       get 
       { 
        // use NetworkCredentials 
        return new NetworkCredential(_UserName, _PassWord, _DomainName); 
       } 
      } 
      public bool GetFormsCredentials(out Cookie authCookie, out string user, out string password, out string authority) 
      { 
       // not use FormsCredentials unless you have implements a custom autentication. 
       authCookie = null; 
       user = password = authority = null; 
       return false; 
      } 
     } 

     void LoadReport() 
     { 
      string strCompanyName = objSession.SelCompanyName; 
      string strHeading = ""; 
      string strBranchName = objSession.SelBranchName; 
      rptvwMain.ProcessingMode = ProcessingMode.Remote; 
      rptvwMain.ServerReport.ReportServerCredentials = new CustomReportCredentials(AppConfig.ReportServerUserName, AppConfig.ReportServerPassword, AppConfig.ReportServerDomain); 
      string strReportServerUrl = AppConfig.ReportServerUrl + AppConfig.ReportServerFolder; 
      rptvwMain.ServerReport.ReportServerUrl = new Uri(strReportServerUrl); 
      List<ReportParameter> parameters = new List<ReportParameter>(); 
    if (pstrType == "OB") 
      { 
       strHeading = "Ledger Opening Balance"; 
       rptvwMain.ServerReport.ReportPath = "/Account/OpeningBalance"; 
      } 


      parameters.Add(new ReportParameter("FyId", Convert.ToInt16(objSession.FyId).ToString())); 
      parameters.Add(new ReportParameter("AccountGroupId", cmbAccountGroup.SelectedValue)); 
      parameters.Add(new ReportParameter("LedgerId", cmbLedgerId.SelectedValue)); 
      parameters.Add(new ReportParameter("BranchId", Convert.ToInt64(objSession.BranchId).ToString())); 
      parameters.Add(new ReportParameter("StDate", Convert.ToDateTime(RadDtpFromDate.SelectedDate).ToString())); 
      parameters.Add(new ReportParameter("EnDate", Convert.ToDateTime(RadDtpToDate.SelectedDate).ToString())); 
      parameters.Add(new ReportParameter("CompanyName", strCompanyName.ToString())); 
      parameters.Add(new ReportParameter("BranchName", strBranchName.ToString())); 
      parameters.Add(new ReportParameter("Heading",strHeading.ToString())); 
      rptvwMain.ServerReport.SetParameters(parameters); 

      rptvwMain.ServerReport.SetDataSourceCredentials(new[] { new DataSourceCredentials() { Name =AppConfig.ReportServerDataSource , UserId = AppConfig.ReportServerDSUserName, Password = AppConfig.ReportServerDSPassword } }); 
      rptvwMain.ShowZoomControl = true; 
      rptvwMain.ServerReport.Refresh(); 
     } 
    } 

試試看這個代碼........ 可能這段代碼對你有幫助??????

2

我從來沒有到調用ReportViewer.DataBind();以下是我通常所做的:

IEnumerable<ReportClass> ds = DataTranslator.GetReportData(Int64.Parse(<someId>)); 
report.LocalReport.ReportPath = "<some_path_to_report.rdlc>"; 
report.LocalReport.DataSources.Add(new ReportDataSource("DataSet", ds)); 
report.Visible = true; 
report.LocalReport.Refresh(); 
+0

但是,您在pageload方法上執行此操作,或者您使用onclick方法執行此操作?你使用什麼版本的ReportViewer? –

+0

我通過回發控件調用該方法。我特意從DropDownList_SelectedIndexChanged事件中抽取這個。我正在使用ReportViewer.WebForms版本10.0.0.0。 – m4chine

相關問題