Integrating Reporting Services into ASP.NET

by David Lloyd



Listing One



<%@ Page language="c#" Codebehind="ReportLauncher.aspx.cs" 

  AutoEventWireup="false" Inherits="NorthwindBI.ReportLauncher" %>

<HTML>

    <HEAD>

        <title>Report Launcher Web Form</title>

        <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">

        <meta content="C#" name="CODE_LANGUAGE">

        <meta content="JavaScript" name="vs_defaultClientScript">

        <meta content="http://schemas.microsoft.com/intellisense/ie5" 

                                                   name="vs_targetSchema">

    </HEAD>

    <body>

        <form id="ReportForm" method="post" runat="server" 

                           encType="multipart/form-data" target="_blank">

            <table>

                <tr>

                    <td><b>Report Launcher Web Form</b></td>

                </tr>

                <tr>

                    <td><asp:label id="lblMessage" 

                                            Runat="server"></asp:label></td>

                </tr>

                <tr>

                    <td>&nbsp;</td>

                </tr>

                <tr>

                    <td>

                        <table>

                            <tr>

                                <td colSpan="2">Please enter information 

                                                         for the Report</td>

                            </tr>

                            <tr>

                                <td colSpan="2">&nbsp;</td>

                           </tr>

                            <tr>

                                <td>Beginning Date:

                                </td>

                                <td><asp:textbox id="txtBeginningDate" 

                                          Runat="server"></asp:textbox>&nbsp;

                                    <asp:requiredfieldvalidator 

                                            id="rfvBeginningDate" 

                                            Runat="server" 

                                            ErrorMessage="Beginning 

                                                      Date is required."

                                       ControlToValidate=

                                         "txtBeginningDate">

                                         </asp:requiredfieldvalidator>&nbsp;

                                    <asp:comparevalidator id=

                                          "cvBeginningDate" Runat="server" 

                                           ErrorMessage="Beginnging Date 

                                           must be a Date."

                                       Type="Date" Operator="DataTypeCheck" 

                                         ControlToValidate="txtBeginningDate">

                                         </asp:comparevalidator></td>

                            </tr>

                            <tr>

                                <td>End Date:

                                </td>

                                <td><asp:textbox id="txtEndDate" 

                                         Runat="server"></asp:textbox>&nbsp;

                                   <asp:requiredfieldvalidator id="rfvEndDate"

                                         Runat="server" ErrorMessage="End Date

                                         is required." ControlToValidate=

                                         "txtEndDate">

                                         </asp:requiredfieldvalidator>&nbsp;

                                    <asp:comparevalidator id="cvEndDate" 

                                         Runat="server" ErrorMessage=

                                         "End Date must be a Date."Type="Date"

                                       Operator="DataTypeCheck" 

                                           ControlToValidate="txtEndDate">

                                           </asp:comparevalidator></td>

                            </tr>

                            <tr>

                                <td colSpan="2">&nbsp;</td>

                            </tr>

                            <tr>

                                <td align="right" colSpan="2"><asp:button 

                                    id="btnSubmit" Runat="server" 

                                    Text="Run Report"></asp:button></td>

                            </tr>

                        </table>

                    </td>

                </tr>

            </table>

        </form>

    </body>

</HTML>





Listing Two



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.IO;

using NorthwindBI.localhost; // To reference the Web Service, it 

                             // will be <project name>.<web reference name>

namespace NorthwindBI

{

    /// <summary>

    /// Summary description for ReportLauncher.

    /// </summary>

    public class ReportLauncher : System.Web.UI.Page

    {

       protected System.Web.UI.WebControls.TextBox txtBeginningDate;

        protected System.Web.UI.WebControls.Button btnSubmit;

        protected System.Web.UI.WebControls.RequiredFieldValidator 

                                                           rfvBeginningDate;

        protected System.Web.UI.WebControls.CompareValidator cvBeginningDate;

        protected System.Web.UI.WebControls.RequiredFieldValidator rfvEndDate;

        protected System.Web.UI.WebControls.CompareValidator cvEndDate;

        protected System.Web.UI.WebControls.Label lblMessage;

        protected System.Web.UI.WebControls.TextBox txtEndDate;

    

        private void Page_Load(object sender, System.EventArgs e)

        {

        }

        #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.btnSubmit.Click += 

                               new System.EventHandler(this.btnSubmit_Click);

            this.Load += new System.EventHandler(this.Page_Load);

        }

        #endregion



        private void btnSubmit_Click(object sender, System.EventArgs e)

        {

            // Initialize Web Service

            ReportingService rs = new ReportingService();

            rs.Credentials = System.Net.CredentialCache.DefaultCredentials;



            //Setup Parameters for Report

            ParameterValue[] parms = new ParameterValue[2];

            parms[0]= new ParameterValue();

            parms[0].Name = "Beginning_Date";

            parms[0].Value = txtBeginningDate.Text;

            parms[1]= new ParameterValue();

            parms[1].Name = "Ending_Date";

            parms[1].Value = txtEndDate.Text;



            //Setup Arguements for the render method

            byte[] results = null;  // Results always returns as a byte array

            string reportPath = "/NorthwindBusinessIntelligence/SalesByYear"; 

                                 //Do not include root path (i.e "http://...")

            string format = "PDF";  

                             //other formats include "IMAGE", "HTML5", et. al

           string historyID = null;

            string devInfo = @"<DeviceInfo><Toolbar>False</Toolbar>

                          </DeviceInfo>";  //Turns off menu bar for the page

            DataSourceCredentials[] credentials = null;

            string showHideToggle = null;

            string encoding;

            string mimeType;

            Warning[] warnings = null;

            ParameterValue[] reportHistoryParameters = null;

            string[] streamIDs = null;

            

            try

            {

                // Call the Reporting Service "render" method to get byte[]

                results = rs.Render(reportPath, format, historyID, devInfo, 

                    parms, credentials, showHideToggle, out encoding, 

                    out mimeType, out reportHistoryParameters, out warnings,

                    out streamIDs);

                StreamToPDF(results);

            }

            catch (Exception ex)

            {

                this.lblMessage.Text = ex.Message; 

            }

        }

        private void StreamToPDF(byte[] report)

        {

            // Buffer this pages output until the PDF is complete.

            Response.Buffer = true;

            // Tell the browser this is a PDF document so it 

            //                       will use an appropriate viewer.

            Response.ContentType = "application/pdf";



            // Added appropriate headers

            Response.AddHeader("Content-Disposition","inline; 

                                                    filename=Report.pdf");

            Response.AddHeader("Content-Length", report.Length.ToString());

            // Write the PDF stream out

            Response.BinaryWrite(report);

            // Send all buffered content to the client

            Response.Flush();

        }

    }

}













4



