การแสดงรายงานด้วย Crstal report บน ASP.Net ถ้าคุณต้องการ Preview รายงาน บน PDF โดยไม่ใช้ CrystalReportViewer แต่จะใช ReportDocument Export ไปเป็น PDF แล้วเปิดบนเบราว์เวอร์
จากบนทความ: ASP.NET Crystal Reports Export PDF เราไปเพิ่มปุ่ม View in PDF แล้วใส่โค้ดดังนี้:
แล้วใส่โค้ดดังนี้
protected void btViewPDF_Click(object sender, EventArgs e)
{
string strConn = WebConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
string sql = “”;
{
string strConn = WebConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
string sql = “”;
if (txtCategory.Text != “” && txtProducts.Text != “”)
sql = ” SELECT * FROM Categories Where CategoryName Like ‘%” + txtCategory.Text.Trim() + “%’; ” +
” SELECT * FROM Products Where ProductName like ‘%” + txtProducts.Text.Trim() + “%’;”;
sql = ” SELECT * FROM Categories Where CategoryName Like ‘%” + txtCategory.Text.Trim() + “%’; ” +
” SELECT * FROM Products Where ProductName like ‘%” + txtProducts.Text.Trim() + “%’;”;
else if (txtCategory.Text != “”)
sql = ” SELECT * FROM Categories Where CategoryName Like ‘%” + txtCategory.Text.Trim() + “%’; ” +
” SELECT * FROM Products Where CategoryID in (SELECT CategoryID FROM Categories Where CategoryName Like ‘%” + txtCategory.Text.Trim() + “%’);”;
sql = ” SELECT * FROM Categories Where CategoryName Like ‘%” + txtCategory.Text.Trim() + “%’; ” +
” SELECT * FROM Products Where CategoryID in (SELECT CategoryID FROM Categories Where CategoryName Like ‘%” + txtCategory.Text.Trim() + “%’);”;
else if (txtProducts.Text != “”)
sql = ” SELECT * FROM Categories Where CategoryID in (SELECT CategoryID FROM Products Where ProductName like ‘%” + txtProducts.Text.Trim() + “%’); ” +
” SELECT * FROM Products Where ProductName like ‘%” + txtProducts.Text.Trim() + “%’”;
else
sql = ” SELECT * FROM Categories; ” +
” SELECT * FROM Products; “;
sql = ” SELECT * FROM Categories Where CategoryID in (SELECT CategoryID FROM Products Where ProductName like ‘%” + txtProducts.Text.Trim() + “%’); ” +
” SELECT * FROM Products Where ProductName like ‘%” + txtProducts.Text.Trim() + “%’”;
else
sql = ” SELECT * FROM Categories; ” +
” SELECT * FROM Products; “;
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(strConn);
conn.Open();
conn.Open();
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
{
CrystalDecisions.CrystalReports.Engine.ReportDocument rpt = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
rpt.Load(Server.MapPath(“CrystalReport.rpt”));
rpt.SetDatabaseLogon(“sa”, “enabler”, “.\\SQLEXPRESS”, “Northwind”);
rpt.Subreports[0].Database.Tables[0].SetDataSource(ds.Tables[1].DefaultView);
rpt.SetDatabaseLogon(“sa”, “enabler”, “.\\SQLEXPRESS”, “Northwind”);
rpt.Subreports[0].Database.Tables[0].SetDataSource(ds.Tables[1].DefaultView);
rpt.SetDataSource(ds.Tables[0].DefaultView);
rpt.SetParameterValue(“pmMain”, “รายงานสินค้าตาม ประเภทเภทสินค้า”);
rpt.SetParameterValue(“pmSub”, “รายงานสินค้า”, “product”);
rpt.SetParameterValue(“pmMain”, “รายงานสินค้าตาม ประเภทเภทสินค้า”);
rpt.SetParameterValue(“pmSub”, “รายงานสินค้า”, “product”);
this.Session["ReportSource"] = rpt;
CrystalReportViewer1.Visible = false;
ReportDocument repDoc = this.Session["ReportSource"] as ReportDocument;
Response.Buffer = false;
Response.ClearContent();
Response.ClearHeaders();
Response.Buffer = false;
Response.ClearContent();
Response.ClearHeaders();
repDoc.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, false, “ProductDetail”);
}
}
ระบุเงื่อไขเรียบร้อย แล้วกดปุ่ม View in PDF คุณจะได้รายงานแสดงบน PDF
No comments:
Post a Comment