Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

A reporting service using SOAP calls passing XML to a Data Extension

, 19 Oct 2005 CPOL
Demostrates how to render a report by passing XML to a data extension via SOAP calls.
RSDataExtensionXml_demo.zip
RSTestApp
CustomDataExtension
CustomDataExtension.csproj.user
Web References
BusinessService
BusinessData.disco
BusinessData.wsdl
Reference.map
Global.asax
RsReportViewer
AwReportViewer.suo
Design
vssver.scc
RsReportViewer.csproj.user
Web References
RS
Reference.map
ReportService.wsdl
RSTestApp.csproj.webinfo
RSTestReports
Northwind.rds
Northwind.udl
Report1.rdl
Report1.rdl.data
RSTest.rptproj.user
RSTestReports.rptproj
RSTestReports.rptproj.user
Sales Order Detail.rdl
Sales Order Detail.rdl.data
Territory Sales Drilldown.rdl
Territory Sales Drilldown.rdl.data
RSTestWS
Global.asax
RSTestWS.csproj.webinfo
temp
Web References
ReportServer
Reference.map
ReportService.wsdl
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Net;
using System.IO;
using System.Threading;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Principal;
using System.Xml;
using System.Data.SqlClient;

using System.Web.Services.Protocols;
using RSTestApp.ReportServer;

namespace RSTestApp
{
	/// <summary>
	/// Summary description for WebForm1.
	/// </summary>
	public class WebForm1 : System.Web.UI.Page
	{

		[DllImport("advapi32.dll", SetLastError=true)]
		public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword, 
			int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

		protected System.Web.UI.WebControls.DropDownList DropDownList1;
		protected System.Web.UI.WebControls.DropDownList DropDownList2;
		protected System.Web.UI.WebControls.Panel Panel1;

		protected System.Web.UI.WebControls.Button btnCompanySales;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				SqlConnection conn = new SqlConnection(@"Persist Security Info=True;User ID=sa;Initial Catalog=Northwind;Data Source=(local);");

				ListItem item = new ListItem("SELECTION","");

				// populate region
				DataSet ds1 = new DataSet();
				SqlDataAdapter adaptRegion = new SqlDataAdapter("select distinct region from customers", conn);
				adaptRegion.Fill(ds1);
				DropDownList1.DataTextField ="region";
				DropDownList1.DataValueField = "region";
				DropDownList1.DataSource = ds1;
				DropDownList1.DataBind();
				DropDownList1.Items.Insert(0, item);

				// populate city
				DataSet ds2 = new DataSet();
				SqlDataAdapter adaptCity = new SqlDataAdapter("select distinct city from customers", conn);
				adaptCity.Fill(ds2);
				DropDownList2.DataTextField = "city";
				DropDownList2.DataValueField = "city";
				DropDownList2.DataSource = ds2;
				DropDownList2.DataBind();
				DropDownList2.Items.Insert(0, item);

				adaptRegion.Dispose();
				adaptCity.Dispose();

				conn.Close();
				conn.Dispose();
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.btnCompanySales.Click += new System.EventHandler(this.btnCompanySales_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void btnCompanySales_Click(object sender, System.EventArgs e)
		{

			const int LOGON32_PROVIDER_DEFAULT = 0;
			//This parameter causes LogonUser to create a primary token.
			const int LOGON32_LOGON_INTERACTIVE = 2;
			IntPtr tokenHandle = new IntPtr(0);
			IntPtr dupeTokenHandle = new IntPtr(0);

			tokenHandle = IntPtr.Zero;
 
			// Call LogonUser to obtain a handle to an access token.
			bool returnValue = LogonUser("UserID", "Domain", "Password", LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref tokenHandle);

			WindowsIdentity newId = new WindowsIdentity(tokenHandle);
			WindowsImpersonationContext impersonatedUser = newId.Impersonate();

			string sCurname = WindowsIdentity.GetCurrent().Name;

			IPrincipal principal = new WindowsPrincipal(newId);

			Thread.CurrentPrincipal = principal;

			HttpContext.Current.User = principal;

			ReportServer.ReportingService rs = new ReportServer.ReportingService();			
			rs.Credentials = System.Net.CredentialCache.DefaultCredentials;

			StreamWriter stream = new StreamWriter( @"C:\report.log", true );                  

			// Render arguments
			byte[] result = null;
			string reportPath = "/RSTest/Report1";
			string format = "HTML4.0";
			string historyID = null;
			string devInfo = @"<DeviceInfo><Toolbar>false</Toolbar><HTMLFragment>true</HTMLFragment></DeviceInfo>";

			// Use parameter
			
			string sRegion = DropDownList1.SelectedItem.Value;
			string sCity = DropDownList2.SelectedItem.Value;

			XmlDocument doc = new XmlDocument();
			doc.LoadXml("<doc></doc>");

			XmlElement elem = doc.CreateElement("Parameters");

			// append region
			XmlElement childElem;
			
			childElem = doc.CreateElement("Region");
			childElem.AppendChild(doc.CreateTextNode(sRegion));
			elem.AppendChild(childElem);

			childElem = doc.CreateElement("City");
			childElem.AppendChild(doc.CreateTextNode(sCity));
			elem.AppendChild(childElem);

			doc.DocumentElement.AppendChild(elem);

			string sFilters = string.Empty;

			if (sRegion.Length > 0)
			{
				sFilters += "region='" + sRegion + "'";
			}

			if (sCity.Length > 0)
			{
				if (sFilters.Length > 0)
					sFilters += " and ";
				sFilters += "city='" + sCity + "'";
			}

			elem = doc.CreateElement("Filters");
			elem.AppendChild(doc.CreateTextNode(sFilters));
			doc.DocumentElement.AppendChild(elem);

			ParameterValue[] parameters = new ParameterValue[1];
			parameters[0] = new ParameterValue();
			parameters[0].Name = "XmlCommandText";
			parameters[0].Value = doc.InnerXml;

			DataSourceCredentials[] credentials = null;

			string showHideToggle = null;
			string encoding;
			string mimeType;
			Warning[] warnings = null;
			ParameterValue[] reportHistoryParameters = null;
			string[] streamIDs = null;

			try
			{				
				result = rs.Render(reportPath, format, historyID, devInfo, parameters, credentials, showHideToggle, out encoding, out mimeType, out reportHistoryParameters, out warnings, out streamIDs);
				Response.BinaryWrite(result);
				Panel1.Visible = false;
			}
			catch (SoapException ex)
			{
				stream.WriteLine(ex.Detail.OuterXml);
			}
			// Write the contents of the report to file.
			try
			{
				stream.WriteLine( "Render successful." );
				stream.WriteLine( "Result written to the file." );
			}
			catch ( Exception ex )
			{
				Console.WriteLine( ex.Message );
			}
			finally
			{
				if (stream != null)
				{
					stream.Close();
				}
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ken C. Len
Software Developer (Senior) LEN Associates Inc.
United States United States
Years of software consulting and software development using Microsoft development products such as Microsoft Content Management System, SQL Server Reporting Service, ASP.Net C# VB.Net, HTML and javascript web development, Visual Studio add-on development, C++ MFC/ATL and COM+ development, and ActiveX components.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.1411023.1 | Last Updated 19 Oct 2005
Article Copyright 2005 by Ken C. Len
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid