0
votes

I see the topic isn't new, but I searched many sites, found many clues, but nothing worked for me:(

I am using Crystal Reports Viewer control in my ASP.NET application. Report is quite simple, there are two parameters which I have to pass. I have two CrystalReportsSource and one CrystalReportsViewer controls on my site. When the page loads I run this snippet:

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

Setting source is needed, because I have two kinds of report and depending on some other session parameter I change which report should I print on screen (and which report source should I bind).

Unfortunately, this code doesn't work for me. CRViewer shows me little prompt/box saying that ~"Logging into database failed" (its only my translation, cause this is in my locale). I have no idea how to make it works. Nor my DB (Access), nor reports need credential to login (other words - I don't have to put them in any place).

Any help would be appreciated.

1
Make sure that you had given the ConnectionStrings correctly.thevan
ConnectionString is generated via wizard. If I click option edit report the report file will opens, so this should be fine...Mateusz Chromiński

1 Answers

0
votes

Since you are dynamically changing the report source, you will need to dynamically specify the logon as well.

It would be easier if your server and database are thesame. I have not tried this myself but you may give it a try.

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

Note 'If your server and dbnames are thesame you may, you may not need the second switch statement. Instead just use: With crConnectionInfo .ServerName = "yourServerName" .DatabaseName = "YourDBName" .UserID = "yourUserId" .Password = "yourPassword" End With