-->

08 March 2020

Crystal-Reports in Asp.Net

  Asp.Net CS By Example       08 March 2020

 Crystal-Reports in Asp.Net 
  In this post Crystal Reports , we shall learn on how to create a Crystal Reports in visual studio. A Crystal Report is used to display data and export it in pdf, excel etc format.
  In this crystal report we are going display employee details.Now let us create a Typed Dataset with a DataTable to hold the values which will be used to create the crystal Report.
To create Dataset follow below steps:
  => Right click On Project.
  => select 'Add' Menu
  => select 'New Item' Menu
  => select 'Data' Setcion in Visual C#.
  => Now Select 'Data set' form Windows.
  => Give proper Name to Dataset E.g. 'DsEmpDetails.xsd'
  => And Last step is click On 'Add' Button.

DsEmpDetails.xsd
Dataset

Now to create datatable in dataset follow below steps:
   => Right click inside Dataset .
   => select 'Add' popup Menu.
   => select 'Datatable' option.
Datatable Add In DataSet

   => Now empty datatable with default name 'DataTable1'. By double click on it or pressing 'F2' shortcut key we can change this name.
   => Now add column to datatable right click on it and select 'Add' option from popup menu. Under 'Add' option select 'Column' option or press 'Ctrl+L' ShortCut Key.
Add Column to datatable

   => Now 'DataColumn1' is column will be created in datatable. this column name we can change.
   => Now to set the data type for this column.Right on that column and select 'Properties' option from popup option or press 'Alt+Enter' ShortCut key.
   => In Properties Windows we can set data type our column from 'DataType' dropdown.We can select System.Boolean,System.String,System.Single etc.
   => After adding we wanted column in datatable save it.

Datatable add column and set its datatype


The Typed Dataset will look as follows.
Datatable

Now add a new CrystalReport file to the Project follow below steps:
   => Right click On Project.
   => select 'Add' Menu
   => select 'New Item' Menu
   => select 'Reporting' Setcion in Visual C#.
   => Now Select 'Crystal Report' form Windows.
   => Give proper Name to Crystal Report E.g. 'crtEmpDetials.rpt'
   => And click On 'Add' Button.
Crystal Report add in asp.net


   => Now 'Crystal Report Gallery' Windows will open. From This screen Select below option and click on 'OK' button.
Crystal Report Gallery

   => Now 'Standard Report Creation Wizard' will be open.
   => In this screen. select Project Data Option.
   => 1sty right click on it and select Refresh option.
   => now Expand it and select 'Ado.Net Datasets' option.
   => Now exppand 'Ado.Net Datasets' and select our Dataset.
   => Now exppand 'our Dataset' and select our Datatable E.g.(dtEmpDetails).
   => Select Our Datatable E.g.(dtEmpDetails) and click on '>' Button for select datatable on crystal Report.
   => And at last click on 'Finish' Button.

 Open the Crystal Report and design the report as required using the fields added in the Field Expert.

Crystal Report Designing

 Now we degin here Page for Call 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: FrmCrystalReportDemo.aspx  
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FrmCrystalReportDemo.aspx.cs" 
Inherits="LearnAsp.Net.CrystalReports.FrmCrystalReportDemo" %>

<!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 Example</h1>
        <asp:Button ID="Button1" runat="server" Text="Export Crystal Report" 
            onclick="Button1_Click" />
    </div>
    </form>
</body>
</html>

  Code: FrmCrystalReportDemo.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.Data.OracleClient;
using System.Configuration;

namespace LearnAsp.Net.CrystalReports
{
    public partial class FrmCrystalReportDemo : System.Web.UI.Page
    {
        static string strConn = ConfigurationManager.ConnectionStrings["LearnAsp"].ToString();
        private OracleConnection objConn = new OracleConnection(strConn);

        protected void Button1_Click(object sender, EventArgs e)
        {
            GetData();
        }

        protected void GetData()
        {
            String query = " select empno,ename,job,sal from emp ";
            OracleCommand objCmd = new OracleCommand(query, objConn);
            DataTable dt = new DataTable();
            objConn.Open();
            OracleDataAdapter oda = new OracleDataAdapter(objCmd);
            oda.Fill(dt);
            objConn.Close();

            if (dt.Rows.Count > 0)
            {
                GenerateReport(dt);
            }
        }

        public void GenerateReport(DataTable EmpDetRpt)
        {
            String ReportPath = "";
            String ExportPath = "";
            String PDFNAME = "EmpDetails" + DateTime.Now.ToString("ddMMyyyyhhmmss") + ".pdf";
            String creatfpdf = Server.MapPath(@"~/GarbageBin/");
            var finalPDF = System.IO.Path.Combine(creatfpdf, PDFNAME);

            ReportPath = Server.MapPath("~\\CrystalReports\\crtEmpDetials.rpt");
            ExportPath = Server.MapPath("..\\GarbageBin\\") + PDFNAME;
            ShowReport(EmpDetRpt, ReportPath, ExportPath);

            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(DataTable DTbl, String RptPath, String ExportPath)
        {
            ReportDocument rpt = new ReportDocument();
            rpt.Load(RptPath);
            rpt.SetDataSource(DTbl);
            rpt.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, ExportPath);
            rpt.Close();
            rpt.Dispose();
        }
    }
}

Output
Crystal Report Export to pdf

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

Thanks for reading Crystal-Reports in Asp.Net

Previous
« Prev Post

No comments:

Post a Comment

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