Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Change Database via Runtime code

I have been looking at several issues posted in the last few months that deal with changing the database connection information at runtime. However, none of them that I have found or read have provided the solution. I have tried a couple ways and have not been successful. I am able to get a report that has no parameters to run but a report with dynamic parameters still prompts for the database login.

Here is my code for changing the connection information.

Try
      Dim myTables As Tables = myReportDocument.Database.Tables

      For Each myTable As CrystalDecisions.CrystalReports.Engine.Table In myTables
        Dim myTableLogonInfo As TableLogOnInfo = myTable.LogOnInfo
        myTableLogonInfo.ConnectionInfo = myConnectionInfo

        myTable.ApplyLogOnInfo(myTableLogonInfo)
        'This code is needed if you are not logged on as the owner of the database objects
        'myTable.Location = "REPORTS." + myTable.Location
      Next

      For Each mySubRpt As ReportDocument In myReportDocument.Subreports
        Dim mySubTables As Tables = mySubRpt.Database.Tables
        For Each myTable As CrystalDecisions.CrystalReports.Engine.Table In mySubTables
          Dim myTableLogonInfo As TableLogOnInfo = myTable.LogOnInfo
          myTableLogonInfo.ConnectionInfo = myConnectionInfo

          myTable.ApplyLogOnInfo(myTableLogonInfo)
        Next
      Next


    Catch ex As Exception
      MessageBox.Show("Failed to Set Database connection information : " & ex.Message, "Database Login", MessageBoxButtons.OK)
    End Try

Here is the fist code that I had that would run the report and parameter screen but still pointed to the wrong datasource.

Dim myconn As New ConnectionInfo
Dim myTables As New TableLogOnInfos

myTables = RptViewer.LogOnInfo

With myconn
        For i As Integer = 0 To sArr.Length - 1
          If sArr(i).Contains("Data Source") Then
            .DatabaseName = sArr(i).Substring(InStr(sArr(i), "="))
            .ServerName = sArr(i).Substring(InStr(sArr(i), "="))
          End If
          If sArr(i).Contains("User") Then
            .UserID = sArr(i).Substring(InStr(sArr(i), "="))
          End If
          'If sArr(i).Contains("Password") Then
          '  .Password = sArr(i).Substring(InStr(sArr(i), "="))
          'End If
        Next i
        .Password = "password"
      End With
      SetDBLogonForReport(myconn, myReport)

      For Each tbl As TableLogOnInfo In myTables
        tbl.ConnectionInfo = myconn
      Next

Any solutions?

Former Member
Not what you were looking for? View more on this topic or Ask a question