2011-09-13 71 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数据源设置为保存在会话中的报表文档。