水晶報表 Crystal Report 調用存儲過程時出錯 找不到表 ,解決方法。
用 CrystalReportViewer1 控件在asp.net的網頁上顯示報表,假設做報表時調用數據表數據的方式調用是能夠成功的。但報表是用存儲過程獲取數據方式會出現下面錯誤:
找不到表‘RptOpenCheck;1‘ 。 文件 G:\TEMP\FO-OpenCheck {6D191F06-DECF-4A25-88FC-8553E3D435AA}.rpt 內出錯: 找不到表。
Error: 未將對象引用設置到對象的實例。
The table ‘RptOpenCheck;1‘ could not be found. Error in File G:\TEMP\FO-OpenCheck {6D191F06-DECF-4A25-88FC-8553E3D435AA}.rpt: The table could not be found.
未能打開該連接。 未能打開該連接。
G:\TEMP\FO-OpenCheck {4E60249E-FC16-4F3D-A610-138FC3297171}.rpt
VS2005 環境,Crsytal Reports 11.5
代碼下面:
Dim crtableLogoninfos As New TableLogOnInfos
Dim crtableLogoninfo As New TableLogOnInfo
Dim crConnectionInfo As New ConnectionInfo
Dim crParameterFields As ParameterFields
Dim crParameterField As ParameterField
Dim crParameterValues As ParameterValues
Dim crParameterDefValues As ParameterValues
Dim crParameterValue As ParameterValue
Dim crParameterDiscreteValue As ParameterDiscreteValue
Dim CrTables As Tables
Dim CrTable As Table
Dim ReportName As String
Dim PrintTo As String ‘ P Printer V Window
Dim ReportPath As String
Dim UserName As String
Dim Password As String
Dim ServerName As String
Dim DatabaseName As String
Dim crReportDocument As New ReportDocument
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load, Me.Load
Dim strParam As String = Request("p")
If Right(strParam, 1) = "~" Then
strParam = Mid(strParam, 1, Len(strParam) - 1)
End If
‘參數轉換為數組
s = Split(strParam, "~")
‘use odbc connection database
With crConnectionInfo
.AllowCustomConnection = True
.ServerName = ”ODBCName"
‘.DatabaseName = "TempDB"
.UserID = "sa"
.Password = "Microwin"
End With
‘‘use SQL connection database
‘With crConnectionInfo
‘ .AllowCustomConnection = True
‘ .ServerName = "(local)"
‘ .DatabaseName = "TempDB"
‘ .UserID = "sa"
‘ .Password = "Microwin"
‘End With
‘指定報表路徑
ReportPath = Server.MapPath(Request.ApplicationPath)
ReportNamePath = ReportPath & "\testing.rpt"
‘check report file exists and Load Report
If System.IO.File.Exists(ReportNamePath) Then
crReportDocument.Load(ReportNamePath)
End If
‘設置報表文檔給報表控件
Me.CrystalReportViewer1.ReportSource = crReportDocument
CrTables = crReportDocument.Database.Tables
For Each CrTable In CrTables
crtableLogoninfo = CrTable.LogOnInfo
crtableLogoninfo.ConnectionInfo = crConnectionInfo
CrTable.ApplyLogOnInfo(crtableLogoninfo)
‘就是這句沒有加,所以會出現以上錯誤信息。僅僅有報表是調用存儲過程取數據時才會出現錯誤。花了我幾天時間,最終攻克了。
CrTable.Location = CrTable.Name
Next
‘設置控件顯示的屬性
With CrystalReportViewer1
.AutoDataBind = True
.ReuseParameterValuesOnRefresh = True
.EnableDatabaseLogonPrompt = False
.EnableParameterPrompt = False
CrystalReportViewer1.DisplayGroupTree = False
CrystalReportViewer1.DisplayPage = True
CrystalReportViewer1.DisplayToolbar = True
CrystalReportViewer1.ReportSource = crReportDocument
End With
If Not IsPostBack Then
‘取用戶請求的參數賦值給報表。假設報表須要參數的話。從第三個元素開始為報表參數值。
crParameterFields = Nothing
crParameterFields = CrystalReportViewer1.ParameterFieldInfo
Dim j As Integer = UBound(s, 1)
For i = 0 To crParameterFields.Count - 1
crParameterField = crParameterFields.Item(i)
crParameterValues = crParameterField.CurrentValues
crParameterDefValues = Nothing
crParameterDefValues = New ParameterValues
crParameterDefValues = crParameterField.DefaultValues
crParameterDiscreteValue = Nothing
crParameterDiscreteValue = New ParameterDiscreteValue
If i > (j - 2) Then
Select Case crParameterField.ParameterValueKind
Case ParameterValueKind.BooleanParameter
crParameterDiscreteValue.Value = False
Case ParameterValueKind.CurrencyParameter
crParameterDiscreteValue.Value = Nothing
Case ParameterValueKind.DateParameter
crParameterDiscreteValue.Value = System.DateTime.Now
Case ParameterValueKind.DateTimeParameter
crParameterDiscreteValue.Value = System.DateTime.Now
Case ParameterValueKind.NumberParameter
crParameterDiscreteValue.Value = Nothing
Case ParameterValueKind.StringParameter
crParameterDiscreteValue.Value = " "
Case ParameterValueKind.TimeParameter
crParameterDiscreteValue.Value = System.DateTime.Now
End Select
crParameterValues.Add(crParameterDiscreteValue)
Else
Select Case crParameterField.ParameterValueKind
Case ParameterValueKind.BooleanParameter
crParameterDiscreteValue.Value = IIf(s(i + 2) = "0", False, True)
Case ParameterValueKind.CurrencyParameter
crParameterDiscreteValue.Value = IIf(Trim(s(i + 2) & "") = "", Nothing, s(i + 2))
Case ParameterValueKind.DateParameter
crParameterDiscreteValue.Value = IIf(Trim(s(i + 2) & "") = "", Nothing, s(i + 2))
Case ParameterValueKind.DateTimeParameter
crParameterDiscreteValue.Value = IIf(Trim(s(i + 2) & "") = "", Nothing, s(i + 2))
Case ParameterValueKind.NumberParameter
crParameterDiscreteValue.Value = IIf(Trim(s(i + 2) & "") = "", Nothing, s(i + 2))
Case ParameterValueKind.StringParameter
crParameterDiscreteValue.Value = IIf(Trim(s(i + 2) & "") = "", " ", s(i + 2))
Case ParameterValueKind.TimeParameter
crParameterDiscreteValue.Value = IIf(Trim(s(i + 2) & "") = "", Nothing, s(i + 2))
End Select
crParameterValues.Add(crParameterDiscreteValue)
End If
Next
End If
End Sub
水晶報表 Crystal Report 調用存儲過程時出錯 找不到表 ,解決方法。