on 09-08-2014 3:54 PM
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.
Thanks,
Andrew Huang
Message was edited by: Ludek Uher
Hi,
Still O/S, would someone help?
Thanks,
Andrew Huang
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
So the data source changes?
It works after manually mapping and "Verify Database" is always up to date.
Report data source must be the same as the original or CR SDK maps the field automatically and any fields it can't find it simply deletes.
Try adding RAS to your project and then use the code above:
Here’s all of mine:
using System.Reflection;
using System.Runtime.InteropServices;
using System;
using System.IO;
using System.Xml;
using System.Drawing;
using System.Drawing.Printing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
using System.Diagnostics;
using System.Globalization;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using CrystalDecisions.ReportAppServer;
using CrystalDecisions.ReportAppServer.ClientDoc;
using CrystalDecisions.ReportAppServer.Controllers;
using CrystalDecisions.ReportAppServer.ReportDefModel;
using CrystalDecisions.ReportAppServer.CommonControls;
using CrystalDecisions.ReportAppServer.CommLayer;
using CrystalDecisions.ReportAppServer.CommonObjectModel;
using CrystalDecisions.ReportAppServer.ObjectFactory;
using CrystalDecisions.ReportAppServer.Prompting;
using System.Data.OleDb;
using CrystalDecisions.ReportAppServer.DataSetConversion;
using CrystalDecisions.ReportAppServer.DataDefModel;
using CrystalDecisions.ReportSource;
using CrystalDecisions.Windows.Forms;
//using System.Data.SqlClient;
using System.Runtime.InteropServices;
//using System.Net.Mail;
using CrystalDecisions.ReportAppServer.XmlSerialize;
Be sure to select the version 13:
Don
Hi Don,
I tried your code, the result is same, would you help me to check it out?
How to connect rptClientDoc to reportDoc? Because I have to print out without preview.
ReportDocument reportDoc = new ReportDocument();
reportDoc.Load(rptFile, OpenReportMethod.OpenReportByTempCopy);
reportDoc.DataSourceConnections[0].SetConnection(xsdFile, "", false);
CrystalDecisions.ReportAppServer.ClientDoc.ISCDReportClientDocument rptClientDoc;
rptClientDoc = reportDoc.ReportClientDocument;
System.Data.DataSet reportData = new System.Data.DataSet();
reportData.ReadXml(xmlFile, XmlReadMode.ReadSchema);
CrystalDecisions.ReportAppServer.DataDefModel.ISCRDataSet iscrReportData = (CrystalDecisions.ReportAppServer.DataDefModel.ISCRDataSet)CrystalDecisions.ReportAppServer.DataSetConversion.DataSetConverter.Convert(reportData);
rptClientDoc.DatabaseController.SetDataSource(iscrReportData);
reportDoc.PrintOptions.PrinterName = "Microsoft XPS Document Writer";
reportDoc.PrintToPrinter(1, false, 0, 0);
reportDoc.Dispose();
Thanks,
Andrew Huang
Hi Don,
I created crystal report viewer, but it still display the original data:
private void Form1_Load(object sender, EventArgs e)
{
string path = @"C:\Temp\Test2";
String rptFile = System.IO.Path.Combine(path, "Delivery Note2.rpt");
String xmlFile = System.IO.Path.Combine(path, "Delivery Note2.xml");
String xsdFile = System.IO.Path.Combine(path, "Delivery Note2.xsd");
this.crystalReportViewer1.Dock = DockStyle.Fill;
this.Controls.Add(this.crystalReportViewer1);
ReportDocument reportDoc = new ReportDocument();
reportDoc.Load(rptFile, OpenReportMethod.OpenReportByTempCopy);
reportDoc.DataSourceConnections[0].SetConnection(xsdFile, "", false);
CrystalDecisions.ReportAppServer.ClientDoc.ISCDReportClientDocument rptClientDoc;
rptClientDoc = reportDoc.ReportClientDocument;
System.Data.DataSet reportData = new System.Data.DataSet();
reportData.ReadXml(xmlFile, XmlReadMode.ReadSchema);
CrystalDecisions.ReportAppServer.DataDefModel.ISCRDataSet iscrReportData = (CrystalDecisions.ReportAppServer.DataDefModel.ISCRDataSet)CrystalDecisions.ReportAppServer.DataSetConversion.DataSetConverter.Convert(reportData);
rptClientDoc.DatabaseController.SetDataSource(iscrReportData);
this.crystalReportViewer1.ReportSource = rptClientDoc.ReportSource;
this.crystalReportViewer1.RefreshReport();
}
Thanks,
Andrew Huang
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
Hi Andrew
I don't see you actually creating a dataset fom your xml. E.g.; something like this:
DataSet ds = new DataSet();
// Create new FileStream with which to read the schema.
System.IO.FileStream fsReadXml = new System.IO.FileStream
(myXMLfile, System.IO.FileMode.Open);
try
{
ds.ReadXml(fsReadXml);
.
.
.
}
Also see: Walkthrough: Reading XML Data into a Dataset
- Ludek
Senior Support Engineer AGS Product Support, Global Support Center Canada
Follow us on Twitter
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Andrew,
Do you have Saved Data checked on in the Report?
What happens if you refresh the data?
If you manually set the data source in CR Designer does that work, no mapping UI pop up and/or message like Data up to date if you verify the database also?
Try this also:
bool IsRpt = true;
CrystalDecisions.ReportAppServer.ClientDoc.ISCDReportClientDocument rptClientDoc;
rpt.Load(rptName.ToString(), OpenReportMethod.OpenReportByTempCopy);
rptClientDoc = rpt.ReportClientDocument;
System.Data.DataSet ds = new System.Data.DataSet();
ds.Tables.Add(dt1);
ds.Tables.Add(dt2);
ds.WriteXml(@"D:\Atest\list_of_workflows.xml");
ds.WriteXmlSchema(@"D:\Atest\list_of_workflows.xsd");
ds.ReadXml(@"D:\Atest\Dev Element\data_ReplicateCRIssue.xml", XmlReadMode.ReadSchema);
ISCRDataSet DS1 = (ISCRDataSet) CrystalDecisions.ReportAppServer.DataSetConversion.DataSetConverter.Convert(ds);
// uses this for OLE DB DS record set
rptClientDoc.DatabaseController.SetDataSource(DS1, "FunctionSales", "FunctionTotalPrice");
IsRpt = true;
Requires using RAS and ReportClientDocument
Then view the RCD also:
try
{
if (!IsRpt)
{
crystalReportViewer1.ReportSource = rptClientDoc.ReportSource;
}
else
{
crystalReportViewer1.ReportSource = rpt;
}
}
catch (Exception ex)
{
MessageBox.Show("ERROR: " + ex.Message);
}
Don
Thanks, Don.
For your questions:
Do you have Saved Data checked on in the Report?
Before the report document created, the three files (rpt, xml and xsd) are already saved in a specified folder, which named by GUID. So it's always different for each time. And I checked that DataSourceConnections is updated. But I'm not sure how to check the data source attached the report.What happens if you refresh the data?
Display the original data. (In designer)If you manually set the data source in CR Designer does that work, no mapping UI pop up and/or message like Data up to date if you verify the database also?
It works after manually mapping and "Verify Database" is always up to date.I may forget something:
Thanks,
Andrew Huang
User | Count |
---|---|
101 | |
13 | |
13 | |
11 | |
11 | |
7 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.