Answered by:
How to open crystal report in new tab of browser on button click in ".pdf" format

Question
-
User-343058236 posted
Curretly I'm able to print the crystal report in pdf form. This is my code to print my report in pdf form. Report name and path would be "D:\\Report_Payslip.pdf""
private void View_Click(object sender, EventArgs e) { DataTable dt = new DataTable(); using (SqlConnection conn = new SqlConnection(GetConnectionString())) { string sql = "SELECT * FROM employee_sal AS es,employee_info AS ei WHERE ei.employee_id = '" + TextBox1.Text + "' AND es.sal_month = '" + TextBox2.Text + "' AND es.sal_year = '" + TextBox3.Text + "'"; SqlCommand cmd = new SqlCommand(sql, conn); SqlDataAdapter adt = new SqlDataAdapter(cmd); cmd.Connection.Open(); adt.Fill(dt); Report_Payslip rp = new Report_Payslip(); string mfiledate = DateTime.Now.ToShortDateString(); string mfiletime = DateTime.Now.ToShortTimeString(); rp.SetDataSource(dt); ExportOptions CrExportOptions; DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions(); PdfRtfWordFormatOptions CrFormatTypeOptions = new PdfRtfWordFormatOptions(); CrDiskFileDestinationOptions.DiskFileName = "D:\\Report_Payslip.pdf"; CrExportOptions = rp.ExportOptions; { CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile; CrExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat; CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions; CrExportOptions.FormatOptions = CrFormatTypeOptions; } rp.Export(); } }
Here in the codeReport_Payslip rp = new Report_Payslip();
Report_Payslip is my crystal report of ".rpt" format.
I want to open this "Report_Payslip.rpt" file in new tab of browser in pdf form. Please help.
Monday, September 9, 2013 11:48 AM
Answers
-
User-762086073 posted
Write the following code after the line
rp.SetDataSource(dt);
string reportFilePath = Server.MapPath("Report_Payslip.rpt");
ReportDocument crReportDocument = new ReportDocument();
crReportDocument.Load(reportFilePath);
crReportDocument.SetDataSource(ds.Tables["Report_Payslip"]); // you can set data source as your convinient
string exportPath = "d:\\data\\Report_Payslip.pdf"; //where file will be exported
crReportDocument.ExportToDisk(ExportFormatType.PortableDocFormat, exportPath);
//Following code will display the pdf file in new page
Response.Redirect("ViewPDF.aspx?reportFile=" + exportPath);//after this code create a new page ViewPDF.aspx and open ViewPDF.aspx.cs write the following code under Page_Load event
if (!IsPostBack)
{
System.Net.WebClient client = new System.Net.WebClient();
Byte[] buffer = client.DownloadData(Request.QueryString["reportFile"]);if (buffer != null)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", buffer.Length.ToString());
Response.BinaryWrite(buffer);
}
}// hope this will be help full for you, reply
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, September 9, 2013 3:10 PM -
User-762086073 posted
please debug it and ensure that the data is comming from database to dataset or not.
and correct the code
private void View_Click(object sender, EventArgs e) { DataSet ds = new DataSet(); using (SqlConnection conn = new SqlConnection(GetConnectionString())) { string sql = "SELECT * FROM employee_sal AS es,employee_info AS ei WHERE ei.employee_id = '" + TextBox1.Text + "' AND es.sal_month = '" + TextBox2.Text + "' AND es.sal_year = '" + TextBox3.Text + "'"; SqlCommand cmd = new SqlCommand(sql, conn); SqlDataAdapter da = new SqlDataAdapter(cmd); cmd.Connection.Open(); da.Fill(ds, "Report_Payslip"); // debug and check here data is comming from database or not //Report_Payslip rp = new Report_Payslip(); string mfiledate = DateTime.Now.ToShortDateString(); string mfiletime = DateTime.Now.ToShortTimeString(); // rp.SetDataSource(ds); string reportFilePath = Server.MapPath("Report_Payslip.rpt"); ReportDocument crReportDocument = new ReportDocument(); crReportDocument.Load(reportFilePath); crReportDocument.SetDataSource(ds.Tables["Report_Payslip"]); string exportPath = "D:\\Report_Payslip.pdf"; //where file will be exported and ensure that the drive or folder shared or write permission crReportDocument.ExportToDisk(ExportFormatType.PortableDocFormat, exportPath); Response.Redirect("ViewPDF.aspx?reportFile=" + exportPath); } }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, September 11, 2013 1:07 PM
All replies
-
User-762086073 posted
Write the following code after the line
rp.SetDataSource(dt);
string reportFilePath = Server.MapPath("Report_Payslip.rpt");
ReportDocument crReportDocument = new ReportDocument();
crReportDocument.Load(reportFilePath);
crReportDocument.SetDataSource(ds.Tables["Report_Payslip"]); // you can set data source as your convinient
string exportPath = "d:\\data\\Report_Payslip.pdf"; //where file will be exported
crReportDocument.ExportToDisk(ExportFormatType.PortableDocFormat, exportPath);
//Following code will display the pdf file in new page
Response.Redirect("ViewPDF.aspx?reportFile=" + exportPath);//after this code create a new page ViewPDF.aspx and open ViewPDF.aspx.cs write the following code under Page_Load event
if (!IsPostBack)
{
System.Net.WebClient client = new System.Net.WebClient();
Byte[] buffer = client.DownloadData(Request.QueryString["reportFile"]);if (buffer != null)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", buffer.Length.ToString());
Response.BinaryWrite(buffer);
}
}// hope this will be help full for you, reply
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, September 9, 2013 3:10 PM -
User-343058236 posted
Dear frzkfrz,
in this line of code:
crReportDocument.SetDataSource(ds.Tables["Report_Payslip"]);
its underlining "ds" with red line which says "ds doesnot exist in the current context"
when i write "dt" i get underline in Tables.
Tuesday, September 10, 2013 5:52 AM -
User-762086073 posted
//write the following line
DataSet ds = new DataSet();
SqlDataAdapter da= new SqlDataAdapter(sqlCmd, sqlConn); // where sqlCmd is your SqlCommand to get data from table and sqlConn is your SqlConnection string
da = new SqlDataAdapter(sqlCmd, sqlConn);
da.Fill(ds, "Report_Payslip");crReportDocument.Load(reportFilePath);
crReportDocument.SetDataSource(ds.Tables["Report_Payslip"]);// and continue... to export as pdf and view pdf as described
Tuesday, September 10, 2013 6:10 AM -
User-343058236 posted
Now Im able to open the crystal report in pdf form on same tab (not in new tab).
But I am not getting the database data in the report.
This is the updated code which you said me to put :)
private void View_Click(object sender, EventArgs e) { DataSet ds = new DataSet(); using (SqlConnection conn = new SqlConnection(GetConnectionString())) { string sql = "SELECT * FROM employee_sal AS es,employee_info AS ei WHERE ei.employee_id = '" + TextBox1.Text + "' AND es.sal_month = '" + TextBox2.Text + "' AND es.sal_year = '" + TextBox3.Text + "'"; SqlCommand cmd = new SqlCommand(sql, conn); SqlDataAdapter da = new SqlDataAdapter(cmd); cmd.Connection.Open(); da.Fill(ds, "Report_Payslip"); Report_Payslip rp = new Report_Payslip(); string mfiledate = DateTime.Now.ToShortDateString(); string mfiletime = DateTime.Now.ToShortTimeString(); rp.SetDataSource(ds); string reportFilePath = Server.MapPath("Report_Payslip.rpt"); ReportDocument crReportDocument = new ReportDocument(); crReportDocument.Load(reportFilePath); crReportDocument.SetDataSource(ds.Tables["Report_Payslip"]); string exportPath = "D:\\Report_Payslip.pdf"; //where file will be exported crReportDocument.ExportToDisk(ExportFormatType.PortableDocFormat, exportPath); Response.Redirect("ViewPDF.aspx?reportFile=" + exportPath); }
Wednesday, September 11, 2013 4:38 AM -
User-762086073 posted
please debug it and ensure that the data is comming from database to dataset or not.
and correct the code
private void View_Click(object sender, EventArgs e) { DataSet ds = new DataSet(); using (SqlConnection conn = new SqlConnection(GetConnectionString())) { string sql = "SELECT * FROM employee_sal AS es,employee_info AS ei WHERE ei.employee_id = '" + TextBox1.Text + "' AND es.sal_month = '" + TextBox2.Text + "' AND es.sal_year = '" + TextBox3.Text + "'"; SqlCommand cmd = new SqlCommand(sql, conn); SqlDataAdapter da = new SqlDataAdapter(cmd); cmd.Connection.Open(); da.Fill(ds, "Report_Payslip"); // debug and check here data is comming from database or not //Report_Payslip rp = new Report_Payslip(); string mfiledate = DateTime.Now.ToShortDateString(); string mfiletime = DateTime.Now.ToShortTimeString(); // rp.SetDataSource(ds); string reportFilePath = Server.MapPath("Report_Payslip.rpt"); ReportDocument crReportDocument = new ReportDocument(); crReportDocument.Load(reportFilePath); crReportDocument.SetDataSource(ds.Tables["Report_Payslip"]); string exportPath = "D:\\Report_Payslip.pdf"; //where file will be exported and ensure that the drive or folder shared or write permission crReportDocument.ExportToDisk(ExportFormatType.PortableDocFormat, exportPath); Response.Redirect("ViewPDF.aspx?reportFile=" + exportPath); } }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, September 11, 2013 1:07 PM -
User-578610739 posted
Hi,
While exporting you can open with window.open script from your code. please check link.
http://forumarray.com/how-to-open-crystal-report-in-a-new-window-504791
Thursday, September 12, 2013 2:01 AM -
User-343058236 posted
All good till now, just one last query.
pdf is not opening in new tab, it opens on the current tab
Thursday, September 12, 2013 3:15 AM -
User-762086073 posted
Look ViewPDF.aspx.cs file and try to open file in new tab by using the following code under Page_Load event .
form1.Target = "_blank";
Thursday, September 12, 2013 6:19 AM -
User-343058236 posted
form1.Target = "_blank";Nops. This one is not opeing the pdf in new tab. Neway thanx alot for your help :)
Atleast now im able to open pdf in tabThursday, September 12, 2013 8:35 AM