DotNet Academy of Rajesh Rolen

Solutions by Rajesh Rolen

Crystal Report in Asp.net by code

1. right click on project in solution explorer and add new dataset in project.
2. create table on this dataset with field which you want to show in report.
3. from solution explorer add new crystal report and in wizard of crystal report select that dataset and table which you just created for report
4. add new form or take an existing form in project and add crystal report viewer on that form in which we will show our report.
5. write below code in crystal report viewer's form
//header file need to include
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine ;

//code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindReport();

CrystalReportViewer1.ShowFirstPage();
}
}
private void BindReport()
{ string servername= System.Configuration.ConfigurationManager.AppSettings["servername"].ToString();
string databasename=ConfigurationManager.AppSettings["databasename"].ToString();
string userid=ConfigurationManager.AppSettings["userid"].ToString();
string password=ConfigurationManager.AppSettings["password"].ToString();


CrystalDecisions.Shared.TableLogOnInfo mylogin = new TableLogOnInfo();
CrystalReportFormSaleInquiry crinq = new CrystalReportFormSaleInquiry();
//object of business logic layer to get data from database
BLL.BLLMIS.BLLFormSale formsale = new KIDZEE.BLL.BLLMIS.BLLFormSale();
//to get data from database in datatable
DataTable dtrpt = formsale.SelectMISformsale(new Guid(Session["inquiryid"].ToString()));

foreach (CrystalDecisions.CrystalReports.Engine.Table tblrpt in crinq.Database.Tables)
{
mylogin=tblrpt.LogOnInfo;
mylogin.ConnectionInfo.DatabaseName=databasename;
mylogin.ConnectionInfo.ServerName=servername;
mylogin.ConnectionInfo.UserID=userid;
mylogin.ConnectionInfo.Password=password;
tblrpt.ApplyLogOnInfo(mylogin);
tblrpt.SetDataSource(dtrpt);
CrystalReportViewer1.ReportSource = crinq;
//exporting Report to Pdf
MemoryStream oStream; // using System.IO
oStream = (MemoryStream)
crinq.ExportToStream(
CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/pdf";
Response.BinaryWrite(oStream.ToArray());
Response.End();
}

0 comments:

Post a Comment

About this blog

My Blog List

Advertise On This Site

Site Info

Advertise on this Site

To advertise on this site please mail on RajeshRolen@gmail.com

Information Source

About

Pages

Dot Net Academy

Advertis in This Area of Site

Powered by Blogger.

Followers

Search This Blog