Answered by:
Log on failed [RESOLVED]

Question
-
Hello everybody,
I am using following code to show a report designed with CR 10. I am getting logon failed exception however the sql server login parameters are correct and I can connect sql server with these parameters and even report in design mode.
CrystalDecisions.CrystalReports.Engine.ReportDocument reportDoc =
new CrystalDecisions.CrystalReports.Engine.ReportDocument();reportDoc.FileName = Server.MapPath("Reports/CustomerList.rpt");
//reportDoc.Refresh();string serverName = GetServerName(); string databaseName = GetDatabaseName(); string userName = GetUserName(); string password = GetPassword();
reportDoc.SetDatabaseLogon(userName,password,serverName,databaseName,
true);rptCustomerList.ReportSource = reportDoc;
Thanks.
Monday, February 19, 2007 2:01 PM
Answers
-
Here's some boilerplate code that works reliably for me. Just call LogonDB and pass it your connection info.
public void LogonDB(String server, String db, String user, String pwd)
{
// provide connection info for the logon
ConnectionInfo connectionInfo = new ConnectionInfo();
connectionInfo.ServerName = server;
connectionInfo.DatabaseName = db;
connectionInfo.UserID = user;
connectionInfo.Password = pwd;
try
{
Logon(this.reportDocument1, connectionInfo);
}
catch (Exception e)
{
MessageBox.Show("Error: \r\n" + e.Message);
}
}
// KBase Article ID:c2011464
// How do you change the database and server in a report at runtime?
// When using ODBC, the DSN name is the server name. The DSN should be under the
// System DSN and not under the User DSN.
// If you have a subreport that connects to a different database or server than
// your main report, use the 'ApplyLogon' method instead of the 'logon' method.
// When using the ApplyLogon method, ensure you iterate through each subreport.
// It is not possible to use the CR for VS .NET SDK to change a report from using
// SQL Authentication to Windows Authentication, or vice versa. This functionality
// is available through the Report Application Server (RAS) .NET SDK or Report Designer
// Component (RDC).
// The Logon method iterates through all tables
public bool Logon(ReportDocument cr, ConnectionInfo ci)
{
SubreportObject subObj;
if (!ApplyLogon(cr, ci))
return (false);
try
{
foreach (ReportObject obj in cr.ReportDefinition.ReportObjects)
{
if (obj.Kind == ReportObjectKind.SubreportObject)
{
subObj = (SubreportObject)obj;
if (!ApplyLogon(cr.OpenSubreport(subObj.SubreportName), ci))
return (false);
}
}
}
catch (Exception e)
{
MessageBox.Show("Error: \r\n" + e.Message);
}
return (true);
}
private bool ApplyLogon(ReportDocument cr, ConnectionInfo ci)
{
TableLogOnInfo li;
Tables tables;
// for each table apply connection info
try
{
tables = cr.Database.Tables;
// for each table apply connection info
foreach (Table tbl in tables)
{
log.MakeLogEntry("In the foreach block of ApplyLogon");
li = tbl.LogOnInfo;
li.ConnectionInfo = ci;
tbl.ApplyLogOnInfo(li);
// check if logon was successful; if TestConnectivity returns false, check
// logon credentials
if (tbl.TestConnectivity())
{
// drop fully qualified table location
if (tbl.Location.IndexOf(".") > 0)
{
tbl.Location = tbl.Location.Substring(tbl.Location.LastIndexOf(".") + 1);
}
else tbl.Location = tbl.Location;
}
else return (false);
}
}
catch (Exception e)
{
MessageBox.Show("Error: \r\n" + e.Message);
}
return (true);
}Wednesday, February 21, 2007 9:06 PM
All replies
-
Here's some boilerplate code that works reliably for me. Just call LogonDB and pass it your connection info.
public void LogonDB(String server, String db, String user, String pwd)
{
// provide connection info for the logon
ConnectionInfo connectionInfo = new ConnectionInfo();
connectionInfo.ServerName = server;
connectionInfo.DatabaseName = db;
connectionInfo.UserID = user;
connectionInfo.Password = pwd;
try
{
Logon(this.reportDocument1, connectionInfo);
}
catch (Exception e)
{
MessageBox.Show("Error: \r\n" + e.Message);
}
}
// KBase Article ID:c2011464
// How do you change the database and server in a report at runtime?
// When using ODBC, the DSN name is the server name. The DSN should be under the
// System DSN and not under the User DSN.
// If you have a subreport that connects to a different database or server than
// your main report, use the 'ApplyLogon' method instead of the 'logon' method.
// When using the ApplyLogon method, ensure you iterate through each subreport.
// It is not possible to use the CR for VS .NET SDK to change a report from using
// SQL Authentication to Windows Authentication, or vice versa. This functionality
// is available through the Report Application Server (RAS) .NET SDK or Report Designer
// Component (RDC).
// The Logon method iterates through all tables
public bool Logon(ReportDocument cr, ConnectionInfo ci)
{
SubreportObject subObj;
if (!ApplyLogon(cr, ci))
return (false);
try
{
foreach (ReportObject obj in cr.ReportDefinition.ReportObjects)
{
if (obj.Kind == ReportObjectKind.SubreportObject)
{
subObj = (SubreportObject)obj;
if (!ApplyLogon(cr.OpenSubreport(subObj.SubreportName), ci))
return (false);
}
}
}
catch (Exception e)
{
MessageBox.Show("Error: \r\n" + e.Message);
}
return (true);
}
private bool ApplyLogon(ReportDocument cr, ConnectionInfo ci)
{
TableLogOnInfo li;
Tables tables;
// for each table apply connection info
try
{
tables = cr.Database.Tables;
// for each table apply connection info
foreach (Table tbl in tables)
{
log.MakeLogEntry("In the foreach block of ApplyLogon");
li = tbl.LogOnInfo;
li.ConnectionInfo = ci;
tbl.ApplyLogOnInfo(li);
// check if logon was successful; if TestConnectivity returns false, check
// logon credentials
if (tbl.TestConnectivity())
{
// drop fully qualified table location
if (tbl.Location.IndexOf(".") > 0)
{
tbl.Location = tbl.Location.Substring(tbl.Location.LastIndexOf(".") + 1);
}
else tbl.Location = tbl.Location;
}
else return (false);
}
}
catch (Exception e)
{
MessageBox.Show("Error: \r\n" + e.Message);
}
return (true);
}Wednesday, February 21, 2007 9:06 PM -
I found the problem was on my side and I didn't notice the report was connected to the database via DSN and the DSN rejected the login info. When I gave the login info and server name of DSN, it worked well.
Thanks a lot.Thursday, February 22, 2007 8:52 AM