djoshi
djoshi

Reputation: 363

Crystal report datsource Set by ConnectionInfo is not getting set- Does not connect to right database

I have 8 reports that use the exact same code to set the reporting datasource out of which 5 reports work and are able to point to Production environment. The rest of the 3 reports, I have ran and rerun database verify and database update yet when I run these reports on production, they bring data back from DEV environment.

Here is how my datasource is getting set. I call stored procedure in all 8 crystal reports . I have done very detailed debugging and verified that the datasource data is getting the correct information so what is missing.

            string database = ConfigurationManager.AppSettings[env + "Database"].ToString();
            string server = ConfigurationManager.AppSettings[env + "Server"].ToString();


            CrystalReportViewer1.ParameterFieldInfo = fields;
            rptDoc.Load(Server.MapPath(report));


            ConnectionInfo connectionInfo = Reports.GetConnectionInfo(server, database, "userID", "password");


            //connectionInfo.Attributes = attributes;
            connectionInfo.Type = ConnectionInfoType.SQL;
            SetDBLogonForReport(connectionInfo, env);
            CrystalReportViewer1.ReportSource = rptDoc;



 private void SetDBLogonForReport(ConnectionInfo oConnectionInfo, string env)
    {
        try
        {
            TableLogOnInfos oTableLogOnInfos = CrystalReportViewer1.LogOnInfo;
            string[] sparams = new string[]{
            };

            foreach (CrystalDecisions.CrystalReports.Engine.Table oTable in rptDoc.Database.Tables)
            {
                if (oTable.LogOnInfo.ConnectionInfo.ServerName == oConnectionInfo.ServerName)
                {
                    TableLogOnInfo oTableLogOnInfo = oTable.LogOnInfo;

                    oTableLogOnInfo.ConnectionInfo = oConnectionInfo;

                    oTable.ApplyLogOnInfo(oTableLogOnInfo);

                    // oTable.Location = String.Format( "{0}.dbo.{1}", oConnectionInfo.DatabaseName, oTable.Name );

                    bool b = oTable.TestConnectivity();

                    if (!b)
                    {
                        invokeErrorLogger(sparams, env);
                    }
                }
            }

        }
        catch
        {
            throw;
        }
    }

Upvotes: 0

Views: 3096

Answers (1)

Justin
Justin

Reputation: 2103

The reports that work are already pointing to your prod server. You are only applying the logon credentials if the server stored in the report matches the server name you are trying to set. Change to the following:

// Clear existing connection info first
rptDoc.DataSourceConnections.Clear();

foreach (CrystalDecisions.CrystalReports.Engine.Table oTable in rptDoc.Database.Tables)
{
  var oTableLogonInfo = oTable.LogonInfo;
  oTableLogonInfo.ConnectionInfo = oConnectionInfo;
  oTable.ApplyLogOnInfo(oTableLogonInfo);
  bool b = oTable.TestConnectivity();
  if (!b)
  {
      invokeErrorLogger(sparams, env);
  }
}  

Upvotes: 1

Related Questions