2011-09-13 70 views
5

我已經使用水晶報告創建了報告。我正在使用visual studio 2010.當我嘗試轉到其他頁面時出現問題。當我嘗試導航到第2頁或最後一頁時,屏幕上顯示錯誤「無有效報告源」可用。有誰知道我需要做什麼?感謝您使用這個線程解決方案的時間沒有有效的報告源可用 - 水晶報告

回答

2

保存您在會議報告,然後在頁面後給報表源從會議回來

protected void Page_Load(object sender, EventArgs e) 
{ 
     if (IsPostBack) 
     { 
      try 
      { 
       CrystalReportViewer1.ReportSource = (ReportDocument)Session["Report"]; 
       CrystalReportViewer1.RefreshReport(); 
       CrystalReportViewer1.DataBind(); 
      } 
      catch (Exception ex) 
      { 

       // throw; 
      } 
     } 

    } 
    protected void CrystalReportViewer1_PreRender(object sender, EventArgs e) 
    { 

    } 
    protected void btnPrint_Click(object sender, EventArgs e) 
    { 
     ReportDocument rptDoc = new ReportDocument(); 
     rptDoc.Load(Server.MapPath("Reports\\BalanceReportNew\\BalanceReport.rpt")); 
     rptDoc.SetDataSource(ReportData()); 
     Session["Report"] = rptDoc; 
     CrystalReportViewer1.ReportSource = rptDoc; 
     CrystalReportViewer1.RefreshReport(); 
     CrystalReportViewer1.DataBind(); 
    } 
    public DataTable ReportData() 
    { 
     string ClassName = ddlClass.SelectedValue; 
     string Division = ddlDivison.SelectedValue; 
     string Subject = ddlSubjects.SelectedValue; 
     DataTable ReportData = objRpt.getReportData(ClassName, Division, Subject); 
     return ReportData; 
    } 
0

那麼它不是一個大問題,您只需要創建數據源的會話。然後在頁面加載時傳遞它。所有晶體報告功能將正常工作。

如果您需要任何進一步的幫助或代碼,請告訴我。

0

感謝@RăzvanPanda。
完整的代碼在這裏給出。

protected void Page_Load(object sender, EventArgs e){ 
    CrystalDecisions.CrystalReports.Engine.ReportDocument report = 
          new CrystalDecisions.CrystalReports.Engine.ReportDocument(); 

         TableLogOnInfos crtableLogoninfos = new TableLogOnInfos(); 
         TableLogOnInfo crtableLogoninfo = new TableLogOnInfo(); 

         ConnectionInfo crConnectionInfo = new ConnectionInfo(); 
         Tables CrTables; 
         report.Load(Server.MapPath("~/Reports/Monthly/CrMonthly.rpt")); 

         crConnectionInfo.ServerName = ConfigurationManager.AppSettings["Server4Crystal"].ToString();//[SQL SERVER NAME] 
         crConnectionInfo.DatabaseName = ConfigurationManager.AppSettings["Database4Crystal"].ToString();//[DATABASE NAME] 

         crConnectionInfo.UserID = ConfigurationManager.AppSettings["User4Crystal"].ToString();//[DB USER NAME] 
         crConnectionInfo.Password = ConfigurationManager.AppSettings["Password4Crystal"].ToString(); //[DB PASSWORD] 

    //LOOP THROUGH EACH TABLE & PROVIDE LOGIN CREDENTIALS 
         CrTables = report.Database.Tables; 
         foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in CrTables) 
         { 
          crtableLogoninfo = CrTable.LogOnInfo; 
          crtableLogoninfo.ConnectionInfo = crConnectionInfo; 
          CrTable.ApplyLogOnInfo(crtableLogoninfo); 
         } 

    //PROVIDE PARAMETERS TO CRYSTAL REPORT, IF REQUIRED. 
         ParameterField paramField = new ParameterField(); 
         ParameterFields paramFields = new ParameterFields(); 
         ParameterDiscreteValue paramDiscreteValue = new ParameterDiscreteValue(); 

         paramField.Name = "PARAMETER_NAME"; 
         paramDiscreteValue.Value = "PARAMETER_VALUE"; 
         paramField.CurrentValues.Add(paramDiscreteValue); 
         paramFields.Add(paramField); 
    //ADD MORE PARAMETERS, IF REQUIRED.... 


         StoreMonthlyViewer.ParameterFieldInfo = paramFields; 
         StoreMonthlyViewer.ReportSource = report; 
    //FINALLY REFRESH REPORT 
         StoreMonthlyViewer.RefreshReport(); 
         StoreMonthlyViewer.ToolPanelView = CrystalDecisions.Web.ToolPanelViewType.None; 
} 
2

以下應該解決您的問題:

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (Page.IsPostBack) 
     { 
      //whatever you do when the page is loaded for the first time 
      //this could even be bindReport(); 
     } 
     else 
     { 
      bindReport(); 
     } 
    } 

public void bindReport() 
    { 
     ReportDocument rptDoc = new ReportDocument(); 
     dsSample ds = new dsSample(); // .xsd file name 
     DataTable dt = new DataTable(); 
     // Just set the name of data table 
     dt.TableName = "Crystal Report Example"; 
     dt = getMostDialledNumbers(); //This function populates the DataTable 
     ds.Tables[0].Merge(dt, true, MissingSchemaAction.Ignore); 
     // Your .rpt file path will be below 
     rptDoc.Load(Server.MapPath("mostDialledNumbers.rpt")); 
     //set dataset to the report viewer. 
     rptDoc.SetDataSource(ds); 
     CrystalReportViewer1.ReportSource = rptDoc; 
     CrystalReportViewer1.RefreshReport(); 
     //in case you have an UpdatePanel in your page, it needs to be updated 
     UpdatePanel1.Update(); 
    } 
0

作爲一種在其他的答案說。將ReportDocument存儲在會話中(或其他)並設置ReportSource。

例子:

protected void Page_Load(object sender, EventArgs e) 
{ 
      if (Session["ReportSource"] != null) 
      { 
      CrystalReportViewer1.ReportSource = (ReportDocument) Session["ReportSource"]; 
      } 
      else 
      { 
      ReportDocument reportDocument = new ReportDocument(); 
      reportDocument.Load("MyReport.rpt"); 
      CrystalReportViewer1.ReportSource = reportDocument; 
      Session["ReportSource"] = reportDocument; 
      } 
} 
0

嘗試尋找您的報告不包含

通常,當你插入代碼中的一些參數,您的報告不包含參數

1

它發生的一些參數#簡單解決方案

我剛剛解決了這個問題,使用CrystalReportViewer 導航事件

在查看報告按鈕,我在會話中保存的報告文件

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click 
    ' -- the ds is dataset variable containing data to be displayed in the report 

    rptDoc.SetDataSource(ds) 
    Session.Add("rptdoc", rptDoc) 
    CrystalReportViewer1.ReportSource = rptDoc 

    End Sub 

那麼的CrystalReportViewer的導航事件中,我的的CrystalReportViewer數據源設置爲會話

Protected Sub j(ByVal source As Object, ByVal e As CrystalDecisions.Web.NavigateEventArgs) Handles CrystalReportViewer1.Navigate 

    rpt.SetDataSource(ds) 
    CrystalReportViewer1.ReportSource = session("rptdoc") 

End Sub 

因此之前每次您導航到報表中的另一個頁面,將CrystalReportViewer數據源設置爲保存在會話中的報表文檔。