<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: crystal code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 25 Jul 2008 00:39:30 GMT</pubDate>
    <description>DZone Snippets: crystal code</description>
    <item>
      <title>Programmatically Change a Crystal Reports datasource location</title>
      <link>http://snippets.dzone.com/posts/show/4029</link>
      <description>I had an aspx.net project where I needed to generate a pdf.&lt;br /&gt;I used crystal to do the heavy lifting of geneating the pdf for me.&lt;br /&gt;But as I needed to deploy it to multiple databases/clients I needed a way of programmatically changing the datasource location of the report.  So with a bit of help from the crystal knowledgebase here is the code I use.&lt;br /&gt;There is a lot of debugging information in here and trust me when it fails you need it.&lt;br /&gt;The .dbo of the location reference obviously means I was working with sql server.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;   ' --------------------------------------------------------------------------&lt;br /&gt;   ' SetupReport&lt;br /&gt;   '&lt;br /&gt;   ' Description:&lt;br /&gt;   '    sets up the crystal report&lt;br /&gt;   '&lt;br /&gt;   ' Arguments:&lt;br /&gt;   '    objCrystalReportDocument             - crystal report document object&lt;br /&gt;   '&lt;br /&gt;   ' Dependencies:&lt;br /&gt;   '    GetConnnectionInfo&lt;br /&gt;   '    CrystalDescisions&lt;br /&gt;   '&lt;br /&gt;   ' History:&lt;br /&gt;   ' 03/17/2006 - WSR : created&lt;br /&gt;   '&lt;br /&gt;   Private Function SetupReport(ByRef objCrystalReportDocument As CrystalDecisions.CrystalReports.Engine.ReportDocument) As System.Boolean&lt;br /&gt;&lt;br /&gt;      ' a heck of a lot of objects used&lt;br /&gt;      Dim crParameterDiscreteValue As CrystalDecisions.Shared.ParameterDiscreteValue&lt;br /&gt;      Dim crParameterFieldDefinitions As CrystalDecisions.CrystalReports.Engine.ParameterFieldDefinitions&lt;br /&gt;      Dim crParameterFieldLocation As CrystalDecisions.CrystalReports.Engine.ParameterFieldDefinition&lt;br /&gt;      Dim crParameterValues As CrystalDecisions.Shared.ParameterValues&lt;br /&gt;      Dim crSections As CrystalDecisions.CrystalReports.Engine.Sections&lt;br /&gt;      Dim crSection As CrystalDecisions.CrystalReports.Engine.Section&lt;br /&gt;      Dim crReportObjects As CrystalDecisions.CrystalReports.Engine.ReportObjects&lt;br /&gt;      Dim crReportObject As CrystalDecisions.CrystalReports.Engine.ReportObject&lt;br /&gt;      Dim crSubreportDocument As CrystalDecisions.CrystalReports.Engine.ReportDocument&lt;br /&gt;      Dim crSubreportObject As CrystalDecisions.CrystalReports.Engine.SubreportObject&lt;br /&gt;      Dim crConnectionInfo As CrystalDecisions.Shared.ConnectionInfo&lt;br /&gt;      Dim crDatabase As CrystalDecisions.CrystalReports.Engine.Database&lt;br /&gt;      Dim crTables As CrystalDecisions.CrystalReports.Engine.Tables&lt;br /&gt;      Dim aTable As CrystalDecisions.CrystalReports.Engine.Table&lt;br /&gt;      Dim bTable As CrystalDecisions.CrystalReports.Engine.Table&lt;br /&gt;      Dim crTableLogOnInfo As CrystalDecisions.Shared.TableLogOnInfo&lt;br /&gt;      Dim blnTest As System.Boolean&lt;br /&gt;      Dim strLocation As System.String&lt;br /&gt;      Dim blnErrors As System.Boolean&lt;br /&gt;&lt;br /&gt;      ' instantiate the debug page&lt;br /&gt;      m_strDebugPage = New System.Text.StringBuilder(4096)&lt;br /&gt;      blnErrors = False&lt;br /&gt;&lt;br /&gt;      crConnectionInfo = GetConnectionInfo()&lt;br /&gt;&lt;br /&gt;      crDatabase = objCrystalReportDocument.Database&lt;br /&gt;&lt;br /&gt;      crTables = crDatabase.Tables&lt;br /&gt;&lt;br /&gt;      'For intCounter = 0 To objCrystalReportDocument.Database.Tables.Count - 1&lt;br /&gt;      For Each aTable In crTables&lt;br /&gt;&lt;br /&gt;         crTableLogOnInfo = aTable.LogOnInfo&lt;br /&gt;&lt;br /&gt;         OutputDebugLine("BEFORE")&lt;br /&gt;         OutputDebugLine("TABLE NAME: " &amp; aTable.Name)&lt;br /&gt;         OutputDebugLine("TABLE LOC: " &amp; aTable.Location)&lt;br /&gt;         OutputDebugLine("SERVER: " &amp; crTableLogOnInfo.ConnectionInfo.ServerName)&lt;br /&gt;         OutputDebugLine("DB: " &amp; crTableLogOnInfo.ConnectionInfo.DatabaseName)&lt;br /&gt;         OutputDebugLine("UID: " &amp; crTableLogOnInfo.ConnectionInfo.UserID)&lt;br /&gt;         OutputDebugLine("PWD: " &amp; crTableLogOnInfo.ConnectionInfo.Password)&lt;br /&gt;         OutputDebugLine("RN: " &amp; crTableLogOnInfo.ReportName)&lt;br /&gt;         OutputDebugLine("TN: " &amp; crTableLogOnInfo.TableName)&lt;br /&gt;&lt;br /&gt;         crTableLogOnInfo.ConnectionInfo = crConnectionInfo&lt;br /&gt;         aTable.ApplyLogOnInfo(crTableLogOnInfo)&lt;br /&gt;         strLocation = crConnectionInfo.DatabaseName &amp; ".dbo." &amp; aTable.Location.Substring(aTable.Location.LastIndexOf(".") + 1)&lt;br /&gt;         OutputDebugLine("New Location: " &amp; strLocation)&lt;br /&gt;         Try&lt;br /&gt;            aTable.Location = strLocation&lt;br /&gt;         Catch ex As Exception&lt;br /&gt;            OutputDebugLine("Set Location Error: " &amp; ex.ToString)&lt;br /&gt;            blnErrors = True&lt;br /&gt;         End Try&lt;br /&gt;&lt;br /&gt;         OutputDebugLine("AFTER")&lt;br /&gt;         OutputDebugLine("TABLE NAME: " &amp; aTable.Name)&lt;br /&gt;         OutputDebugLine("TABLE LOC: " &amp; aTable.Location)&lt;br /&gt;         OutputDebugLine("SERVER: " &amp; crTableLogOnInfo.ConnectionInfo.ServerName)&lt;br /&gt;         OutputDebugLine("DB: " &amp; crTableLogOnInfo.ConnectionInfo.DatabaseName)&lt;br /&gt;         OutputDebugLine("UID: " &amp; crTableLogOnInfo.ConnectionInfo.UserID)&lt;br /&gt;         OutputDebugLine("PWD: " &amp; crTableLogOnInfo.ConnectionInfo.Password)&lt;br /&gt;         OutputDebugLine("RN: " &amp; crTableLogOnInfo.ReportName)&lt;br /&gt;         OutputDebugLine("TN: " &amp; crTableLogOnInfo.TableName)&lt;br /&gt;         Try&lt;br /&gt;            blnTest = aTable.TestConnectivity()&lt;br /&gt;            OutputDebugLine("CONNECTED? " &amp; blnTest.ToString())&lt;br /&gt;         Catch ex As Exception&lt;br /&gt;            OutputDebugLine("CONNECTED? NO")&lt;br /&gt;            OutputDebugLine(ex.ToString)&lt;br /&gt;            blnErrors = True&lt;br /&gt;         End Try&lt;br /&gt;&lt;br /&gt;         '// THIS STUFF HERE IS FOR REPORTS HAVING SUBREPORTS &lt;br /&gt;         '// set the sections object to the current report's section &lt;br /&gt;         crSections = objCrystalReportDocument.ReportDefinition.Sections&lt;br /&gt;&lt;br /&gt;         '// loop through all the sections to find all the report objects &lt;br /&gt;         For Each crSection In crSections&lt;br /&gt;&lt;br /&gt;            crReportObjects = crSection.ReportObjects&lt;br /&gt;&lt;br /&gt;            '//loop through all the report objects in there to find all subreports &lt;br /&gt;            For Each crReportObject In crReportObjects&lt;br /&gt;&lt;br /&gt;               If crReportObject.Kind = ReportObjectKind.SubreportObject Then&lt;br /&gt;&lt;br /&gt;                  crSubreportObject = CType(crReportObject, CrystalDecisions.CrystalReports.Engine.SubreportObject)&lt;br /&gt;                  '//open the subreport object and logon as for the general report &lt;br /&gt;                  crSubreportDocument = crSubreportObject.OpenSubreport(crSubreportObject.SubreportName)&lt;br /&gt;                  crDatabase = crSubreportDocument.Database&lt;br /&gt;                  crTables = crDatabase.Tables&lt;br /&gt;&lt;br /&gt;                  For Each bTable In crTables&lt;br /&gt;&lt;br /&gt;                     crTableLogOnInfo = bTable.LogOnInfo&lt;br /&gt;&lt;br /&gt;                     OutputDebugLine("BEFORE")&lt;br /&gt;                     OutputDebugLine("TABLE NAME: " &amp; bTable.Name)&lt;br /&gt;                     OutputDebugLine("TABLE LOC: " &amp; bTable.Location)&lt;br /&gt;                     OutputDebugLine("SERVER: " &amp; crTableLogOnInfo.ConnectionInfo.ServerName)&lt;br /&gt;                     OutputDebugLine("DB: " &amp; crTableLogOnInfo.ConnectionInfo.DatabaseName)&lt;br /&gt;                     OutputDebugLine("UID: " &amp; crTableLogOnInfo.ConnectionInfo.UserID)&lt;br /&gt;                     OutputDebugLine("PWD: " &amp; crTableLogOnInfo.ConnectionInfo.Password)&lt;br /&gt;                     OutputDebugLine("RN: " &amp; crTableLogOnInfo.ReportName)&lt;br /&gt;                     OutputDebugLine("TN: " &amp; crTableLogOnInfo.TableName)&lt;br /&gt;&lt;br /&gt;                     crTableLogOnInfo.ConnectionInfo = crConnectionInfo&lt;br /&gt;                     bTable.ApplyLogOnInfo(crTableLogOnInfo)&lt;br /&gt;                     strLocation = crConnectionInfo.DatabaseName &amp; ".dbo." &amp; bTable.Location.Substring(bTable.Location.LastIndexOf(".") + 1)&lt;br /&gt;                     OutputDebugLine("New Location: " &amp; strLocation)&lt;br /&gt;                     Try&lt;br /&gt;                        bTable.Location = strLocation&lt;br /&gt;                     Catch ex As Exception&lt;br /&gt;                        OutputDebugLine("Set Location Error: " &amp; ex.ToString)&lt;br /&gt;                        blnErrors = True&lt;br /&gt;                     End Try&lt;br /&gt;&lt;br /&gt;                     OutputDebugLine("AFTER")&lt;br /&gt;                     OutputDebugLine("TABLE NAME: " &amp; bTable.Name)&lt;br /&gt;                     OutputDebugLine("TABLE LOC: " &amp; bTable.Location)&lt;br /&gt;                     OutputDebugLine("SERVER: " &amp; crTableLogOnInfo.ConnectionInfo.ServerName)&lt;br /&gt;                     OutputDebugLine("DB: " &amp; crTableLogOnInfo.ConnectionInfo.DatabaseName)&lt;br /&gt;                     OutputDebugLine("UID: " &amp; crTableLogOnInfo.ConnectionInfo.UserID)&lt;br /&gt;                     OutputDebugLine("PWD: " &amp; crTableLogOnInfo.ConnectionInfo.Password)&lt;br /&gt;                     OutputDebugLine("RN: " &amp; crTableLogOnInfo.ReportName)&lt;br /&gt;                     OutputDebugLine("TN: " &amp; crTableLogOnInfo.TableName)&lt;br /&gt;                     Try&lt;br /&gt;                        blnTest = bTable.TestConnectivity()&lt;br /&gt;                        OutputDebugLine("CONNECTED? " &amp; blnTest.ToString())&lt;br /&gt;                     Catch ex As Exception&lt;br /&gt;                        OutputDebugLine("CONNECTED? NO")&lt;br /&gt;                        OutputDebugLine(ex.ToString)&lt;br /&gt;                        blnErrors = True&lt;br /&gt;                     End Try&lt;br /&gt;&lt;br /&gt;                  Next bTable&lt;br /&gt;&lt;br /&gt;               End If&lt;br /&gt;&lt;br /&gt;            Next crReportObject&lt;br /&gt;&lt;br /&gt;         Next crSection&lt;br /&gt;&lt;br /&gt;      Next aTable&lt;br /&gt;&lt;br /&gt;      ' get parameter fields from report&lt;br /&gt;      crParameterFieldDefinitions = objCrystalReportDocument.DataDefinition.ParameterFields&lt;br /&gt;&lt;br /&gt;      '    ' Set the first parameter&lt;br /&gt;      '    ' - Get the parameter, tell it to use the current values vs default value.&lt;br /&gt;      '    ' - Tell it the parameter contains 1 discrete value vs multiple values.&lt;br /&gt;      '    ' - Set the parameter's value.&lt;br /&gt;      '    ' - Add it and apply it.&lt;br /&gt;      '    ' - Repeat these statements for each parameter.&lt;br /&gt;      '    '&lt;br /&gt;      crParameterFieldLocation = crParameterFieldDefinitions.Item("@psindex")&lt;br /&gt;      crParameterValues = crParameterFieldLocation.CurrentValues&lt;br /&gt;      crParameterDiscreteValue = New CrystalDecisions.Shared.ParameterDiscreteValue&lt;br /&gt;      crParameterDiscreteValue.Value = m_intReportID&lt;br /&gt;      crParameterValues.Add(crParameterDiscreteValue)&lt;br /&gt;      crParameterFieldLocation.ApplyCurrentValues(crParameterValues)&lt;br /&gt;&lt;br /&gt;      ' if there were errors&lt;br /&gt;      If blnErrors Then&lt;br /&gt;&lt;br /&gt;         ' display debug page&lt;br /&gt;         OutputDebugPage()&lt;br /&gt;&lt;br /&gt;      End If&lt;br /&gt;&lt;br /&gt;   End Function&lt;br /&gt;   ' --------------------------------------------------------------------------&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;   ' --------------------------------------------------------------------------&lt;br /&gt;   ' GetConnectionInfo&lt;br /&gt;   '&lt;br /&gt;   ' Description:&lt;br /&gt;   '    retrieves the connection information from the data layer object&lt;br /&gt;   '&lt;br /&gt;   ' Arguments: none&lt;br /&gt;   '&lt;br /&gt;   ' Dependencies:&lt;br /&gt;   '    DataLayer.GetConnectInfo&lt;br /&gt;   '&lt;br /&gt;   ' History:&lt;br /&gt;   ' 03/17/2006 - WSR : created&lt;br /&gt;   '&lt;br /&gt;   Private Function GetConnectionInfo() As CrystalDecisions.Shared.ConnectionInfo&lt;br /&gt;&lt;br /&gt;      Dim objConnectionInfo As CrystalDecisions.Shared.ConnectionInfo&lt;br /&gt;      Dim strDSN As System.String&lt;br /&gt;      Dim strDB As System.String&lt;br /&gt;      Dim strUID As System.String&lt;br /&gt;      Dim strPWD As System.String&lt;br /&gt;      Dim blnTrust As System.Boolean&lt;br /&gt;&lt;br /&gt;      ' get connection information from data layer&lt;br /&gt;      m_objDataLayer.GetConnectInfo(strDSN, strDB, strUID, strPWD, blnTrust)&lt;br /&gt;&lt;br /&gt;      ' create new crystal connection info object&lt;br /&gt;      objConnectionInfo = New CrystalDecisions.Shared.ConnectionInfo&lt;br /&gt;&lt;br /&gt;      ' populate it&lt;br /&gt;      objConnectionInfo.IntegratedSecurity = False&lt;br /&gt;      objConnectionInfo.ServerName = strDSN&lt;br /&gt;      objConnectionInfo.UserID = strUID&lt;br /&gt;      objConnectionInfo.Password = strPWD&lt;br /&gt;      objConnectionInfo.DatabaseName = strDB&lt;br /&gt;&lt;br /&gt;      ' return object&lt;br /&gt;      GetConnectionInfo = objConnectionInfo&lt;br /&gt;&lt;br /&gt;   End Function&lt;br /&gt;   ' --------------------------------------------------------------------------&lt;br /&gt;&lt;br /&gt;   ' --------------------------------------------------------------------------&lt;br /&gt;   ' OutputDebugLine&lt;br /&gt;   '&lt;br /&gt;   ' Description: appends a line to the debug string builder&lt;br /&gt;   '&lt;br /&gt;   ' Arguments: text to add&lt;br /&gt;   '&lt;br /&gt;   ' Dependencies:&lt;br /&gt;   '    m_strDebugPage&lt;br /&gt;   '&lt;br /&gt;   ' History:&lt;br /&gt;   ' 03/17/2006 - WSR : created&lt;br /&gt;   ' 2007.04.25 - WSR : revised to use string builder&lt;br /&gt;   '&lt;br /&gt;   Function OutputDebugLine(ByVal strLine As System.String) As System.Boolean&lt;br /&gt;&lt;br /&gt;      m_strDebugPage.Append("&lt;div&gt;" &amp; Server.HtmlEncode(strLine) &amp; "&lt;/div&gt;")&lt;br /&gt;&lt;br /&gt;   End Function&lt;br /&gt;   ' --------------------------------------------------------------------------&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;   ' --------------------------------------------------------------------------&lt;br /&gt;   ' OutputDebugPage&lt;br /&gt;   '&lt;br /&gt;   ' Description: sends debug string builder to response&lt;br /&gt;   '&lt;br /&gt;   ' Arguments: none&lt;br /&gt;   '&lt;br /&gt;   ' Dependencies:&lt;br /&gt;   '    m_strDebugPage&lt;br /&gt;   '&lt;br /&gt;   ' History:&lt;br /&gt;   ' 2007.04.25 - WSR : created&lt;br /&gt;   '&lt;br /&gt;   Function OutputDebugPage() As System.Boolean&lt;br /&gt;&lt;br /&gt;      With Response&lt;br /&gt;         .ClearHeaders()&lt;br /&gt;         .ClearContent()&lt;br /&gt;         .ContentType = "text/html"&lt;br /&gt;      End With&lt;br /&gt;&lt;br /&gt;      Response.Write("&lt;html&gt;&lt;head&gt;&lt;title&gt;Debug Page&lt;/title&gt;&lt;/head&gt;&lt;body&gt;")&lt;br /&gt;      Response.Write(m_strDebugPage.ToString())&lt;br /&gt;      Response.Write("&lt;/body&gt;&lt;/html&gt;")&lt;br /&gt;&lt;br /&gt;      Response.Flush()&lt;br /&gt;      Response.End()&lt;br /&gt;&lt;br /&gt;   End Function&lt;br /&gt;   ' --------------------------------------------------------------------------&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 16 May 2007 20:30:09 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4029</guid>
      <author>Will_Rickards (Will Rickards)</author>
    </item>
  </channel>
</rss>
