Tuesday, December 29, 2009

Generates Crystal Report without using dataset in ASP.net

Here I m giving you a sample code for generating crystal report without using dataset.Do the following steps for generating report.....

1)Create Web form name as Default.aspx
Drag and drop button control on that form.


Write the following code on Default.aspx.cs

SqlConnection con = new SqlConnection("server=.;integrated security=true;database=WIFI");

SqlCommand com = new SqlCommand();
SqlDataReader dr;

protected void Button1_Click(object sender, EventArgs e)
{
com.CommandText = " Write the quary what u want to display on crystal report";
com.Connection = con;
con.Open();
dr = com.ExecuteReader();
dr.Close();
con.Close();
Response.Redirect("Default2.aspx");
}

2)Create second Web form named as Default2.aspx
Drag and drop CrystalReportViewer control on that




3)Then add Crystal Report from Add New Item named as CrystalReport.rpt
In Field Explrer add Database field
Right Click on Database field and select Database Expert
Then Select new Connection and make connection with OLEDB(ADO),With specify server and database name for proper connetion



4)Write the code on Default2.aspx.cs

using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;


protected void Page_Load(object sender, EventArgs e)
{
ReportDocument rd = new ReportDocument();
TableLogOnInfos tblinfos = new TableLogOnInfos();
TableLogOnInfo tblinfo = new TableLogOnInfo();
ConnectionInfo cn = new ConnectionInfo();
Tables tab;

cn.ServerName = ".";
cn.DatabaseName = "WIFI";
cn.IntegratedSecurity = true;

rd.Load(Server.MapPath("CrystalReport.rpt"));
tab = rd.Database.Tables;
foreach (CrystalDecisions.CrystalReports.Engine.Table crtab in tab)
{
tblinfo = crtab.LogOnInfo;
tblinfo.ConnectionInfo = cn;
crtab.ApplyLogOnInfo(tblinfo);
}

CrystalReportViewer1.HasDrillUpButton = false;
CrystalReportViewer1.HasExportButton = true;
CrystalReportViewer1.HasGotoPageButton = false;
CrystalReportViewer1.HasPageNavigationButtons = true;
CrystalReportViewer1.HasPrintButton = true;
CrystalReportViewer1.HasRefreshButton = true;
CrystalReportViewer1.HasSearchButton = false;
CrystalReportViewer1.HasToggleGroupTreeButton = false;
CrystalReportViewer1.DisplayGroupTree = false;
CrystalReportViewer1.HasViewList = false;
CrystalReportViewer1.HasZoomFactorList = false;
CrystalReportViewer1.HasCrystalLogo = false;

CrystalReportViewer1.ReportSource = rd;
CrystalReportViewer1.DataBind();
}

No comments:

Post a Comment