Tutorial: 3-tier ASP.NET / SSRS solution with PDF output, part 3. The Business Logic Layer.
Get Adobe Flash player

Tutorial: 3-tier ASP.NET / SSRS solution with PDF output, part 3. The Business Logic Layer. 

Posted by Igor Wednesday, August 04, 2010 5:27:22 AM
Rate this Content 6 Votes

As you remember, from the architectural point of view we should implement 3-tier solution with the User Interface, Business Logic and Data Access Layers.

Tutorial Part 3. The Business Logic Layer implementation.

A few words beforehand. In general, the main idea is to have the Business Logic class, which could incapsulate any / complex custom business logic (i.e. use several stored procedures at the database, use, for example, the LINQ logic if required, fetch the data from remote services etc) and pack the results in one or more custom object data sets which will be passed to the SSRS report.

Let's take a look to the implementation.

1. First of all, we will have the Parent base class, which will be responsible for the PDF output from the SSRS report:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Reporting.WebForms;

namespace SSRSTutorial
{
    public class ReportBase
    {
        public ReportViewer thisReport { get; set; }

        public ReportBase(ReportViewer reportViewer, string localReportPath)
        {
            if (reportViewer == null)
            {
                thisReport = new ReportViewer();
            }
            else
            {
                thisReport = reportViewer;
            }
            thisReport.ProcessingMode = ProcessingMode.Local;
            thisReport.LocalReport.ReportPath = localReportPath;
            thisReport.LocalReport.DataSources.Clear();
            thisReport.LocalReport.EnableExternalImages = true;
        }

        protected byte[] RenderReport()
        {
            return RenderReport(true);
        }

        protected byte[] RenderReport(bool performBinaryWrites)
        {
            byte[] bytes = null;
            if (performBinaryWrites)
            {
                Warning[] warnings;
                string[] streamids;
                string mimeType;
                string encoding;
                string extension;
                bytes = thisReport.LocalReport.Render(
                   "PDF", null, out mimeType, out encoding,
                    out extension,
                   out streamids, out warnings);
            }
            else
            {
                thisReport.LocalReport.Refresh();
            }
            return bytes;
        }
    }
}

As you may see, the base class:
a) create the new ReportViewer object at the constructor (or just use the reference to the ReportViewer already created) and adjust the ReportViewer in turn to use our SSRS report created (i.e. the Report1.rdlc file)
b) the RenderReport method provide us with the PDF binary output as the aaray of bytes from the ReportViewer object which inialized with our local Report1.rdlc SSRS report.

2. As well as we have the base class implemented, we just needs to implement the business logic class which is able to provide the custom logic calculations. You may found the class implemented below:

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using Microsoft.Reporting.WebForms;
using System.IO;
using System.Configuration;
using System.Data;

using SSRSTutorial.DAL;


namespace SSRSTutorial
{
    public class blReport : ReportBase
    {
        private List<dalReport> _outList = new List<dalReport>();

    public blReport(ReportViewer reportViewer)
            : base(reportViewer,  @"Z:\Work\ThePathToOurReport\report\Report1.rdlc") {}

        public byte[] RunReport(string option, out string RetMsg)
        {
            RetMsg = "";

            RetMsg = GetReport(option, out _outList);
            if (_outList.Count() == 0)
            {
                RetMsg += " NO RECORDS FOUND!";
                return null;
            }

            // populate the datasources for this report
            thisReport.LocalReport.DataSources.Add(new ReportDataSource("SSRSTutorial_DAL_dalReport", _outList));

            return RenderReport();
        }

        private string GetReport(string userName, out List<dalReport> sList)
        {
            string retMsg = "";
            sList = new List<dalReport>();

            //++++++++++++++++++++++++++++++++++++++++++++
            //You can implement any custom business logic here

            try
            {
                SSRSTutorial.ssrstutorialDataContext dc = new SSRSTutorial.ssrstutorialDataContext();
                #region
                    List<SSRSTutorial.GetUsersByNameResult> sl = dc.GetUsersByName(userName).ToList();

                    if (sl != null || sl.Count() > 0)
                        foreach (SSRSTutorial.GetUsersByNameResult r in sl)
                        {
                                //DATA
                                dalReport dr = new dalReport();
                                dr.UserName = r.UserName;
                                dr.UserAddress = r.UserAddress;
                                dr.UserId = r.UserId;
                                //add the line
                                sList.Add(dr);
                        }                
                #endregion
            }
            catch (System.Exception ex)
            { retMsg = "GetReport Exception:" + ex.Message + " Stack: " + ex.StackTrace; };           
 
           return retMsg;
        }
    }
}

We are using the Business Logic class from above at the User Interface layer ( dafault.aspx.sc Code Behind ):

       SSRSTutorial.blReport reportOutput = new SSRSTutorial.blReport(null);
       byte[] rptData = reportOutput.RunReport(UserName, out outputMessage);

 

That's all for the Business Logic layer of our tutorial.

End of Part 3.

To be continued

(c) SoftPilot2000 Software Outsourcing Ukraine. All rights reserved.
Comments are closed on this post.