Here, we will learn how to create and bind crystal reports in asp.net without use of any database. we generally use database for bind data to crystal reports, but in this example i will show you how to bind dynamically data to crystal reports at runtime. we will use here dataset method for bind data to crystal reports in asp.net.
First create asp.net web application and right click on Project >> Add New Item >> DataSet
– Add new Dataset to asp.net web application for bind dynamically crystal reports.
– Add New DataTable here for make dynamically columns for data. Add new DataTable in DataSet.
– After adding new DataTable add some columns which you want to bind in crystal reports. add some columns shows like below screen.
– do same process for add new columns in DataTable.
– Here, we took three columns ID, Name, City in this asp.net example.
– After creating DataTable in Dataset, now create new crystal reports for display data in asp.net.
– go to Field Explorer >> Database Field >> Database Expert >> Project Data >> ADO.Net DataSets
– Design the Crystal Reports with columns from Database Filed. drag some columns in crystal reports details section for display crystal reports in asp.net.
now, add new crystalreportviewer on asp.net web forms for display crystal reports.
write below code for bind data to crystal reports in asp.net.
C# Code
protected void Page_Load(object sender, EventArgs e)
{
DataSet1 ds = new DataSet1();
DataTable tbll = ds.Tables.Add(“Items”);
tbll.Columns.Add(“id”, Type.GetType(“System.Int32”));
tbll.Columns.Add(“Name”, Type.GetType(“System.String”));tbll.Columns.Add(“City”, Type.GetType(“System.String”));
DataRow rowss;
int i = 1;
for (i = 1; i <= 9; i++)
{
rowss = tbll.NewRow();
rowss[“id”] = i;
rowss[“Name”] = “Name” + i;
rowss[“City”] = “City” + i;
tbll.Rows.Add(rowss);
}ReportDocument rept = new ReportDocument();
rept.Load(Server.MapPath(“~/CrystalReport2.rpt”));
rept.SetDataSource((DataTable)ds.Tables[1]);
CrystalReportViewer1.ReportSource = rept;}
VB.Net Code
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim ds As DataSet1 = New DataSet1()
Dim tbll As DataTable = ds.Tables.Add(“Items”)
tbll.Columns.Add(“id”, Type.GetType(“System.Int32”))
tbll.Columns.Add(“Name”, Type.GetType(“System.String”))
tbll.Columns.Add(“City”, Type.GetType(“System.String”))Dim rowss As DataRow
Dim i As Integer = 1
For i = 1 To 9 Step i + 1
rowss = tbll.NewRow()
rowss(“id”) = i
rowss(“Name”) = “Name” + i.ToString()
rowss(“City”) = “City” + i.ToString()
tbll.Rows.Add(rowss)
NextDim rept As ReportDocument = New ReportDocument()
rept.Load(Server.MapPath(“~/CrystalReport2.rpt”))
rept.SetDataSource(CType(ds.Tables(1), DataTable))
CrystalReportViewer1.ReportSource = reptEnd Sub
– Here is the out put of dynamically bind data to crystal reports in asp.net using c#.