How to display record in Crystal Report?
In this article ,I am explaining about how to use crystal report in ASP.Net for bind data base record details. We can bind data in crystal report with help of xml tags or xml data set, here I have used XML dataset for bind records with crystal report.
How to create XML dataset with SQL Server table details?
Create dataset: Right Click your project name -> ADD new Item ->select dataset (DataSet1.xsd)
Then right click of that dataset and add new column (for your field names, types) as same like in the SQL Server table field names. .
How to connect dataset and CrystalReport Page in ASP.Net?
Right Click your project name -> ADD new Item ->Choose Crystal report and name (like this CrystalReport1.rpt).
Choose Field explorer in Left side and right click database Field -> Choose Database expert->choose new ADO.Net connection in tree and choose your dataset path and click finish.
Then your fields are displayed in field explorer drag and drop in to crystal report.
How to bind records in crystal report?
In this application I collect individual student id (or all) from client show all record details in the crystal report.
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using CrystalDecisions.Web;
using CrystalDecisions.ReportSource;
public partial class Default2 : System.Web.UI.Page
{
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ConnectionString);
SqlCommand sqlcmd;
SqlDataAdapter da;
DataTable dt = new DataTable();
string sid,query;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
sid = Request.QueryString["sid"];
try
{
sqlcon.Open();
//Check whether query string have value or not if not have value then select all record in below query otherwise select particular record with help of where condition
if(sid=="" || sid==null)
{
query="select * from student";
}
else
{
query="select * from student where sid='" + sid +"'";
}
sqlcmd = new SqlCommand(query,sqlcon);
da = new SqlDataAdapter(sqlcmd);
dt.Clear();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
ReportDocument RptDoc = new ReportDocument();
RptDoc.Load(Server.MapPath("~/CrystalReport.rpt"));
RptDoc.SetDataSource(dt);
CrystalReportViewer1.ReportSource = RptDoc;
CrystalReportViewer1.DataBind();
}
}
catch(Exception ex)
{
Response.Write(ex.ToString());
}
finally
{
sqlcon.Close();
}
}
}
}
Source Code Detail:
Here with I have attached source code to show student detail in crystal report. Download it and try to learn crystal report concepts.
Front End : ASP.NET
Code Behind : C#
Conclusion:
I hope that this Article is help to you understand crystal report concepts.
Hi, i have a problem for crystal report id generate i used id generator function code but it generate only one or two records then stop. Please help me that one.