Crystal report (.rpt) file can be created by standard report creation wizard at that point user have two options. Either can add command or select tables from database. You can also create crystal report file (.rpt) from visual studio 2010 -> add new itme -> crystal report. Now i am changing database name of rpt file at run time.
CrystalReportViewer crystalreportviewer = new CrystalReportViewer();
ReportDocument rd = new ReportDocument();
rd.Load(filepath);//filepath for rpt file c://command.rpt
crystalreportviewer.ReportSource = rd; // set the source as reportdocument
foreach (CrystalDecisions.CrystalReports.Engine.Table tb in rd.Database.Tables)
{
TableLogOnInfo tbloginfo = new TableLogOnInfo();
CrystalDecisions.Shared.ConnectionInfo ci = new ConnectionInfo();
ci.DatabaseName = databasename; ***// Changing at run time.*** User can change databasename
ci.ServerName = "Myservername";
ci.UserID = "userid";
ci.Password = "userpassword";
tbloginfo.ConnectionInfo = ci;
tb.ApplyLogOnInfo(tbloginfo);
try
{
tb.Location = ci.DatabaseName + ".dbo." + tb.Location.Substring(tb.Location.LastIndexOf(".") + 1);
}
catch
{
}
}
Code does not work when rpt file used command. While debugging , rd(reportdocument).DataBase.Tables[0].Name = "Command" if rpt file has manually selected tables rd(reportdocument).DataBase.Tables[0].Name = "EmployeeTable" (Name of the table)
code wont change the connectionInfo of the table when it finds command.
Please ask if i dint make my self clear.
Thanks, N Avatar