cancel
Showing results for 
Search instead for 
Did you mean: 

Issues with migrating TTX to CDX

0 Kudos

I'm created code to replace all TTX files with CDX.

For RepCountry is still as TableName.

For some it works will but if there are more pointing to the same table then this appears.

My code is below.

How can I solve this?

using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System;
using System.IO;
using System.Windows.Forms;


namespace QuickTTX2XSD
{
    public partial class Form1 : Form
    {
        string xsdPath = @"C:\TFS\Infodis\TM\MAIN\Reports\XSD\";


        public Form1()
        {
            InitializeComponent();
        }


        private void ProcessDir(string sourceDir, string fileMask)
        {
            // Process the list of files found in the directory. 
            string[] fileEntries = Directory.GetFiles(sourceDir, fileMask);
            foreach (string fileName in fileEntries)
            {
                var fileNameToDo = fileName.Replace(".rpt", "_TODO.rpt");


                MessageTXT.Text = "Converting => " + fileName;
                Refresh();
                using (ReportDocument reportDocument = new ReportDocument())
                {
                    try
                    {
                        reportDocument.Load(fileName, OpenReportMethod.OpenReportByTempCopy);
                        if (replaceTTXwithXSD(fileName, reportDocument, xsdPath) == true)
                        {
                            //reportDocument.SaveAs(fileName);
                            reportDocument.SaveAs(fileName);
                            MessageTXT.Text = "Done => " + fileName;
                        }
                        else
                        {
                            if (File.Exists(fileNameToDo))
                                File.Delete(fileNameToDo);


                            reportDocument.SaveAs(fileNameToDo);


                            if (File.Exists(fileName))
                                File.Delete(fileName);


                            MessageTXT.Text = "Failed => " + fileName;
                        }
                    }
                    catch (Exception ex)
                    {
                        if (File.Exists(fileNameToDo))
                        {
                            File.Delete(fileNameToDo);
                        }
                        File.Move(fileName, fileNameToDo);
                        MessageBox.Show(ex.Message);
                    }
                }
            }


            // Recurse into subdirectories of this directory.
            string[] subdirEntries = Directory.GetDirectories(sourceDir);
            foreach (string subdir in subdirEntries)
                // Do not iterate through reparse points
                if ((File.GetAttributes(subdir) & FileAttributes.ReparsePoint) != FileAttributes.ReparsePoint)
                    ProcessDir(subdir, fileMask);
        }




        private void button1_Click(object sender, EventArgs e)
        {
            if (Directory.Exists(tbFolder.Text))
            {
                MessageTXT.Text = "Start searching Crystal Reports";
                Refresh();


                ProcessDir(tbFolder.Text, "*.rpt");


                MessageTXT.Text = "Ready";
            }
            else if (File.Exists(tbFolder.Text))
            {
                MessageTXT.Text = "Start analyzing Crysal Report";
                Refresh();


                FileInfo file = new FileInfo(tbFolder.Text);
                ProcessDir(file.DirectoryName, file.Name);


                MessageTXT.Text = "Ready";
            }
            else
            {
                MessageTXT.Text = "Folder or file does not exists";
            }
        }


        private bool replaceTTXwithXSD(string fileName, ReportDocument reportDocument, string xsdPath)
        {
            bool migratedReport = true;


            CrystalDecisions.CrystalReports.Engine.SubreportObject crSubreportObject;
            CrystalDecisions.CrystalReports.Engine.ReportDocument crSubreportDocument;


            try
            {
                NameValuePairs2 logonProperties = new NameValuePairs2
                {
                    new NameValuePair2 { Name = "ClassName", Value = ""},
                    new NameValuePair2 { Name = "Internal Connection ID",Value = Guid.NewGuid().ToString()},
                    new NameValuePair2 { Name = "Local Schema File",Value = fileName}


                };




                foreach (CrystalDecisions.CrystalReports.Engine.Table table in reportDocument.Database.Tables)
                {


                    if (!SetXSDConnection(fileName, xsdPath, logonProperties, table, reportDocument))
                    {
                        migratedReport = false;
                    }
                }


                //loop through all the sections to find all the report objects
                foreach (CrystalDecisions.CrystalReports.Engine.Section crSection in reportDocument.ReportDefinition.Sections)
                {
                    //loop through all the report objects to find all the subreports
                    foreach (CrystalDecisions.CrystalReports.Engine.ReportObject crReportObject in crSection.ReportObjects)
                    {
                        if (crReportObject.Kind == ReportObjectKind.SubreportObject)
                        {
                            crSubreportObject = (CrystalDecisions.CrystalReports.Engine.SubreportObject)crReportObject;
                            crSubreportDocument = crSubreportObject.OpenSubreport(crSubreportObject.SubreportName);


                            foreach (CrystalDecisions.CrystalReports.Engine.Table crTable in crSubreportDocument.Database.Tables)
                            {
                                if (!SetXSDConnection(fileName, xsdPath, logonProperties, crTable, crSubreportDocument))
                                {
                                    migratedReport = false;
                                }
                            }
                        }
                    }
                }


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


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


        private static Boolean SetXSDConnection(string fileName, string xsdPath, NameValuePairs2 logonProperties, CrystalDecisions.CrystalReports.Engine.Table table, ReportDocument reportDocument)
        {


            string xsdName = Path.GetFileNameWithoutExtension(table.Name.Replace("_ttx", ""));
            //string xsdName = Path.GetFileNameWithoutExtension(table.Name.Replace("_ttx", "")).Replace("_", "");
            string tableName = Path.GetFileNameWithoutExtension(table.Name);
            string xsdFileName = xsdPath + xsdName + ".XSD";
            if (!File.Exists(xsdFileName))
            {
                xsdName = Path.GetFileNameWithoutExtension(table.Name.Replace("_ttx", "").Replace("_", ""));
                xsdFileName = xsdPath + xsdName + ".XSD";
                if (!File.Exists(xsdFileName))
                {
                    MessageBox.Show("File does not exists: " + xsdFileName);
                    return false;
                }
            }


            try
            {
                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 = xsdFileName},
                        new NameValuePair2 {Name = "QE_SQLDB",Value = false},
                        new NameValuePair2 {Name = "SSO ENABLED",Value = false},
                    };


                TableLogOnInfo newInfo = new TableLogOnInfo();
                DbConnectionAttributes newAttributes = new DbConnectionAttributes();


                newAttributes.Collection = connectionProperties;


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


                newInfo.ConnectionInfo.ServerName = xsdFileName;
                newInfo.ConnectionInfo.Type = ConnectionInfoType.CRQE;
                //newInfo.ConnectionInfo.UType = 5;
                newInfo.TableName = xsdName;


                //System.Data.DataSet dataSet = new System.Data.DataSet();
                //dataSet.ReadXmlSchema(xsdFileName);
                //table.SetDataSource(dataSet.Tables[0]);


                table.Location = xsdName;
                table.ApplyLogOnInfo(newInfo);


                if (table.Location != xsdName)
                {
                    return false;
                }
                return true;


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return false;
            }
        }
        private void btnGetFolder_Click(object sender, EventArgs e)
        {
            string startupPath = Application.StartupPath;
            using (FolderBrowserDialog dialog = new FolderBrowserDialog())
            {
                dialog.SelectedPath = tbFolder.Text;
                dialog.Description = "Open a folder which contains the Crystal Reports";
                dialog.ShowNewFolderButton = false;
                dialog.RootFolder = Environment.SpecialFolder.MyComputer;
                if (dialog.ShowDialog() == DialogResult.OK)
                    tbFolder.Text = dialog.SelectedPath;
            }


        }
    }
}


Accepted Solutions (1)

Accepted Solutions (1)

0 Kudos

Who is Enner Perez?

And no such thing as Crystal Reports v13.0.5.891, it's the embedded report designer built into Visual Studio.

TTX is no longer supported, change that data source to XML file also and if it's not embedded in the first XML file combine them or you will need to use a subreport for the second data source.

Answers (7)

Answers (7)

0 Kudos

Hi Don,

Any Idea what I need to change to get this in place?

Ron

0 Kudos

I use CrystalReports v13.0.5.891 and the rest is v13.03501.

How can I change the tablenaam: RepCountry for ALIAS RepCountrCN_ttx.

If want to change the Tabelnaam: RepCountry to RepCountryCN, how can I do that in the code?

This is the RepCountryCN.XSD

0 Kudos

Again, you are using 2 tables now so you need to remove it from the main report and insert a subreport if you continue using multiple data sources/tables.

You can try creating an Alias but I know its not going to work, CR simply can't determine which table it belongs to.

Don

0 Kudos

What SDK are you using now?

0 Kudos

You cannot have 2 tables with the same name, not allowed in CRD so not allowed in SDK either.

0 Kudos

TTX hasn't been supported for al ong time.

Try updating thr connection in CR DEsigner and see it that works.

Don

0 Kudos

I already tried and that worked fine.

I dont know how to change it in the code.

0 Kudos

Issues with migrating TTX to XSD

Original rpt file contains tables which are pointing to the same tableName. In the code I made a split up per dataset.

RepCountryCN will have his own RepcountryCN.XSD

RepCountryCZ will have his own RepCountryCZ.XSD

And so on.

But also the tablename must to the same name.

For other datasets it works as long as they are not pointing to the same tablename.