Skip to Content
0
Former Member
Aug 14, 2015 at 01:28 PM

How to change data source for a ttx-report in WPF-application

440 Views

Hello.

I have a *.rpt-file, created in the "SAP Crystal Reports" program with a ttx-file as data source description.

For example, the .ttx file is very simple:

str1 String 25 test1

I need show this report by a WPF-application, and I want to use an "Object Collection" for filling its data.

I try chahge a data source for the report like this:

namespace WPFTest
{
    public class Filds3
    {
        public string str1 { get; set; }
    }

    public partial class MainWindow : Window
    {
        public CrystalReportsViewer crystalReportsViewer1;

        public MainWindow()
        {
            InitializeComponent();
            try
            {
                this.crystalReportsViewer1 = new CrystalReportsViewer()
                {
                    Height = 500,
                    Width = 800
                };
                
                ReportDocument doc = new ReportDocument();
                doc.Load("Report4.rpt");

                var testValues2 = new ArrayList()
                {
                    new Filds3 { str1 = "sd133" },
                    new Filds3 { str1 = "sd222" },
                    new Filds3 { str1 = "sd344" },
                    new Filds3 { str1 = "sd4323" },
                };

                string modelClassName = "WPFTest.Filds3";

                NameValuePairs2 connectionProperties = new NameValuePairs2
                    {
                        new NameValuePair2
                        {
                            Name = DbConnectionAttributes.CONNINFO_DATABASE_DLL,
                            Value = DbConnectionAttributes.DATABASE_DLL_CRDB_ADOPLUS
                        },
                        new NameValuePair2
                        {
                            Name = DbConnectionAttributes.QE_DATABASE_NAME,
                            Value = ""
                        },
                        new NameValuePair2
                        {
                            Name = "QE_DatabaseType",
                            Value = "ADO.NET (XML)"
                        },
                        new NameValuePair2
                        {
                            Name = DbConnectionAttributes.QE_SERVER_DESCRIPTION,
                            Value = modelClassName
                        },
                        new NameValuePair2
                        {
                            Name = "QE_SQLDB",
                            Value = false
                        },
                        new NameValuePair2
                        {
                            Name = "SSO ENABLED",
                            Value = false
                        },
                    };

                NameValuePairs2 logonProperties = new NameValuePairs2
                {
                    new NameValuePair2
                    {
                        Name = "ClassName",
                        Value = modelClassName
                    },
                    new NameValuePair2
                    {
                        Name = "Internal Connection ID",
                        Value = Guid.NewGuid().ToString()
                    }
                };

                foreach (CrystalDecisions.Shared.IConnectionInfo connection in doc.DataSourceConnections)
                {
                    connection.SetLogonProperties(logonProperties);
                }

                foreach (CrystalDecisions.CrystalReports.Engine.Table table in doc.Database.Tables)
                {
                    //TableLogOnInfo info = table.LogOnInfo;
                    TableLogOnInfo newInfo = new TableLogOnInfo();
                    newInfo.TableName = modelClassName;

                    DbConnectionAttributes newAttributes = new DbConnectionAttributes();

                    newAttributes.Collection = connectionProperties;

                    newInfo.ConnectionInfo.Attributes = newAttributes;
                    newInfo.ConnectionInfo.LogonProperties = logonProperties;

                    newInfo.ConnectionInfo.ServerName = modelClassName;
                    newInfo.ConnectionInfo.Type = ConnectionInfoType.CRQE;
                    newInfo.ConnectionInfo.UType = 5;

                    table.ApplyLogOnInfo(newInfo);                  

                    string location = table.Location;
                    table.SetDataSource(testValues2);

                   // table.Location = "WPFTest_Filds3"; // Exception here
                }

                this.crystalReportsViewer1.ViewerCore.ReportSource = doc;

                this.crystalReportsViewer1.Owner = this;

                this.Grid1.Children.Add(this.crystalReportsViewer1);

            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
}
}

But I getting an Exception like:

Failed to load database information.

Error in File Report4 10384_8240_{4CDE0A62-05A9-4CBA-8A41-B185B3A3946A}.rpt:

Failed to load database information.

Tell me please, how I can correct my code for success.

Thank you.