2011-07-24 103 views
0

我看到的题目是不是新的,但我找遍许多网站,发现了许多线索,但没有为我工作:(Crystal报表 - 登录失败

我使用水晶报表查看器控制在我的ASP.NET 。申请报告很简单,有两个参数,我要通过我有两个CrystalReportsSource和我的网站上一个CrystalReportsViewer控制页面加载时运行此片段:。

CrystalReportSource1.ReportDocument.SetParameterValue("name", Session["name"].ToString()); 
CrystalReportSource1.ReportDocument.SetParameterValue("code", Session["code"].ToString()); 
CrystalReportViewer1.ReportSource = CrystalReportSource1; 

需要设置源,因为我有两种报告,并根据其他会话参数更改我应该在屏幕上打印哪个报告(以及哪个报告源应该我绑定)。

不幸的是,这段代码对我不起作用。 CRViewer向我显示一些提示/框,说〜“登录到数据库失败”(它只有我的翻译,因为这是我的语言环境)。我不知道如何使它工作。我的数据库(访问)也没有报告需要凭证登录(换句话说 - 我不必把它们放在任何地方)。

任何帮助,将不胜感激。

+0

确保您已经正确地给出ConnectionStrings。 – thevan

+0

ConnectionString通过向导生成。如果我点击选项编辑报告,报告文件将打开,所以这应该没问题... –

回答

0

由于您正在动态更改报表源,因此您还需要动态指定登录。

如果你的服务器和数据库是相同的,它会更容易。我没有尝试过,但你可以尝试一下。

Private Sub aMethod(ByVal name as String, ByVal sessionName as String)Handles Me.Load 

    Try 
     Dim cryRpt As New ReportDocument 
     Dim crtableLogoninfos As New TableLogOnInfos 
     Dim crtableLogoninfo As New TableLogOnInfo 
     Dim crConnectionInfo As New ConnectionInfo 
     Dim CrTables As Tables 
     Dim CrTable As Table 

     Select Case sessionName 
      Case OneSessionName 'specify a session name here' 
       Dim rptDoc = OneSessionName 
      Case AnotherSessionName 'specify other session name here' 
       Dim rptDoc = AnotherSessionName 
     End Select 

     Dim rptDoc = CType(rptDoc, String) 

     cryRpt.Load(rptDoc) 

     Select Case sessionName 
      Case OneSessionName 
      With crConnectionInfo 
       .ServerName = "yourServer1Name" 
       .DatabaseName = "YourDB1Name" 
       .UserID = "yourUser1Id" 
       .Password = "yourPassword1" 
      End With 
     Case AnotherSessionName 
      With crConnectionInfo 
       .ServerName = "yourServer2Name" 
       .DatabaseName = "YourDB2Name" 
       .UserID = "yourUser2Id" 
       .Password = "yourPassword2" 
      End With 
     End Select 

     CrTables = cryRpt.Database.Tables 
     For Each CrTable In CrTables 
      crtableLogoninfo = CrTable.LogOnInfo 
      crtableLogoninfo.ConnectionInfo = crConnectionInfo 
      CrTable.ApplyLogOnInfo(crtableLogoninfo) 
     Next 

     crsAllReports.PrintMode = PaperOrientation.Landscape 
     crsAllReports.ReportSource = cryRpt 
    Catch ex As Exception 
     lblError.Text = "No report" 
     lblError.Visible = True 
    End Try 

End Sub 

注意如果您的服务器和dbnames是相同的,您可能不需要第二个switch语句。取而代之的只是使用: With crConnectionInfo .ServerName = "yourServerName" .DatabaseName = "YourDBName" .UserID = "yourUserId" .Password = "yourPassword" End With