How to dynamically change crystal report database connection
Asked Answered
S

3

5

I am new with crystal reports. I tried to to implement the crystal report in my win form c# application using report wizard visual studio 2012, so don't know what happen's in backhand for this. Everything works good on my computer but when i tried install this on another computer connection string changes and gives error.

I tried many links like Dynamic Connection string Change but as i am using report wizard for setup so don't know where to use this.

I also tried all options in report wizard for connection string but didn't find anything that change connection string at run time.

Is there any options by which i can attach connection String from app config at run time.

Sir answered 5/5, 2014 at 10:39 Comment(0)
C
7

Try something like this:

strServer= ConfigurationManager.AppSettings["ServerName"].ToString();
strDatabase= ConfigurationManager.AppSettings["DataBaseName"].ToString();
strUserID= ConfigurationManager.AppSettings["UserId"].ToString();
strPwd= ConfigurationManager.AppSettings["Password"].ToString();

report.DataSourceConnections[0].SetConnection(strServer, strDatabase, strUserID, strPwd);
Cushion answered 5/5, 2014 at 11:44 Comment(4)
Just a note that I had to specify strServer as the name of my odbc DNS connection.Fondue
@Fondue you mean instead of the index 0 at last line report.DataSourceConnections[0].SetConnection(strServer, strDatabase, strUserID, strPwd); ? because I am getting error that says Invalid report file pathFlor
@Flor it’s been a long time, so I don’t really recall, but I believe I meant exactly what I said. I had to use the ODBC DNS connection name for the value of strServer.Fondue
I am getting error Invalid report file path any idea can help please ?Flor
C
3
strServer= ConfigurationManager.AppSettings["ServerName"].ToString();
strDatabase= ConfigurationManager.AppSettings["DataBaseName"].ToString();
strUserID= ConfigurationManager.AppSettings["UserId"].ToString();
strPwd= ConfigurationManager.AppSettings["Password"].ToString();

//may be you need to set the integrated security to false, first.
report.DataSourceConnections[o].IntegratedSecurity = False;

report.DataSourceConnections[0].SetConnection(strServer, strDatabase, strUserID, strPwd);
Cryptic answered 20/10, 2016 at 8:2 Comment(0)
D
0

Here's an example of changing all the main report tables as well as all subreports tables, to a newly specified TargetServer and TargetDatabase with Integrated Authentication:

using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

// using CrystalDecisions.ReportAppServer.CommLayer; // not used directly, but this is needed in Project References.
//
// be sure to set "copy local" = true in the Project:
// see https://mcmap.net/q/443085/-could-not-load-file-or-assembly-39-crystaldecisions-reportappserver-commlayer-version-13-0-2000-0


        static ReportDocument crReportDocument;
        static ConnectionInfo crConnectionInfo = new ConnectionInfo();
        static public string TargetServer { get; set; }
        static public string TargetDatabase { get; set; }

        static void crAssignConnectionInfo()
        {
            crConnectionInfo.UserID = "";
            crConnectionInfo.Password = "";
            crConnectionInfo.DatabaseName = TargetDatabase;
            crConnectionInfo.ServerName = TargetServer;
            crConnectionInfo.IntegratedSecurity = true; // in case the report was saved with SQL authentication, switch to Integrated
        }

        static void SetSubreportLoginInfo(CrystalDecisions.CrystalReports.Engine.Sections objSections)
        {
            foreach (Section section in objSections)
            {
                foreach (ReportObject reportObject in section.ReportObjects)
                {
                    SubreportObject crSubreportObject;
                    switch (reportObject.Kind)
                    {
                        case ReportObjectKind.SubreportObject:
                            crSubreportObject = (SubreportObject)reportObject;
                            ReportDocument subRepDoc = crSubreportObject.OpenSubreport(crSubreportObject.SubreportName);
                            if (subRepDoc.ReportDefinition.Sections.Count > 0) {
                                SetSubreportLoginInfo(subRepDoc.ReportDefinition.Sections);
                            }
                            Tables crTables = subRepDoc.Database.Tables;
                            foreach (Table table in crTables)
                            {
                                TableLogOnInfo tableLogOnInfo = new TableLogOnInfo();
                                tableLogOnInfo.ConnectionInfo.UserID = crConnectionInfo.UserID;
                                tableLogOnInfo.ConnectionInfo.Password = crConnectionInfo.Password;
                                tableLogOnInfo.ConnectionInfo.DatabaseName = crConnectionInfo.DatabaseName;
                                tableLogOnInfo.ConnectionInfo.ServerName = crConnectionInfo.ServerName;
                                tableLogOnInfo.ConnectionInfo.IntegratedSecurity = crConnectionInfo.IntegratedSecurity;

                                table.ApplyLogOnInfo(tableLogOnInfo);
                            }
                            break;
                        case ReportObjectKind.FieldObject:
                        case ReportObjectKind.TextObject:
                        case ReportObjectKind.LineObject:
                        case ReportObjectKind.BoxObject:
                        case ReportObjectKind.PictureObject:
                        case ReportObjectKind.ChartObject:
                        case ReportObjectKind.CrossTabObject:
                        case ReportObjectKind.BlobFieldObject:
                        case ReportObjectKind.MapObject:
                        case ReportObjectKind.OlapGridObject:
                        case ReportObjectKind.FieldHeadingObject:
                        case ReportObjectKind.FlashObject:
                        default:
                            // none of the other objects need to have login assigned
                            break;
                    }
                }
            }
        }

        static void SetCrystalDocumentLogon()
        {
            crAssignConnectionInfo();
            TableLogOnInfo crTableLogonInfo = new TableLogOnInfo();
            foreach (Table crTable in crReportDocument.Database.Tables)
            {
                try
                {
                    crConnectionInfo.Type = crTable.LogOnInfo.ConnectionInfo.Type;
                    crTableLogonInfo.ConnectionInfo = crConnectionInfo;
                    crTableLogonInfo.ReportName = crTable.LogOnInfo.ReportName;
                    crTableLogonInfo.TableName = crTable.LogOnInfo.TableName;

                    crTable.ApplyLogOnInfo(crTableLogonInfo);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error during SetCrystalDocumentLogon " + ex.Message);
                    throw;
                }
                SetSubreportLoginInfo(crReportDocument.ReportDefinition.Sections);
            }
        }
Dysuria answered 6/7, 2020 at 21:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.