-->

13 March 2020

Crystal-Reports CrossTab-Report in Asp.Net

  Asp.Net CS By Example       13 March 2020
 Crystal-Reports CrossTab-Report in Asp.Net 
  In this post Crystal Reports CrossTab-Report Report, we learn on how to create a CrossTab-Report using Crystal Reports in asp.net.
 The Crosstab report is a Matrix kind of a report,allow we to view information in one column in information and in another column and then display the summary value of third column.If you don't kwon how to add crystal-reports and link dataset to crystal-reports to known more Click Here.

Now let us create a Typed Dataset with a DataTable to hold the values which will be used to create the Report. The Typed Dataset will look as follows.
Dataset

The CrossTab report can be added only to a ReportHeader or ReportFooter section.To add CrossTab in crystal-reports right click in ReportHeader or ReportFooter section and follow below steps:

Now create to CrossTab in report follow below steps.
  => Right click in 'ReportHeader' Section.
  => select 'Insert' Menu
  => select 'Cross-Tab' Menu.
CrossTab menu option.png

  => Now new Screen will be open.
  => In this screen 'Cross-Tab-Expert' open.
  => Now select Row,Column and Summary Field and after Click on 'OK' button.
Cross-Tab-Expert.png

After Above step done then our crystal report will be look like below image.
Cross-Tab-Report-Design.png

 Now we degin here Page for Call CrossTab crystal report. We have fetch data from database and Connection String define in web.config. How to read connection string string from web.config Click Here.
  Code: FrmCrossTabReportDemo.aspx  
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FrmCrossTabReportDemo.aspx.cs" 
Inherits="ProjCrystalReport.FrmCrossTabReportDemo" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h1>Crystal Report's Sub-Report  Example</h1>
        <asp:Button ID="btnDownload" runat="server" Text="DownLoad Pdf" 
            onclick="btnDownload_Click"  />
    </div>
    </form>
</body>
</html>

  Code: FrmCrossTabReportDemo.aspx.cs  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using CrystalDecisions.CrystalReports.Engine;
using System.IO;
using System.Configuration;
using System.Data.OracleClient;

namespace ProjCrystalReport
{
    public partial class FrmCrossTabReportDemo : System.Web.UI.Page
    {
        static string strConn = ConfigurationManager.ConnectionStrings["LearnAsp"].ToString();
        private OracleConnection objConn = new OracleConnection(strConn);
        
        protected void btnDownload_Click(object sender, EventArgs e)
        {
            DataTable tbEmp = new DataTable();
            DataSet DsEmp = new DataSet();
            String query = " select job,sal,ename from dept ";
            OracleCommand objCmd = new OracleCommand(query, objConn);
            objConn.Open();
            OracleDataAdapter oda = new OracleDataAdapter(objCmd);
            oda.Fill(tbEmp);
            objConn.Close();

            tbEmp.TableName = "dtEmp";
            DsEmp.Tables.Add(tbEmp.Copy());
            GenerateEmpReport(DsEmp);
        }
        public void GenerateEmpReport(DataSet EmpDetRpt)
        {
            String ReportPath = "";
            String ExportPath = "";
            String PDFNAME = "CrossTabReportDemo" + DateTime.Now.ToString("ddMMyyyyhhmmss") + ".pdf";
            String creatfpdf = Server.MapPath(@"~/GarbageBin/");
            var finalPDF = System.IO.Path.Combine(creatfpdf, PDFNAME);

            ReportPath = Server.MapPath("~\\CrtCrossTab.rpt");
            ExportPath = Server.MapPath("~\\GarbageBin\\") + PDFNAME;

            String[] Parameter = new String[1];
            var ParameterVal = new dynamic[1];
            Parameter[0] = "printby";
            ParameterVal[0] = "abc";

            ShowReport(EmpDetRpt, ReportPath, ExportPath, Parameter, ParameterVal);
            System.Net.WebClient client = new System.Net.WebClient();
            Byte[] buffer = client.DownloadData(finalPDF);

            if (buffer != null)
            {
                Response.ContentType = "application/pdf";
                Response.AddHeader("content-length", buffer.Length.ToString());
                Response.BinaryWrite(buffer);
                Response.End();
            }           
        }

        public static void ShowReport(DataSet Ds, String RptPath, String ExportPath, String[] Parameter, dynamic ParameterValue)
        {
            ReportDocument rpt = new ReportDocument(); 
            rpt.Load(RptPath);
            rpt.SetDataSource(Ds);

            for (int i = 0; i < Parameter.Length; i++)
            {
                rpt.SetParameterValue(Parameter[i], ParameterValue[i]);
            }

            rpt.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, ExportPath);
            rpt.Close();
            rpt.Dispose();
        }
    }
}

Output
Cross-Tab-Report.png

  That's it we have created a Cross Tab Report using Crystal Reports and displayed it within browser in an Asp.Net page.
logoblog

Thanks for reading Crystal-Reports CrossTab-Report in Asp.Net

Previous
« Prev Post

No comments:

Post a Comment

Please do not enter any spam link in the comment box.