Archive

Posts Tagged ‘Crystal Reports’

Crystal Reports CrystalDecisions.CrystalReports.Engine.LogOnException: Logon failed.


Programmatically change the datasource/database on Crystal Reports

Today I got an exception an exception when we moved the reports to the testing environment. The exception was:

Message / Message: Logon failed. Details: [Database Vendor Code: 1005 ] Logon failed. Details: [Database Vendor Code: 1005 ] Error in File MyReportName {5A28B2BB-9269-4B72-B0B9-250D37A7ED62}.rpt: Unable to connect: incorrect log on parameters. Details: [Database Vendor Code: 1005 ]
Source / Source: CrystalDecisions.ReportAppServer.DataSetConversion

The requirement was to use the Pull method to populate the datasource in the report. In other words, connect the report directly to a view in the database.

I was calling the SetDataBaseLogon to pass the credentials and and the datasource to the

report.crReportDocument.SetDatabaseLogon(SqlCon.UserID, _
                          SqlCon.Password, SqlCon.DataSource, "")

With the above code I was still getting the exception. The solution was to set the connection of the DataSourceConnections collection property

'This line is necessary to replace the datasource in the report with the one
'related to the enviroment
If crReportDocument.DataSourceConnections.Count > 0 Then
   crReportDocument.DataSourceConnections(0).SetConnection(SqlCon.DataSource, _
                                             "", _
                                             SqlCon.UserID, _
                                             SqlCon.Password)
End If
'This line sets the credentials for the datasource set in the Datasourceconnections.SetConnection
crReportDocument.SetDatabaseLogon(SqlCon.UserID, SqlCon.Password, SqlCon.DataSource, "")
Advertisements