We will first download the SAP Crystal report from the source, and install it on our system, then with the help of some simple code we will go ahead.
Initial chamber
Firstly, download the SAP Crystal report for respective IDE’s of Visual Studio,
- For Visual Studio 2010
- For Visual Studio 2013
Step 1: Open Visual Studio 2010 and create an Empty Website. Give a suitable name [crystalrpt_demo].
Step 2: In Solution Explorer you get your empty website, and then add a Web Form and SQL Server Database and Dataset. By following these steps:
For Web Form:
crystalrpt_demo(Your Empty Website) - Right Click, Add New Item, Web Form. Name it cry talrpt_demo.aspx.
For SQL Server Database:
crystalrpt_demo (Your Empty Website) - Right Click, Add New Item - SQL Server Database. Add Database inside the App_Data_folder.
For DataSet:
crystalrpt_demo (Your Empty Website) - Right Click, Add New Item, DataSet. Add DataSet inside the App_Code_folder.
Database chamber
Step 3: In Server Explorer, Click on your Database [Database.mdf] - Tables, Add New Table and form a table like this:
Table - tbl_data (Don’t Forget to make ID as IS Identity - True)
Insert some data inside your table by going to your table - tbl_data - Right Click and Show tbl_data. Add data in the table.
Store Procedure - sp_select :
Double Click on the dataset that resides under App_code_Folder. See the following image:
When you double click the dataset you get the blank area, there you have to do something like the following:
Right Click on the blank area -Add - Datatable.
Change the name of table from Datatable1 to Newtbl_data, there you have to add 3 columns for ID, Name, City as we had taken in tbl_data. So add three Columns. You will get something like the following image.
After this process you have to go the crystalrpt_demo- Right Click, Add New Item and find Crystal Report.rpt file.
Crystal Report Gallery will open like the following:
You got something like this:
When your crystal report opens up like the above image, then you can find in the left or right dock. Field Explorer opens up.
Right Click on Field Explorer - Database Expert, Create New Connection and you have to find the table that we had made in DataSet i.e - Newtbl_data.
Once you got your table - Add that table to right hand side pane using “ >>” button and Press OK. See the following image:
You will get your Newtbl_data in Field Explorer like the following:
Here you have to add these ID, Name and City from Database Fields to the Crystal Report - Section 3 Page Details. You can drag these fields to the Crystal Report Page Details.
Design chamber
Step 4: Now open your crystalrpt_demo.aspx file, where we create our design for crystal report, We will add Crystal Report Viewer here.
Go to the toolbox and find Reporting - Drag and Drop Crystal Report Viewer.
crystalrpt_demo.aspx
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
-
- <%@ Register assembly="CrystalDecisions.Web, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" namespace="CrystalDecisions.Web" tagprefix="CR" %>
-
- <!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>
-
- <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server"
- AutoDataBind="true" />
-
- </div>
- <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
- Text="Make it PDF" />
- </form>
- </body>
- </html>
Code chamber Step 5: Open your crystalrpt_demo.aspx.cs and write some code so that our application starts working.
Add some Namespaces to crystalrpt_demo.aspx.cs page.
- 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 System.Data.SqlClient;
- using CrystalDecisions.CrystalReports.Engine;
- using CrystalDecisions.Reporting;
- using CrystalDecisions.Shared;
- using System.IO;
-
- public partial class _Default : System.Web.UI.Page
- {
- SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
- ReportDocument rprt = new ReportDocument();
- string filepath;
- protected void Page_Load(object sender, EventArgs e)
- {
-
- }
-
-
- protected void Button1_Click(object sender, EventArgs e)
- {
- rprt.Load(Server.MapPath("~/CrystalReport.rpt"));
- rprt.FileName = Server.MapPath("~/CrystalReport.rpt");
- SqlCommand cmd = new SqlCommand("sp_select", con);
- SqlDataAdapter sda = new SqlDataAdapter(cmd);
- DataSet ds = new DataSet();
- sda.Fill(ds,"Newtbl_data");
- rprt.SetDataSource(ds);
- CrystalReportViewer1.ReportSource = rprt;
- CrystalReportViewer1.DataBind();
- pdfdemo();
- }
-
- public void pdfdemo()
- {
-
- Response.Clear();
- filepath = Server.MapPath("~/" + "demo.pdf");
- rprt.ExportToDisk(ExportFormatType.PortableDocFormat, filepath);
- FileInfo fileinfo = new FileInfo(filepath);
- Response.AddHeader("Content-Disposition", "inline;filenam=demo.pdf");
- Response.ContentType = "application/pdf";
- Response.WriteFile(fileinfo.FullName);
-
-
-
- }
-
- }
Output chamber Hope you liked it. Thank you for Reading. Have a good day.