cancel
Showing results for 
Search instead for 
Did you mean: 

ApplyLogOnInfo() reverts back to original connection information

Former Member
0 Kudos

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

Accepted Solutions (1)

Accepted Solutions (1)


        /// <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);
            }
        }
0 Kudos

Fixed formatting and moved to .NET SDK forum

Former Member
0 Kudos

Hi Chris,

this one worked fine for me.

Thank you for your work on that!!!

Regards

HowieD

former_member183750
Active Contributor
0 Kudos

For those following this thread, there is also a utility that will actually write the logon code for you. All you have to do is open the report in the utility and the code will be written out in VB and C#.

KB - 1553921 - Is there a utility that would help in writing database logon code?

And of course, sample apps may be good to see also:

http://wiki.sdn.sap.com/wiki/x/JQBmBQ

http://wiki.sdn.sap.com/wiki/x/IgBmBQ

- Ludek

Follow us on Twitter

Got Enhancement ideas? Try the SAP Idea Place

Share Your Knowledge in SCN Topic Spaces

Former Member
0 Kudos

Hi Ludek,

this is a bit strange.

Since this Error seems to be there for several versions right now I would expect that this will be fixed someday.

But hey, that would be to easy, so lets create a tool to create masses of cluttering workaround code!

Sorry but this is really ridiculous!!

Regards

HowieD

former_member183750
Active Contributor
0 Kudos

Umm - I think if the code is done righ, there is no error / issue?

- Ludek

Answers (0)