cancel
Showing results for 
Search instead for 
Did you mean: 

Rebinding Report to new XML XSD

Former Member
0 Kudos

I cannot for the life of me find an example of what I need.

I have a report that is bound by xsd. How can I take this report in c#, and set the datasource to a new xml file? Every possible way that I've tried just throws me a nice logon screen. I know the files exists in the places that it is looking, and there is obviously no username and password to an xml/xsd file. Anyone have any kind of advice??

Accepted Solutions (0)

Answers (2)

Answers (2)

former_member208657
Active Contributor
0 Kudos

This is a sample I wrote a while ago to do this. It's been a while since I used it but it should do what you need.

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;
			}


			m_crReportDocument = new ReportDocument();
			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\training from Kristian\RAS .NET\ras11_csharp_win_xml_report\6a445e5a-3cb2-46de-96f3-db4fda43b580.xml H:\training\BO11\training from Kristian\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;
		}
	}
}

Former Member
0 Kudos

Hi, Salvatore

If you are being prompted for logon, either the report cannot find the XML file, or the structure of the XML file is different from the XSD file that the report was made against.

Here is an article on setting the location to a new XML file:

https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.km.cm.docs/oss_notes/sdn_oss_boj_erq/...

Another option would be to load the XML file into an ADO.NET Dataset, and passing that dataset to the report.

Regards,

Jonathan