博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
水晶报表 Crystal Report 调用存储过程时出错 找不到表 ,解决方法。
阅读量:5044 次
发布时间:2019-06-12

本文共 5076 字,大约阅读时间需要 16 分钟。

用 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

转载于:https://www.cnblogs.com/claireyuancy/p/7119421.html

你可能感兴趣的文章