1272124 - How to get Crystal Report to use the database connection defined in a web.config file

SAP Knowledge Base Article - Public

1272124 - How to get Crystal Report to use the database connection defined in a web.config file

Symptom

Resolution

  • In web.config use the appSettings tag:

<configuration>
   <appSettings>
        <add key=”myDatabase” value=”databaseName”/>
        <add key=”myUsername” value=”sa”/>
        <add key ……/>
    </appSettings>
…..

  • In  code, these values are accessed as follows:

Connectioninfo.DatabaseName = System.Configuraton.AppSettings(“myDatabase”).ToString()

  • Sample of complete code:
Public Sub LogonReport(ByVal rptReport As CrystalDecisions.CrystalReports.Engine.ReportDocument)
     
'Load report
        crReportDocument = rptReport
        crReportDocument.Load(Server.MapPath("") & ReportName)
 
'Code to log on to the database
        crDataBase = crReportDocument.Database
        crTables = crDataBase.Tables
        For Each crTable In crTables
            With crConnInfo
                .ServerName = ConfigurationSettings.AppSettings("strServer")
                .DatabaseName = ConfigurationSettings.AppSettings("strDBase")
                .UserID = ConfigurationSettings.AppSettings("strUID")
                .Password = ConfigurationSettings.AppSettings("strPWD")
            End With
 
            crTableLogonInfo = crTable.LogOnInfo
            crTableLogonInfo.ConnectionInfo = crConnInfo
            crTable.ApplyLogOnInfo(crTableLogonInfo)
            crTable.Location = strDBase & ".dbo." & crTable.Name
            crTable.ApplyLogOnInfo(crTableLogonInfo)
        Next
...
'If need be, also logon to subreport tables....
...
 
'View report
CrystalReportViewer1.ReportSource = rptReport

Keywords

appSettings web.config database logon connect , KBA , BI-DEV-NET , BI Software Development Kits (SDKs) - .NET or Other , How To

Product

Crystal Reports 2008 V1