2011-07-24 43 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