C# Crystal Report 抛出“无法打开连接”。仅在回发时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17914605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Crystal Report throws 'Failed to open the connection.' only on postback
提问by boruchsiper
I created a Crystal Report using the "pull" method to get data from SQL Server Express. I export the report to pdf. It works fine but only on pageLoad. I get this error whenever I try to export the report on a postback.
我使用“pull”方法创建了一个 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
Here is my code in vb.net:
这是我在 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
I get the same error when I run this code in a button click, or even in pageload if I wrap in in a if ispostback
clause.
当我在按钮单击中运行此代码时,或者如果我包含在if ispostback
子句中,甚至在页面加载中运行此代码时,我也会遇到相同的错误。
I've tried running this code in prerender, init etc... without success.
我试过在预渲染、初始化等中运行这段代码……但没有成功。
Crystal Reports version 13.0, .NET 3.5, SQL Server 2008 Express, VS 2010
Crystal Reports 13.0、.NET 3.5、SQL Server 2008 Express、VS 2010
Any help would be much appreciated.
任何帮助将非常感激。
采纳答案by Zec
This is a common problem with embedding Crystal Reports in ASP pages. Essentially, the authentication details are reset on a postback and need to be reassigned in the Page_Init handler. This articleprovides code examples and a link to MSDN explaining the ConnectionInfo class and how it works in this context. In case that link dies, the solution is in two steps:
这是在 ASP 页面中嵌入 Crystal Reports 的常见问题。本质上,身份验证详细信息在回发时重置,需要在 Page_Init 处理程序中重新分配。本文提供了代码示例和 MSDN 链接,解释了 ConnectionInfo 类及其在此上下文中的工作方式。如果链接失效,解决方案分两步:
First, import these:
首先,导入这些:
Imports CrystalDecisions.Shared
Imports CrystalDecisions.CrystalReports.Engine
Then, in the Page_Init
handler, re-apply connection info:
然后,在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
Note - SO doesn't like VB comments and wants to call them (and subsequent lines) literal strings instead. Used C/Java comment operator instead.
注意 - SO 不喜欢 VB 注释,而是希望将它们(和后续行)称为文字字符串。改为使用 C/Java 注释运算符。
回答by Syed Raza Khan
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
'''''''''''''''''''''' After many years of development i found solution.... Enjoy...
''''''''''''''''''''' 经过多年的发展,我找到了解决方案......享受......