Symptom
- How to change the ‘Microsoft Access (.mdb)’ datasource of a Crystal report at runtime?
- Report connects to an mdb file using Access / DAO connection.
Environment
- SAP Crystal Reports developer version for Visual Studio
- Visual Studio 2010 / 2012 / 2013
Resolution
- Here is the sample Crystal Reports .NET SDK (VB) code to change the mdb file datasource of a report.
- The report was designed off efashion.mdb and below code changes it to efashion1.mdb
VB.NET code - InProc RAS SDK
Imports CrystalDecisions.ReportAppServer.ClientDoc
Imports CrystalDecisions.ReportAppServer.DataDefModel
Imports CrystalDecisions.ReportAppServer.ReportDefModel
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Web
Imports CrystalDecisions.Shared
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim ClientDoc As ISCDClientDocument
Dim logonPb As New CrystalDecisions.ReportAppServer.DataDefModel.PropertyBag()
Dim table_Old As New CrystalDecisions.ReportAppServer.DataDefModel.Table()
Dim table_New As New CrystalDecisions.ReportAppServer.DataDefModel.Table()
'Open the report object to initialize the ReportClientDocument
Dim rd As New CrystalDecisions.CrystalReports.Engine.ReportDocument()
rd.Load("C:\Users\Administrator\Desktop\Report1.rpt")
ClientDoc = rd.ReportClientDocument()
table_old = ClientDoc.Database.Tables(0)
'Clone the original table in the report
table_new = table_old.Clone(True)
' Get the connection information from the report document for the first table in the collection.
logonPb = table_new.ConnectionInfo.Attributes("QE_LogonProperties")
logonPb.Item("Database Name") = "C:\Users\Administrator\Desktop\New folder\efashion1.mdb"
' Set a new table for the report, set the old table to the new one.
ClientDoc.DatabaseController.SetTableLocation(table_old, table_new)
'view the report
CrystalReportViewer1.ReportSource = rd
End SubEnd Class
VB.NET code - Crystal Reports SDK
ReportDocument.Database.Table(0).Location = "C:\Users\Administrator\Desktop\New folder\efashion1.mdb"
Keywords
KBA , BI-DEV-NET-SDK , .NET SDK / COM SDK , How To