Skip to Content
avatar image
Former Member

ApplyLogOnInfo() reverts back to original connection information

This is one of the single toughest APIs that I've ever had to use. Although I have developed with Crystal Reports for about 10 years, I continue to have the simplest of problems. Today it became aparent that our current implementation wasn't working anymore. We'd not too long ago switched to VS2010 and the newest version of Crystal Reports for VS2010.

Anyway, I found that when I'd loop through the tables in a report, even though I'd set the ConnectionInfo correctly, upon calling ApplyLogOnInfo(), the database and server would switch right back to what they were set as in the designed RPT file! How's that for frustrating?

Well, after a day's worth of troubleshooting it also became evident that I needed to do a lot of the property setting myself, as the API calls don't seem to do it themselves. So, anyone who's interested in code that seems to work for me, here you go.

In my example I refer to a "Report" object which is just a class with string variables for connection parameters and a boolean swtich for integrated versus non-integrated security.

Edited by: Don Williams on Feb 11, 2011 2:56 PM

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • Feb 11, 2011 at 10:57 PM
            /// <summary>
            /// Defines the datasource connectivity information for the report
            /// </summary>
            /// <param name="document">Crystal Report ReportDocument</param>
            /// <param name="report">Report object</param>
            private void ApplyLoginInfo(ReportDocument document, Report report)
            {
                TableLogOnInfo info = null;
    
                try
                {
                    #region Credentials
    
                    //
                    // Define credentials
                    //
                    info = new TableLogOnInfo();
    
                    info.ConnectionInfo.AllowCustomConnection = true;                
                    info.ConnectionInfo.ServerName = report.Server;
                    info.ConnectionInfo.DatabaseName = report.Database;                
    
                    //
                    // Set the userid/password for the report if we are not using integrated security
                    //
                    if (report.IntegratedSecurity)
                    {
                        info.ConnectionInfo.IntegratedSecurity = true;
                    }
                    else
                    {
                        info.ConnectionInfo.Password = report.Password;
                        info.ConnectionInfo.UserID = report.UserId;
                    }
    
                    #endregion
    
                    #region Apply to connections, tables and sub-reports
    
                    //
                    // Main connection?
                    //
                    document.SetDatabaseLogon(info.ConnectionInfo.UserID,
                        info.ConnectionInfo.Password,
                        info.ConnectionInfo.ServerName,
                        info.ConnectionInfo.DatabaseName,
                        false);
    
                    //
                    // Other connections?
                    //
                    foreach (CrystalDecisions.Shared.IConnectionInfo connection in document.DataSourceConnections)
                    {                    
                        connection.SetConnection(report.Server, report.Database, report.IntegratedSecurity);
                        connection.SetLogon(report.UserId, report.Password);
                        connection.LogonProperties.Set("Data Source", report.Server);
                        connection.LogonProperties.Set("Initial Catalog", report.Database);
                    }
    
                    //
                    // Only do this to the main report (can't do it to sub reports)
                    //
                    if (!document.IsSubreport)
                    {
                        //
                        // Apply to subreports
                        //                
                        foreach (ReportDocument rd in document.Subreports)
                        {
                            ApplyLoginInfo(rd, report);                        
                        }
                    }
    
                    //
                    // Apply to tables
                    //
                    foreach (CrystalDecisions.CrystalReports.Engine.Table table in document.Database.Tables)
                    {
                        TableLogOnInfo tableLogOnInfo = table.LogOnInfo;
                                            
                        tableLogOnInfo.ConnectionInfo = info.ConnectionInfo;                    
                        table.ApplyLogOnInfo(tableLogOnInfo);
                        if (!table.TestConnectivity())
                        {
                            Debug.WriteLine("Failed to apply log in info for Crystal Report");
                        }
                    }
    
                    #endregion
    
                    try
                    {
                        //
                        // Break it all down
                        //
                        document.VerifyDatabase();
                    }
                    catch (LogOnException excLogon)
                    {
                        Debug.WriteLine(excLogon.Message);
                    }
                }
                catch (Exception ex)
                {
                    throw new ApplicationException("Failed to apply login information to the report - " +
                        ex.Message);
                }
            }
    
    Add comment
    10|10000 characters needed characters exceeded