Click here to Skip to main content
15,881,173 members
Articles / Web Development / ASP.NET

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

Rate me:
Please Sign up or sign in to vote.
4.00/5 (5 votes)
19 Oct 2005CPOL8 min read 85.3K   406   26  
Demostrates how to render a report by passing XML to a data extension via SOAP calls.
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.Text;

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

namespace RSTestApp
{
	/// <summary>
	/// Summary description for WebForm1.
	/// </summary>
	public class WebForm2a : 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.Panel Panel2;

		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=AdventureWorks2000;Data Source=(local);");

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

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

				// populate city
				DataSet ds2 = new DataSet();
				SqlDataAdapter adapt2 = new SqlDataAdapter("select distinct SalesPersonID, FirstName + ' ' + LastName as Name from SalesPerson inner join Employee on SalesPerson.SalesPersonID = Employee.EmployeeID", conn);
				adapt2.Fill(ds2);
				DropDownList2.DataTextField = "Name";
				DropDownList2.DataValueField = "SalesPersonID";
				DropDownList2.DataSource = ds2;
				DropDownList2.DataBind();
				DropDownList2.Items.Insert(0, item);

				adapt1.Dispose();
				adapt2.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", "Password1", 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/Territory Sales Drilldown";
			string format = "HTML4.0";
			string historyID = null;
			string devInfo = @"<DeviceInfo><Toolbar>true</Toolbar><HTMLFragment>false</HTMLFragment></DeviceInfo>";

			// Use parameter
			
			string sTerritoryID = DropDownList1.SelectedItem.Value;
			string sSalesPersonID = DropDownList2.SelectedItem.Value;

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

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

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

			childElem = doc.CreateElement("SalesPersonID");
			childElem.AppendChild(doc.CreateTextNode(sSalesPersonID));
			elem.AppendChild(childElem);

			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.Write(ASCIIEncoding.ASCII.GetString(result));
				Response.Flush();
				Response.End();
			}
			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)


Written By
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.

Comments and Discussions