Click to See Complete Forum and Search --> : Crystal Report DB connection from code


Salex
December 6th, 2004, 05:10 PM
Dear colleagues,
I’m writing C# app working with access 200 db. It is supposed to use DSNless ODBC connection string to avoid setting ODBC connection in control panel on every client PC. I have to use Crystal Report as my reporting tool. My problem is to connect reports to db from the code. When the next code is executed, it brings me Database Login screen and says “LogOn failed” every time I click Ok.

Code:

ReportDocument ReportDoc = new ReportDocument();
ReportDoc.Load(“c:\report.rpt”);
try
{
TableLogOnInfo logOnInfo = new TableLogOnInfo ();
int i = 0;
// Loop through every table in the report.
for (i=0;i < ReportDoc.Database.Tables.Count;i++)
{
logOnInfo.ConnectionInfo.ServerName = "";
logOnInfo.ConnectionInfo.UserID = "admin";
logOnInfo.ConnectionInfo.Password = "";
logOnInfo.ConnectionInfo.DatabaseName = “c:\database.mdb”;
ReportDoc.Database.Tables [i].ApplyLogOnInfo (logOnInfo);
}
return true;
}
catch (Exception ex)
{
return false;
}
crystalReportViewer1.ReportSource = ReportDoc;


Thanks in advance

springsoft
December 6th, 2004, 11:22 PM
Do you have any subreports??
If you do, you need to iterate through the .Subreports collection of the report object to ensure that logon info is passed to them also. It's one of the commonest reasons reports don't run when invoked from an application - authorisation needs to occur within every object establishing a connection to the database.

Dave