Technical Questions
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Technical Questions
Message Icon Topic: Changing databaseLogon at runtime. Post Reply Post New Topic
Author Message
Ibanez
Newbie
Newbie


Joined: 06 May 2011
Online Status: Offline
Posts: 3
Quote Ibanez Replybullet Topic: Changing databaseLogon at runtime.
     Posted: 17 May 2011 at 2:03am
I have created a CR in Visual Studio 2010, and during creation I point to a server and database. To prevent that you get a dialog during runtime, you set the database credentials before the CR is loaded like this:
Rapport.SetDatabaseLogon("UserId", "Password")

The question now is: what if you want to connect to the same database but stored on another server? According to the books I read, the said use something like this:
Rapport.SetDatabaseLogon("UserId", "Password", "localhost\SQLEXPRESS2008", "database")

But this does not work!!! I'm getting the 'Database Login' dialog, where the serverTextbox and databaseTextbox are filled with the servername and databasename I used during creation of the report. According to the official guide CR 2008 the connection information is stored in the rpt-file. But how can I access this and change the connInfo? Is there anyone who can help me?

Thanks
IP IP Logged
Ibanez
Newbie
Newbie


Joined: 06 May 2011
Online Status: Offline
Posts: 3
Quote Ibanez Replybullet Posted: 17 May 2011 at 11:31pm
I have found the solution.

Imports CrystalDecisions
Imports CrystalDecisions.CrystalReports.Engine
Imports System.Configuration
Imports System.Data.SqlClient
Imports CrystalDecisions.Shared
Imports System.Data

Public Class frmPrintWorkorder
    Dim decomposeConnString As New SqlConnectionStringBuilder
    Dim connString As String
    Private Sub frmPrintWorkorder_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim crTableLogonInfo As New TableLogOnInfo
        Dim crConnectionInfo As New ConnectionInfo
        Dim crTables As Tables = Nothing
        Dim crTable As Table = Nothing

        connString = ConfigurationManager.ConnectionStrings("name in app.config").ConnectionString
        decomposeConnString.ConnectionString = connString
        Dim userId As String = decomposeConnString.UserID
        Dim password As String = decomposeConnString.Password
        Dim server As String = decomposeConnString.DataSource
        Dim database As String = decomposeConnString.InitialCatalog
        Dim security As Boolean = decomposeConnString.IntegratedSecurity

        Dim Rapport As Werkbon = Me.Werkbon1
        Rapport.SetDatabaseLogon(userId, password)
        With crConnectionInfo
            .ServerName = server
            .DatabaseName = database
            .UserID = userId
            .Password = password
            .IntegratedSecurity = security
        End With

        crTables = Rapport.Database.Tables
        For Each crTable In crTables
            crTableLogonInfo = crTable.LogOnInfo
            crTableLogonInfo.ConnectionInfo = crConnectionInfo
            crTable.ApplyLogOnInfo(crTableLogonInfo)
        Next
    End Sub
End Class
IP IP Logged
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum