我正在Visual Studio 2008中开发一个水晶报表,它使用几个不同的数据库作为DataSource。一切正常,直到我尝试导航到第2页。有效的代码(因为它的结果有限)看起来像这样
Dim mssqlstr As String
mssqlstr = "SELECT TOP 1 t1.*, t2.column1, t2.column2 FROM
tablename1 As t1, tablename2 As t2 WHERE t1.ID = '" & txtID.Text & "'
AND t2.column2 = RTRIM(LEFT(t1.column_2, 2)) ORDER BY t1.ID DESC"
Dim DAms As New OleDbDataAdapter(mssqlstr, conn)
DAms.Fill(dsQRpt, "tablename")
'The code below is shared by the other subreport functions
QPrpt.Load(Server.MapPath("crreport.rpt"))
QPrpt.SetDataSource(dsQRpt)
crQtrProgress.ReportSource = QPrpt
crQtrProgress.RefreshReport()
但是当我需要来自此查询的更大结果集时
mssqlstr = "SELECT column1, column2 FROM tablename ORDER BY ID DESC"
我收到错误
登录失败。
详细信息:crdb_adoplus:未将对象引用设置为对象的实例 文件C中的错误:\ Users \ ALFRED~1.CAL \ AppData \ Local \ Temp \ rptQuarterlyProgress {10667888-35C5-41CA-93EF-214A64741965} .rpt:无法连接:登录参数不正确。“
两个查询都使用相同的连接字符串,报告字段来自数据表中的拖放字段(.xsd)
我还应该提一下,报告使用多个子报表,每个子报表来自不同的DataSource。所有子报表都以类似方式编码,并且工作正常,除非结果必须转移到下一页。如果我限制结果的数量,那么我从每个子报告获得所需的结果,但如果数据转移到另一个页面... kaboom!我得到“无法连接......”错误。
此外,DataSet连接到数据库并显示表数据没有问题。我一直在寻找解决方案,但没有找到符合我情况的任何东西。
感谢您提供的任何帮助
解 感谢 haraman 提供答案。这是工作代码
Dim dsQRpt = New Data.DataSet
Dim QPrpt = New ReportDocument
Protected Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Page.IsPostBack Then
If Session.Item("CRpt") IsNot Nothing Then
QPrpt = Session.Item("CRpt")
End If
crQtrProgress.ReportSource = QPrpt
crQtrProgress.RefreshReport()
Else
If Session.Item("CRpt") IsNot Nothing Then
Session.Remove("CRpt")
End If
Session.Add("CRpt", QPrpt)
End If
End Sub
Protected Sub btRunReport_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim mssqlstr As String
mssqlstr = "SELECT column1, column2 FROM tablename ORDER BY ID DESC"
Dim DAms As New OleDbDataAdapter(mssqlstr, conn)
DAms.Fill(dsQRpt, "tablename")
'Populate Report
QPrpt.Load(Server.MapPath("crreport.rpt"))
QPrpt.SetDataSource(dsQRpt)
crQtrProgress.ReportSource = QPrpt
Session.Add("CRpt", QPrpt)
End Sub
答案 0 :(得分:2)
似乎CrystalReportViewer在PostBack上丢失了ReportDocument。您可以尝试在会话中保存ReportDocument,然后在PostBack上将其重新分配给PageLoad事件上的CrystalReportViewer,例如
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
QPrpt.Load(Server.MapPath("crreport.rpt"))
QPrpt.SetDataSource(dsQRpt)
If Page.IsPostBack Then
If Session.Item("CRpt") IsNot Nothing Then
QPrpt = Session.Item("CRpt")
End If
Else
If Session.Item("CRpt") IsNot Nothing Then
Session.Remove("CRpt")
End If
Session.Add("CRpt", QPrpt)
End If
crQtrProgress.ReportSource = QPrpt
crQtrProgress.RefreshReport()
End Sub
Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
If Session.Item("CRpt") IsNot Nothing Then
Session.Remove("CRpt")
End If
Session.Add("CRpt", QPrpt)
End Sub
如果您在设置LogOnInfo时遇到特定问题,那么您也可以查看此SO帖子Report asking for database login on setting DataTable as DataSource