Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Change datasource for xml and xsd in runtime

Hi

I've a report working with Xml and Xsd, for some reason, I've to create a new "instance" when calling the report, means, create new folder to save the three files: .rpt, .xml and .xsd.

The sample code as following:

            System.Data.DataSet reportData = new System.Data.DataSet();

            reportData.ReadXml(xmlFile);

            ReportDocument reportDoc = new ReportDocument();

            reportDoc.Load(rptFile);

            reportDoc.DataSourceConnections[0].SetConnection(xsdFile, "", false);

            reportDoc.SetDataSource(reportData);

The report always uses the initial data (in report design) instead of the new datasource, but the DataSourceConnections updated.

Questions please:

    How to change the data source in runtime?

    How can I check the current data source?

PS.

  • Visual Studio 2012
  • Crystal Reports for .NET 12.0.2000.0
  • C#

Thanks,

Andrew Huang

Message was edited by: Ludek Uher

Former Member
replied

Hi Andrew,

SO I went back and had a second look at this question and my code snippets and I can simplify it even more.

Your first example you were loading the XML data into a Dataset and using an XSD file for the structure:

            System.Data.DataSet reportData = new System.Data.DataSet();

            reportData.ReadXml(xmlFile);

            ReportDocument reportDoc = new ReportDocument();

            reportDoc.Load(rptFile);

// comment this line out and it should work            reportDoc.DataSourceConnections[0].SetConnection(xsdFile, "", false);

            reportDoc.SetDataSource(reportData);

What you should do to generate the XML/XSD is use this line so it it's all in  the xml file

"c:\\sc.xml", XmlWriteMode.WriteSchema);

Then your code will work, as long as your remove the one line.

If you want to read the XML file directly then the below code will work and you are not limited to a Dataset memory limitation.

Try this:

try
{
    foreach (CrystalDecisions.CrystalReports.Engine.Table rptTable in rpt.Database.Tables)
    {
        try
        {
            rptTable.Location = @"C:\Datafolder\" + rptTable.Location + ".xml";
        }
        catch (Exception ex)
        {
            MessageBox.Show("ERROR: " + ex.Message);
        }


    }
}
catch (Exception ex)
{
    MessageBox.Show("ERROR: " + ex.Message);
}

This assumes you are using the crdb_ADOPlus driver.

Don

0 View this answer in context
Not what you were looking for? View more on this topic or Ask a question