簡體   English   中英

Crystal Report引發“無法打開連接”。 僅回發

[英]Crystal Report throws 'Failed to open the connection.' only on postback

我使用“拉”方法創建了一個Crystal Report,以從SQL Server Express獲取數據。 我將報告導出為pdf。 它工作正常,但僅在pageLoad上有效。 每當我嘗試在回發中導出報告時,都會出現此錯誤。

Failed to open the connection.
Details:  [Database Vendor Code: 4060 ]Failed to open the connection.
CrystalReportPull {2B7D5D2A-C29F-4F27-AFAD-EEAECD909D08}.rpt
Details:  [Database Vendor Code: 4060 ] 
  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

 Exception Details: System.Runtime.InteropServices.COMException: Failed to open the connection.
Details:  [Database Vendor Code: 4060 ]Failed to open the connection.
CrystalReportPull {2B7D5D2A-C29F-4F27-AFAD-EEAECD909D08}.rpt
Details:  [Database Vendor Code: 4060 ]

Source Error: 

Line 58:                 CrystalTable.ApplyLogOnInfo(logonInfo)
Line 59:             Next
Line 60:             report.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, True, "ExportedReport")
Line 61:             report.Dispose()
Line 62:         End If

 Source File:  C:\Users\boruch\Dropbox\Korns-ConnectionStr\reports\CreateReport.aspx.vb    Line:  60 

Stack Trace: 

[COMException (0x800002f4): Failed to open the connection.
Details:  [Database Vendor Code: 4060 ]
Failed to open the connection.
CrystalReportPull {2B7D5D2A-C29F-4F27-AFAD-EEAECD909D08}.rpt
Details:  [Database Vendor Code: 4060 ]]
   CrystalDecisions.ReportAppServer.Controllers.ReportSourceClass.Export(ExportOptions pExportOptions, RequestContext pRequestContext) +0
   CrystalDecisions.ReportSource.EromReportSourceBase.ExportToStream(ExportRequestContext reqContext) +525

[InternalException: Failed to open the connection.
Details:  [Database Vendor Code: 4060 ]
Failed to open the connection.
CrystalReportPull {2B7D5D2A-C29F-4F27-AFAD-EEAECD909D08}.rpt
Details:  [Database Vendor Code: 4060 ]]
   CrystalDecisions.ReportAppServer.ConvertDotNetToErom.ThrowDotNetException(Exception e) +346
   CrystalDecisions.ReportSource.EromReportSourceBase.ExportToStream(ExportRequestContext reqContext) +627
   CrystalDecisions.CrystalReports.Engine.FormatEngine.ExportToStream(ExportRequestContext reqContext) +1203
   CrystalDecisions.CrystalReports.Engine.ReportDocument.ExportToStream(ExportOptions options) +150
   CrystalDecisions.CrystalReports.Engine.ReportDocument.ExportToHttpResponse(ExportOptions options, HttpResponse response, Boolean asAttachment, String attachmentName) +211
   CrystalDecisions.CrystalReports.Engine.ReportDocument.ExportToHttpResponse(ExportFormatType formatType, HttpResponse response, Boolean asAttachment, String attachmentName) +240
   reports_CreateReport.Page_SaveStateComplete(Object sender, EventArgs e) in C:\Users\boruch\Dropbox\Korns-ConnectionStr\reports\CreateReport.aspx.vb:60
   System.Web.UI.Page.OnSaveStateComplete(EventArgs e) +9644490
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1282

這是我在vb.net中的代碼:

txt_from.Text = Now.Date.AddYears(-1)
        txt_to.Text = Now.Date
        If IsPostBack Then
            Dim report As New ReportDocument()
            report.Load(Server.MapPath("~/reports/CrystalReportPull.rpt"), OpenReportMethod.OpenReportByTempCopy)
            Dim custID As Integer = -1
            If ddl_Customer.SelectedValue <> "" Then
                custID = CInt(ddl_Customer.SelectedValue)

            End If
            report.SetParameterValue(0, txt_from.Text)
            report.SetParameterValue(1, txt_to.Text)
            report.SetParameterValue(2, custID)


            Dim logonInfo As New CrystalDecisions.Shared.TableLogOnInfo()
            Dim CrystalTable As CrystalDecisions.CrystalReports.Engine.Table
            For Each CrystalTable In report.Database.Tables
                logonInfo = CrystalTable.LogOnInfo
                logonInfo.ConnectionInfo.ServerName = "BORUCH-PC\SQLEXPRESS"
                logonInfo.ConnectionInfo.DatabaseName = ""
                logonInfo.ConnectionInfo.UserID = ""
                logonInfo.ConnectionInfo.Password = ""

                CrystalTable.ApplyLogOnInfo(logonInfo)
            Next
            report.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, True, "ExportedReport")
            report.Dispose()
        End If

當我在一次按鈕單擊中運行此代碼時,或者如果在一個if ispostback子句中包裝它,甚至在頁面加載中,我也會遇到相同的錯誤。

我嘗試在prerender,init等中運行此代碼,但未成功。

Crystal Reports版本13.0,.NET 3.5,SQL Server 2008 Express,VS 2010

任何幫助將非常感激。

這是將Crystal Reports嵌入ASP頁中的常見問題。 本質上,身份驗證詳細信息會在回發時重置,並且需要在Page_Init處理程序中重新分配。 本文提供了代碼示例和MSDN鏈接,解釋了ConnectionInfo類及其在此上下文中的工作方式。 如果鏈接消失,則解決方案分為兩個步驟:

首先,導入以下內容:

Imports CrystalDecisions.Shared
Imports CrystalDecisions.CrystalReports.Engine

然后,在Page_Init處理程序中,重新應用連接信息:

Dim myConnectionInfo As ConnectionInfo = New ConnectionInfo()
Dim myReport As New ReportDocument()
myReport.Load(Server.MapPath("ReportName")) -- name of the crystal report

Dim myTables As Tables = myReport.Database.Tables

For Each myTable As CrystalDecisions.CrystalReports.Engine.Table In myTables
  Dim myTableLogonInfo As TableLogOnInfo = myTable.LogOnInfo
  myConnectionInfo.ServerName = "" -- <SQL servername>
  myConnectionInfo.DatabaseName = "" -- leave database name blank
  myConnectionInfo.UserID = "" -- username
  myConnectionInfo.Password = "" -- password
  myTableLogonInfo.ConnectionInfo = myConnectionInfo
  myTable.ApplyLogOnInfo(myTableLogonInfo)
Next

CrystalReportViewer1.ReportSource = myReport

注意-SO不喜歡VB注釋,而是想調用它們(和后續行)文字字符串。 改用C / Java注釋運算符。

    Try
        Dim myConnectionInfo As ConnectionInfo = New ConnectionInfo()
        Dim rpt As New rptCardPrinting()

        Dim myTables As Tables = rpt.Database.Tables

        For Each myTable As CrystalDecisions.CrystalReports.Engine.Table In myTables
            Dim myTableLogonInfo As TableLogOnInfo = myTable.LogOnInfo
            myConnectionInfo.ServerName = sqlconn
            myConnectionInfo.DatabaseName = ""
            myConnectionInfo.UserID = sqluser
            myConnectionInfo.Password = sqlpass
            myTableLogonInfo.ConnectionInfo = myConnectionInfo
            myTable.ApplyLogOnInfo(myTableLogonInfo)
        Next
        frmReportViewer.CrystalReportViewer1.ReportSource = rpt

        rpt.SetParameterValue("prt", txtCnicPassport.Text)
        rpt.PrintToPrinter(1, False, 0, 0)
        rpt.Close()
        rpt.Dispose()


    Catch ex As Exception
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

    End Try

    '' \\endreport

''''''''''''''''''''''經過多年的發展,我找到了解決方案....享受...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM