Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Change the data source of a Crystal Reports report at run-time using C#

, 19 Oct 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
This is a common issue that development teams face when reports are being deployed to the production environment.

It’s a known fact that in Crystal Reports, if you try to display details, using data in a SQL Server database other than the one that you’ve used to design the report, either you have to set the database location or refresh the report. This is a common issue that development teams face when reports are being deployed to the production environment.

But this can be prevented using the following method.

The following namespaces are required:

using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

And we have to assign the required SQL Server information to each table/view of the report, sub reports, and the report viewer.

Use the following code:

SQLReport report = new SQLReport();
//Get SQL Server Details
string zServer = @"SERVER_NAME";
string zDatabase = @"DATABASE";
string zUsername = @"USER";
string zPassword = @"PASSWORD";

ConnectionInfo ciReportConnection = new ConnectionInfo();

ciReportConnection.ServerName = zServer;
ciReportConnection.DatabaseName = zDatabase;
ciReportConnection.UserID = zUsername;
ciReportConnection.Password = zPassword;

//Assign data source details to tables

foreach (Table table in report.Database.Tables) {
     table.LogOnInfo.ConnectionInfo = ciReportConnection;

foreach (ReportDocument subrep in report.Subreports) {
     foreach (Table table in subrep.Database.Tables) {
         table.LogOnInfo.ConnectionInfo = ciReportConnection;

//Assign data source details to the report viewer
if (this.crystalReportViewer1.LogOnInfo != null) {
     TableLogOnInfos tlInfo = this.crystalReportViewer1.LogOnInfo;
     foreach (TableLogOnInfo tbloginfo in tlInfo) {
         tbloginfo.ConnectionInfo = ciReportConnection;
crystalReportViewer1.ReportSource = report;


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


About the Author

Manjuke Fernando
Technical Lead Air Liquide Industrial Services (Singapore)
Singapore Singapore
I have been in software industry for more than 8 years. I have developed different type of software using different languages. Many of them are database related (both web & window based), SQL being as the back end most of the time. Up-to-date I have knowledge in languages such as C#, VB.Net, T-SQL, JAVA, VB6 & C++, making C# the most proficient of all. Also I have worked using different technologies like ASP.Net, SharePoint, Crystal Reports (But I really hate designing reports) & MS SQL Server and have involved in designing & developing software for major companies like FedEx, Softlogic Holdings, IronOne Technologies & Brandix. Currently I am working as a Tech Lead in Singapore.
Follow on   Twitter   LinkedIn

Comments and Discussions

QuestionDoesn't work for Commands PinmemberAllan Kaliel9-Jul-14 7:57 
Questionwhat about table changes? PinmemberLAXATECH1-Oct-13 20:39 
AnswerRe: what about table changes? PinmemberManjuke Fernando3-Oct-13 2:21 
QuestionError Pinmemberaju1844-Apr-12 19:40 
AnswerRe: Error PinmemberManjuke Fernando4-Apr-12 20:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150331.1 | Last Updated 19 Oct 2011
Article Copyright 2011 by Manjuke Fernando
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid