水晶报表仅显示第一页

4

我正在使用ASP.NET、SQL和Crystal Report。我已经成功生成了单页报告。但是当报告大小超过一页时,Crystal Report只显示第一页的数据。当我点击下一页按钮时,它会显示“源为空或未找到源”的提示信息。

  Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim rptDoc As New ReportDocument
    Dim ds As New StudentDataSet
    Dim sqlCon As SqlConnection
    Dim dt As New DataTable
    dt.TableName = "Crystal Report Example"
    sqlCon = New SqlConnection(myCon)
    Dim da As New SqlDataAdapter("select * from tblStudent", sqlCon)
    da.Fill(dt)
    ds.Tables(0).Merge(dt)
    rptDoc.Load(Server.MapPath("~\Reports\StudentList.rpt"))
    rptDoc.SetDataSource(ds)
    CrystalReportViewer1.ReportSource = rptDoc

  End Sub

enter image description here


你看,对于大多数人来说,这种情况是不会发生的。而且你还没有发布任何与大多数人不同的细节,这意味着在你的问题中没有任何人可以用来回答它的信息。请发布源代码或其他细节,以便这里的人们有些可依靠的东西。 - Erwin Bolwidt
好的,确切的消息框是“没有有效的报告源可用”。我现在还不能发布图片。当我点击Crystal报表的下一页按钮时,它就会显示。 - Abdul
1个回答

1

加载报表的代码必须在每个 postback 上执行。


Page_Init 是放置此代码的正确位置(Page_Load 可能会导致一些错误)。

尝试进行此更改(抱歉,由于我使用 C#,可能存在 VB 错误):

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
 ButtonClicked = true
 ShowReport()

Protected void Page_Init(object sender, EventArgs e)
 ShowReport()

Protected Sub ShowReport() 
 Dim rptDoc As New ReportDocument
 Dim ds As New StudentDataSet
 Dim sqlCon As SqlConnection
 Dim dt As New DataTable
 dt.TableName = "Crystal Report Example"
 sqlCon = New SqlConnection(myCon)
 Dim da As New SqlDataAdapter("select * from tblStudent", sqlCon)
 da.Fill(dt)
 ds.Tables(0).Merge(dt)
 rptDoc.Load(Server.MapPath("~\Reports\StudentList.rpt"))
 rptDoc.SetDataSource(ds)
 CrystalReportViewer1.ReportSource = rptDoc

End Sub

嘿,建议在每个页面卸载时关闭ReportDocument;这样可以避免报告计数器不受控制地增加,从而停止应用程序。

protected void Page_Unload(object sender, EventArgs e)
{
    if (reportDocument != null)
        reportDocument.Close();
}

谢谢,但现在它在加载时显示报告,而不需要点击按钮。另外,请解释一下ButtonClicked = true的作用。像你一样,我也是VB开发人员。 - Abdul
修改后的代码:将ShowReport()方法从Page_Init移动到Page_Load,并添加以下判断语句: If Page.IsPostBack Then Report() End If - Abdul
我使用了一个名为ButtonClicked的布尔变量,它在按钮单击时被设置,以避免这个问题。 - Emanuele Greco
你也可以在关闭报告后进行处理。 - Incredible

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接