cancel
Showing results for 
Search instead for 
Did you mean: 

ReplaceConnection doesn't change the current connection to a new one

Former Member
0 Kudos

Is there anything wrong with this code?  I was trying to change the database logon dynamically in my code but unfortunately it won't do it.  It is still pointing the the saved connection in the report.

    protected void Page_Load(object sender, EventArgs e)

    {

            CrystalDecisions.Enterprise.SessionMgr boSessionMgr;

            CrystalDecisions.Enterprise.InfoStore boInfoStore;

            CrystalDecisions.Enterprise.EnterpriseService boEnterpriseService;

            CrystalDecisions.Enterprise.InfoObjects boInfoObjects;

            CrystalDecisions.ReportAppServer.ClientDoc.ReportAppFactory boReportAppFactory;

         

        string boQuery;

            boReportName = (Request["ReportName"] is object ? Request["ReportName"] : "");

            if (Session["boEnterpriseSession"] != null)

            {

                boEnterpriseSession = (CrystalDecisions.Enterprise.EnterpriseSession)Session["boEnterpriseSession"];

            }

            else

            {

                //Log on to the Enterprise CMS

                boSessionMgr = new CrystalDecisions.Enterprise.SessionMgr();

                boEnterpriseSession = boSessionMgr.Logon(cmsUserID, cmsPwd, cmsServer, cmsAuth);

                Session.Add("boEnterpriseSession", boEnterpriseSession);

            }

            //get report object from session or create a new report object

            if (Session["boReportClientDocument"] != null)

            {

                boReportClientDocument = (CrystalDecisions.ReportAppServer.ClientDoc.ReportClientDocument)Session["boReportClientDocument"];

            }

            else

            {

                boEnterpriseService = boEnterpriseSession.GetService("", "InfoStore");

                boInfoStore = new CrystalDecisions.Enterprise.InfoStore(boEnterpriseService);

                //Retrieve the report object from the InfoStore, only need the SI_ID for RAS

                boQuery = "Select SI_ID From CI_INFOOBJECTS Where SI_NAME = '" + boReportName + "' AND SI_Instance=0"; //ADD THIS IF REPORT NAME IS NOT UNIQUE-- AND SI_PARENTID=9354

                boInfoObjects = boInfoStore.Query(boQuery);

                if (boInfoObjects.ResultCount > 0)

                {

                    boInfoObject = boInfoObjects[1];

                }

                else {

                    Response.Write("Report (" + boReportName + ") cannot be found.");

                    Response.End();

                }

                boEnterpriseService = null;

                //Retrieve RASReportFactory

                boEnterpriseService = boEnterpriseSession.GetService("RASReportFactory");

                boReportAppFactory = (CrystalDecisions.ReportAppServer.ClientDoc.ReportAppFactory)boEnterpriseService.Interface;

                //Open the report from Enterprise

                boReportClientDocument = boReportAppFactory.OpenDocument(boInfoObject.ID, 0);

            }

            //Database Log on

            DatabaseLogon(dbServerName, dbDatabaseName, dbUserID, dbPwd, boReportClientDocument);

            //Set Current Document Session

            Session.Add("boReportClientDocument", boReportClientDocument);

            boCrystalReportViewer.ReportSource = Session["boReportClientDocument"];

          

    }

    private void DatabaseLogon(string sServerName, string sDBName, string sUserID, string sPwd, ISCDReportClientDocument rcd)

    {

        //rcd.DatabaseController.LogonEx(sServerName , sDBName , sUserID , sPwd );

        //Create the logon propertybag for the connection we wish to use

        CrystalDecisions.ReportAppServer.DataDefModel.PropertyBag logonDetails = new CrystalDecisions.ReportAppServer.DataDefModel.PropertyBag();

        logonDetails.Add("Auto Translate", -1);

        logonDetails.Add("Connect Timeout", 15);

        logonDetails.Add("Data Source", sServerName );

        logonDetails.Add("General Timeout", 0);

        logonDetails.Add("Initial Catalog", sDBName );

        logonDetails.Add("Integrated Security", "False");

        logonDetails.Add("Locale Identifier", 1033);

        logonDetails.Add("OLE DB Services", -5);

        logonDetails.Add("Provider", "SQLOLEDB");

        logonDetails.Add("Use Encryption for Data", 0);

        logonDetails.Add("Owner", "dbo"); // schema

        //Create the QE (query engine) propertybag with the provider details and logon property bag.

        CrystalDecisions.ReportAppServer.DataDefModel.PropertyBag QE_Details = new CrystalDecisions.ReportAppServer.DataDefModel.PropertyBag();

        QE_Details.Add("Database DLL", "crdb_ado.dll");

        QE_Details.Add("QE_DatabaseName", sDBName );

        QE_Details.Add("QE_DatabaseType", "OLE DB (ADO)");

        QE_Details.Add("QE_LogonProperties", logonDetails);

        QE_Details.Add("QE_ServerDescription", sServerName );

        QE_Details.Add("QE_SQLDB", "True");

        QE_Details.Add("SSO Enabled", "False");

        QE_Details.Add("Owner", "dbo");

        CrystalDecisions.ReportAppServer.DataDefModel.ConnectionInfo newConnInfo = new CrystalDecisions.ReportAppServer.DataDefModel.ConnectionInfo();

        CrystalDecisions.ReportAppServer.DataDefModel.ConnectionInfo oldConnInfo;

        CrystalDecisions.ReportAppServer.DataDefModel.ConnectionInfos oldConnInfos;

        oldConnInfos = rcd.DatabaseController.GetConnectionInfos(null);

        for (int I = 0; I < oldConnInfos.Count; I++)

        {

            oldConnInfo = oldConnInfos[I];

            newConnInfo.Attributes = QE_Details;

            newConnInfo.Kind = CrystalDecisions.ReportAppServer.DataDefModel.CrConnectionInfoKindEnum.crConnectionInfoKindCRQE;

            newConnInfo.UserName = sUserID;

            newConnInfo.Password = sPwd;

            try

            {

                rcd.DatabaseController.ReplaceConnection(oldConnInfo, newConnInfo, null, CrystalDecisions.ReportAppServer.DataDefModel.CrDBOptionsEnum.crDBOptionDoNotVerifyDB);

            }

            catch (Exception ex)

            {

                Response.Write("ERROR: " + ex.Message);

                //return;

            }

        }

    }

Accepted Solutions (0)

Answers (1)

Answers (1)

0 Kudos

Hi Betty,

What DB are you connecting to and what version?

If you are using MS SQL Server 2008 or above do not use the OLEDB Provider in MDAC, you should change it to MS SQL Native 10 or 11. Requires the Client to be installed on the BOE Server also.

logonDetails.Add("Provider", "SQLOLEDB"); should be

logonDetails.Add("Provider", "SQLNCLI10"); // or 11

MS does not fully support MDAC OLE DB provider with newer versions of their DB Server.

Try setting the logon info with the actual info rather than using variables, sometimes they are not what the should be.

Also, if you are using any un-used data connections delete them first.

Don