Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VB

Changing Crystal Report Database logon information at runtime in VS2005

3.40/5 (2 votes)
25 Mar 2008CPOL 1   484  
Resolving Crystal Report Table not Found error message after migrationg the VS2003 Code to VS2005

Introduction

This Article solve the issue of "table not found" error message while exporting the crystal report to the PDF/DOC format in VS2005 after migrating the old code developed in VS2003.

Background

Previously in VS2003, table.Location would report "DATABASE.dbo.NAME" and it was possible to use this to change the Location, but in vs2005 table.Location only reports back the NAME.

Using the code

Deploye the attached file and then pass the ReportDocument as argument to CReportAuthentication.Impersonate(ReportDocument Object)from the place where you want to launch the report.

			Imports System.Configuration
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.ReportSource
Imports CrystalDecisions.Shared
Public Class CReportAuthentication
    Public Shared Sub Impersonate(ByVal myRpt As ReportDocument)
        ' Set the login info dynamically for the report
        Dim username As String = ConfigurationManager.AppSettings("ReportUser")
        Dim password As String = ConfigurationManager.AppSettings("ReportPassword")
        Dim Server As String = ConfigurationManager.AppSettings("Server")
        Dim Database As String = ConfigurationManager.AppSettings("Database")
        Dim logonInfo As New TableLogOnInfo

        Dim table As Table

        For Each table In myRpt.Database.Tables
            logonInfo = table.LogOnInfo
            logonInfo.ConnectionInfo.ServerName = Server
            logonInfo.ConnectionInfo.DatabaseName = Database
            logonInfo.ConnectionInfo.UserID = username
            logonInfo.ConnectionInfo.Password = password
            table.ApplyLogOnInfo(logonInfo)
            'Previously in VS2003, table.Location would report "DATABASE.dbo.NAME"  - 
            'and it was possible to use this to change the Location, but in vs2005 table.
            'Location only reports back the NAME.  See below for a fix.
            'http://vstoolsforum.com/blogs/crystal_reports/archive/2007/06.aspx
            table.Location = Database & ".dbo." & table.Name
        Next table
    End Sub
End Class

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)