没有有效的报告来源 - 水晶报告

时间:2011-09-13 08:01:50

标签: asp.net visual-studio-2010 crystal-reports

我使用水晶报告创建了一份报告。我正在使用visual studio 2010.当我尝试转到另一个页面时会出现问题。当我尝试导航到第2页或最后一页时,错误屏幕上显示无可用的有效报告源。有谁知道我需要做什么?谢谢你的时间

8 个答案:

答案 0 :(得分: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;
    }

答案 1 :(得分: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();
    }

答案 2 :(得分:1)

尝试使用此主题中的解决方案:

No Valid report source is available

从它的内容来看,你应该能够通过在代码中提供ConnectionInfo和ReportSource来使它工作。

答案 3 :(得分:1)

#Simple Solution

我刚刚使用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的Navigate事件中,我将CrystalReportViewer数据源设置为Session

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数据源都将设置为会话中保存的报表文档。

答案 4 :(得分:0)

这不是一个大问题,你只需要创建数据源的会话。然后在页面加载时传递它。所有水晶报告功能都能正常工作。

如果您需要任何进一步的帮助或代码,请与我们联系。

答案 5 :(得分: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;
}

答案 6 :(得分: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;
          }
}

答案 7 :(得分:0)

尝试查找报告中不包含的参数

通常在代码中插入一些参数并且报表中不包含该参数时会发生