Here, we learn how to create crystal reports in asp.net, and how to bind data to crystal reports from sql server database. here we are using dataset method for bind data to crystal reports in ASP.Net.
For binding data to crystal reports, first we must need to create a new database in sql server and create a table and a select stored procedure in database. after finishing this we have to make connection between asp.net web application and sql server using Dataset method.
Here, first we create new Database in sql server and create new Table within a Database.
– Create a new Table “StuMSt” in SQL – server with some columns.
– Create a SELECT Stored Procedure in sql server for select data.
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GOALTER PROCEDURE [dbo].[STUDENT_SELECT]
AS
BEGINSELECT * FROM STUMST
END
Now, create a new asp.net web application for bind crystal report.
After, create a asp.net web application we have to make connection between asp.net web application and sql server database. we are using dataset method for asp.net connection.
Create New DataSet in asp.net web application in Solution Explorer.
Solution Explorer >> Right Click on Project >> Add New Item >> DataSet
Now, finishing dataset binding we create a new crystal reports. for Display crystal reports on web form we have to take a CrystalReportViewer from toolbox on web asp.net web form.
Now, Create a New Crystal Reports in asp.net web application.
Solution Explorer >> Right Click on Project >> Add New Item >> CrystalReport
– There are main five section in Crystal Reports.
1. Report Header : Report Header Generally used to Display Heading/Title of the Reports.
2. Page Header : Page Header used to Display Content/Data Heading or Title.
3. Detail : Detail used to Display the Data/Content of your Reports.
4. Report Footer : Report Footer used to Display Footer of the Reports.
5. Page Footer : Page Footer used to Display Page Number of the Reports.
– here is code for bind crystal report.
C# Code
– first add name space : using CrystalDecisions.CrystalReports.Engine;
protected void Page_Load(object sender, EventArgs e)
{
DataSet1.STUDENT_SELECTDataTable StuDT = new DataSet1.STUDENT_SELECTDataTable();
DataSet1TableAdapters.STUDENT_SELECTTableAdapter StuAdapter = new DataSet1TableAdapters.STUDENT_SELECTTableAdapter();StuDT = StuAdapter.Select();
ReportDocument rept = new ReportDocument();
string spath = “E:/BLOG/RExample/CrystalReport.rpt”;
rept.Load(spath);
rept.SetDataSource((DataTable)StuDT);
CrystalReportViewer1.ReportSource = rept;}
VB.Net Code
Import namespace : Imports CrystalDecisions.CrystalReports.Engine
Dim StuDT As DataSet1.STUDENT_SELECTDataTable = New DataSet1.STUDENT_SELECTDataTable()
Dim StuAdapter As DataSet1TableAdapters.STUDENT_SELECTTableAdapter = New DataSet1TableAdapters.STUDENT_SELECTTableAdapter()StuDT = StuAdapter.Select()
Dim rept As ReportDocument = New ReportDocument()rept.Load(Server.MapPath(“~/CrystalReport.rpt”))
rept.SetDataSource(CType(StuDT, DataTable))
CrystalReportViewer1.ReportSource = rept