Skip to Content

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

Native XML Driver & .NET Application

I found this thread: https://www.sdn.sap.com/irj/sdn/businessobjects-sdk-forums the poster is asking exactly what I need to know, but the suggestions posted aren't working for me.

I'm developing in CR XI R2 SP 4, Visual Studio 2005. In Crystal Reports, I select Native XML Driver, point to my XML file (named MasterXML.xml) and my XSD file. Build the Report & Save. In .NET, I can launch the report with the following code and it works great:

public partial class Form1 : Form
    {
        private ReportDocument nestReport;

        public Form1()
        {
            InitializeComponent();

            this.Load += new EventHandler(Form1_Load);            
        }

        void Form1_Load(object sender, EventArgs e)
        {
            nestReport = new ReportDocument();

            string appPath = @"C:\_NET_2_0_Projects\myCRViewer\myCRViewer";
            string RPT_Path = appPath + @"\Reports\New_ne.rpt";                    

            nestReport.Load(RPT_Path);

            crystalReportViewer1.ReportSource = nestReport;
        }
    }

Now I've taken MasterXML.xml and copied it, renamed it to MasterXML1.xml. I changed several of the element values, but it still conforms to the same schema as the original MasterXML.xml. How do I launch the report with a new XML Source?

public partial class Form1 : Form
    {
        private ReportDocument nestReport;

        public Form1()
        {
            InitializeComponent();

            this.Load += new EventHandler(Form1_Load);            
        }

        void Form1_Load(object sender, EventArgs e)
        {
            nestReport = new ReportDocument();

            string appPath = @"C:\_NET_2_0_Projects\myCRViewer\myCRViewer";
            string RPT_Path = appPath + @"\Reports\New_ne.rpt";
            string XML_Path = appPath + @"\Reports\MasterXML1.xml";            

            nestReport.Load(RPT_Path);

            DataSet dataSet = new DataSet();
            dataSet.ReadXml(XML_Path);            

            //Below does not work
            nestReport.Database.Tables[0].SetDataSource(dataSet);

            //Below also does not work
            //nestReport.SetDataSource(dataSet);

            crystalReportViewer1.ReportSource = nestReport;
        }
    }

No matter what I do, I can not switch the XML datasource.

I'm trying to avoid the ADO.NET dataset route totally because I want my .net viewer to take a RPT location, and an XML location as parameters, and launch the report, with no knowledge of the reports contents.

I've seen a note that pertains to RAS and I've seen a note pertaining to VB 6. But not one for .NET Can someone help? Thanks.

Former Member
replied

Reposting the contents of the entire .cs file.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using CrystalDecisions.ReportAppServer.ClientDoc;
using CrystalDecisions.ReportAppServer.Controllers;
using CrystalDecisions.ReportAppServer.DataDefModel;

namespace cr11_csharp_win_change_XML_file
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private CrystalDecisions.Windows.Forms.CrystalReportViewer crystalReportViewer1;
		private System.Windows.Forms.Button button1;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		private ReportDocument m_crReportDocument = null;
		private ISCDReportClientDocument m_crReportClientDocument = null;
		

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.crystalReportViewer1 = new CrystalDecisions.Windows.Forms.CrystalReportViewer();
			this.button1 = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// crystalReportViewer1
			// 
			this.crystalReportViewer1.ActiveViewIndex = -1;
			this.crystalReportViewer1.Location = new System.Drawing.Point(8, 48);
			this.crystalReportViewer1.Name = "crystalReportViewer1";
			this.crystalReportViewer1.ReportSource = null;
			this.crystalReportViewer1.Size = new System.Drawing.Size(480, 352);
			this.crystalReportViewer1.TabIndex = 0;
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(8, 8);
			this.button1.Name = "button1";
			this.button1.TabIndex = 1;
			this.button1.Text = "button1";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(568, 413);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.crystalReportViewer1);
			this.Name = "Form1";
			this.Text = "Form1";
			this.Resize += new System.EventHandler(this.Form1_Resize);
			this.Load += new System.EventHandler(this.Form1_Load);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void button1_Click(object sender, System.EventArgs e)
		{		
			
			CrystalDecisions.ReportAppServer.DataDefModel.ConnectionInfo crConnectionInfo = null;
			DatabaseController crDatabaseController;			
			string strApplicationDirectory = Application.StartupPath;						
			CrystalDecisions.ReportAppServer.DataDefModel.ISCRTable crTableNew;			
			ISCRPropertyBag crAttributes, crLogonInfo;


			// check if the ReportDocument object already exists, if so destroy it
			if(m_crReportDocument != null)
			{
				m_crReportDocument.Dispose();
				m_crReportDocument = null;
			}

			// we must specify a RAS server because rcapi in proc doesn't work unless 
			// you have an OEM partner keycode.
			m_crReportDocument = new ReportDocument();
			m_crReportDocument.ReportAppServer = "localhost";
			m_crReportDocument.Load(strApplicationDirectory + "\\..\\..\\Report1.rpt");
			m_crReportClientDocument = m_crReportDocument.ReportClientDocument;

			// loop through the tables in the main report.
			crDatabaseController = m_crReportClientDocument.DatabaseController;
			foreach(CrystalDecisions.ReportAppServer.DataDefModel.ISCRTable crTableOld in crDatabaseController.Database.Tables)
			{
				// clone the original table, then get the ConnectionInfo
				crTableNew = crTableOld.Clone(true);
				crConnectionInfo = crTableNew.ConnectionInfo;			

				crAttributes = crConnectionInfo.Attributes;				
				/*
				 * There are 7 items in the property bag for an XML connection.
				 * We'll have to change the QE_ServerDescription in order to 
				 * use a different XML and XSD file.
				 * 
				 * Database DLL="crdb_xml.dll",
				 * QE_DatabaseName="",
				 * QE_DatabaseType="XML",
				 * QE_LogonProperties=,
				 *		Local Schema File="c:\path\to\schema\file\schema.xsd"
				 *		Local XML File="c:\path\to\xml\file\data.xml"
				 * QE_ServerDescription="H:\training\BO11\RAS .NET\ras11_csharp_win_xml_report\6a445e5a-3cb2-46de-96f3-db4fda43b580.xml H:\training\BO11\RAS .NET\ras11_csharp_win_xml_report\f0730b1d-02e1-426c-ad0a-ebe7fb547a66.xsd",
				 * QE_SQLDB="0",
				 * SSO Enabled="0"
				 */

				// this code to change the location of the XML and XSD file is not working right now
				// It gives an error "Invalid argument for database", even though all the correct
				// info is passed into PropertyBags.
				if(crConnectionInfo.Kind == CrConnectionInfoKindEnum.crConnectionInfoKindCRQE)
				{
					crLogonInfo = new PropertyBagClass();
					crLogonInfo.Add("Local Schema File", strApplicationDirectory + "\\schema.xsd");
					crLogonInfo.Add("Local XML File", strApplicationDirectory + "\\data.xml");					
					crAttributes["QE_LogonProperties"] = crLogonInfo;

					crAttributes["QE_ServerDescription"] = String.Concat(strApplicationDirectory + "\\data.xml ", strApplicationDirectory + "\\schema.xsd");                    
					
					crDatabaseController.SetTableLocation(crTableOld, crTableNew);				
				}
			}

			this.crystalReportViewer1.ReportSource = m_crReportDocument;
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{
			this.crystalReportViewer1.Left = 0;
			this.crystalReportViewer1.Width = this.Width;
			this.crystalReportViewer1.Height = this.Width - this.crystalReportViewer1.Top;
            			
		}

		private void Form1_Resize(object sender, System.EventArgs e)
		{
			this.crystalReportViewer1.Left = 0;
			this.crystalReportViewer1.Width = this.Width;
			this.crystalReportViewer1.Height = this.Width - this.crystalReportViewer1.Top;
		}
	}
}

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