Reputation: 1205
I am new with crystal reports. I tried to to implement the crystal report in my win form c# application using report wizard visual studio 2012, so don't know what happen's in backhand for this. Everything works good on my computer but when i tried install this on another computer connection string changes and gives error.
I tried many links like Dynamic Connection string Change but as i am using report wizard for setup so don't know where to use this.
I also tried all options in report wizard for connection string but didn't find anything that change connection string at run time.
Is there any options by which i can attach connection String from app config at run time
.
Upvotes: 5
Views: 21593
Reputation: 536
Here's an example of changing all the main report tables as well as all subreports tables, to a newly specified TargetServer and TargetDatabase with Integrated Authentication:
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
// using CrystalDecisions.ReportAppServer.CommLayer; // not used directly, but this is needed in Project References.
//
// be sure to set "copy local" = true in the Project:
// see https://stackoverflow.com/questions/38025601/could-not-load-file-or-assembly-crystaldecisions-reportappserver-commlayer-ver
static ReportDocument crReportDocument;
static ConnectionInfo crConnectionInfo = new ConnectionInfo();
static public string TargetServer { get; set; }
static public string TargetDatabase { get; set; }
static void crAssignConnectionInfo()
{
crConnectionInfo.UserID = "";
crConnectionInfo.Password = "";
crConnectionInfo.DatabaseName = TargetDatabase;
crConnectionInfo.ServerName = TargetServer;
crConnectionInfo.IntegratedSecurity = true; // in case the report was saved with SQL authentication, switch to Integrated
}
static void SetSubreportLoginInfo(CrystalDecisions.CrystalReports.Engine.Sections objSections)
{
foreach (Section section in objSections)
{
foreach (ReportObject reportObject in section.ReportObjects)
{
SubreportObject crSubreportObject;
switch (reportObject.Kind)
{
case ReportObjectKind.SubreportObject:
crSubreportObject = (SubreportObject)reportObject;
ReportDocument subRepDoc = crSubreportObject.OpenSubreport(crSubreportObject.SubreportName);
if (subRepDoc.ReportDefinition.Sections.Count > 0) {
SetSubreportLoginInfo(subRepDoc.ReportDefinition.Sections);
}
Tables crTables = subRepDoc.Database.Tables;
foreach (Table table in crTables)
{
TableLogOnInfo tableLogOnInfo = new TableLogOnInfo();
tableLogOnInfo.ConnectionInfo.UserID = crConnectionInfo.UserID;
tableLogOnInfo.ConnectionInfo.Password = crConnectionInfo.Password;
tableLogOnInfo.ConnectionInfo.DatabaseName = crConnectionInfo.DatabaseName;
tableLogOnInfo.ConnectionInfo.ServerName = crConnectionInfo.ServerName;
tableLogOnInfo.ConnectionInfo.IntegratedSecurity = crConnectionInfo.IntegratedSecurity;
table.ApplyLogOnInfo(tableLogOnInfo);
}
break;
case ReportObjectKind.FieldObject:
case ReportObjectKind.TextObject:
case ReportObjectKind.LineObject:
case ReportObjectKind.BoxObject:
case ReportObjectKind.PictureObject:
case ReportObjectKind.ChartObject:
case ReportObjectKind.CrossTabObject:
case ReportObjectKind.BlobFieldObject:
case ReportObjectKind.MapObject:
case ReportObjectKind.OlapGridObject:
case ReportObjectKind.FieldHeadingObject:
case ReportObjectKind.FlashObject:
default:
// none of the other objects need to have login assigned
break;
}
}
}
}
static void SetCrystalDocumentLogon()
{
crAssignConnectionInfo();
TableLogOnInfo crTableLogonInfo = new TableLogOnInfo();
foreach (Table crTable in crReportDocument.Database.Tables)
{
try
{
crConnectionInfo.Type = crTable.LogOnInfo.ConnectionInfo.Type;
crTableLogonInfo.ConnectionInfo = crConnectionInfo;
crTableLogonInfo.ReportName = crTable.LogOnInfo.ReportName;
crTableLogonInfo.TableName = crTable.LogOnInfo.TableName;
crTable.ApplyLogOnInfo(crTableLogonInfo);
}
catch (Exception ex)
{
Console.WriteLine("Error during SetCrystalDocumentLogon " + ex.Message);
throw;
}
SetSubreportLoginInfo(crReportDocument.ReportDefinition.Sections);
}
}
Upvotes: 1
Reputation: 31
strServer= ConfigurationManager.AppSettings["ServerName"].ToString();
strDatabase= ConfigurationManager.AppSettings["DataBaseName"].ToString();
strUserID= ConfigurationManager.AppSettings["UserId"].ToString();
strPwd= ConfigurationManager.AppSettings["Password"].ToString();
//may be you need to set the integrated security to false, first.
report.DataSourceConnections[o].IntegratedSecurity = False;
report.DataSourceConnections[0].SetConnection(strServer, strDatabase, strUserID, strPwd);
Upvotes: 3
Reputation: 3416
Try something like this:
strServer= ConfigurationManager.AppSettings["ServerName"].ToString();
strDatabase= ConfigurationManager.AppSettings["DataBaseName"].ToString();
strUserID= ConfigurationManager.AppSettings["UserId"].ToString();
strPwd= ConfigurationManager.AppSettings["Password"].ToString();
report.DataSourceConnections[0].SetConnection(strServer, strDatabase, strUserID, strPwd);
Upvotes: 7