Hi,
I'm trying to use a crystal report with .NET 2005 with C# but experiencing some problems
when trying to connect to the database to pull the data for the report. The code below
shows how I implemented the connection procedures. The problem is the code is unbale to
change which database the report connects to and defaults to the connection that was used
to design the report. The ApplyLogonToReport method does not seem to work. Can anyone help
me with the problem? The report calls a stored procedure to get the data.
private void cmdRun_Click(object sender, EventArgs e)
{
try
{
SqlString user; SqlInt32 loccode;
report = new RptTransactionDetailsCpy()
;
SqlConnectionStringBuilder
constr = new SqlConnectionStringBuilder
(DbClient.
Configurat
ion [DbClient.ConnectionString
] as string);
report.SetDatabaseLogon(co
nstr.UserI
D, constr.Password, constr.DataSource, constr.InitialCatalog);
ConnectionInfo info = new ConnectionInfo();
info.AllowCustomConnection
= true;
info.UserID = constr.UserID;
info.Password = constr.Password;
info.ServerName = constr.DataSource;
info.DatabaseName = constr.InitialCatalog;
info.IntegratedSecurity = constr.IntegratedSecurity;
foreach(ReportObject obj in report.ReportDefinition.Re
portObject
s)
{
if(obj.Kind == ReportObjectKind.Subreport
Object)
{
SubreportObject subObj = (SubreportObject)obj;
ReportDocument rd = report.OpenSubreport(subOb
j.Subrepor
tName);
ApplyLogonToReport(rd, constr.DataSource, constr.InitialCatalog, constr.UserID, constr.Password, constr.IntegratedSecurity)
;
}
}
}
catch(Exception ex){}
}
public bool ApplyLogonToReport(ReportD
ocument rd, string server, string database, string userid, string password, bool sspi)
{
int i = 0;
TableLogOnInfo logonInfo = new TableLogOnInfo();
foreach(CrystalDecisions.C
rystalRepo
rts.Engine
.Table table in rd.Database.Tables)
{
logonInfo = table.LogOnInfo;
logonInfo.ConnectionInfo.A
llowCustom
Connection
= true;
logonInfo.ConnectionInfo.S
erverName = server;
logonInfo.ConnectionInfo.D
atabaseNam
e = database;
logonInfo.ConnectionInfo.I
ntegratedS
ecurity = sspi;
if(!sspi)
{
logonInfo.ConnectionInfo.U
serID = userid;
logonInfo.ConnectionInfo.P
assword = password;
}
table.ApplyLogOnInfo(logon
Info);
if(sspi)
{
rd.DataSourceConnections[s
erver, database].SetConnection(se
rver, database, sspi);
}
else
{
rd.DataSourceConnections[s
erver, database].SetConnection(se
rver, database, userid, password);
}
i++;
if(!table.TestConnectivity
())
{
return false;
}
}
return (true);
}