Preview Crystal Report in PDF format on Button Click using C# and VB.Net in ASP.Net

akhter
 
on Jan 08, 2021 06:19 AM
4711 Views

Crystal report is not previewing as your referred link below

Filter Crystal Report in ASP.Net using C# and VB.Net

        protected void btn_Print_Click(object sender, EventArgs e)
        {
            loadReport();
        }

        private void loadReport()
        {
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("SP_PackViewbyID_CRep_two", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@stPID", txtst.Text);
                cmd.Parameters.AddWithValue("@endPID", txtend.Text);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet dt = new DataSet();
                da.Fill(dt);



                rpt = new ReportDocument();
                //PackView.RefreshReport();
                rpt.Load(Server.MapPath("~/Pckrpt2.rpt"));
                rpt.SetDataSource(dt.Tables["Table"]);

                PackView.ReportSource = rpt;

html

<CR:CrystalReportViewer ID="PackView" runat="server" AutoDataBind="true"
    Height="400" Width="600" BestFitPage="False" ToolPanelView="None" />

But Displaying when i am using below code

rpt.ExportToHttpResponse(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, Response, false, "tbl_BalPacM");

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Jan 09, 2021 03:41 AM
on Jan 12, 2021 06:30 AM

Hi akhter,

Check this example. Now please take its reference and correct your code.

HTML

<asp:Button ID="btnExport" Text="Print" runat="server" OnClick="OnPrint" />
<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="true"
    Height="400" Width="600" BestFitPage="False" ToolPanelView="None" />
<hr />
<asp:Literal ID="ltEmbed" runat="server" />

Namespaces

C#

using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

VB.Net

Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared

Code

C#

ReportDocument crystalReport;
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        LoadReport();
    }
}
private void LoadReport()
{
    crystalReport = new ReportDocument();
    crystalReport.Load(Server.MapPath("~/CustomerReport.rpt"));
    Customers dsCustomers = this.GetData("SELECT TOP 5 * FROM customers");
    crystalReport.SetDataSource(dsCustomers);
    CrystalReportViewer1.ReportSource = crystalReport;
}
private Customers GetData(string query)
{
    string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    SqlCommand cmd = new SqlCommand(query);
    using (SqlConnection con = new SqlConnection(conString))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            cmd.Connection = con;

            sda.SelectCommand = cmd;
            using (Customers dsCustomers = new Customers())
            {
                sda.Fill(dsCustomers, "DataTable1");
                return dsCustomers;
            }
        }
    }
}

protected void OnPrint(object sender, EventArgs e)
{
     LoadReport();
     Stream stream = crystalReport.ExportToStream(ExportFormatType.PortableDocFormat);
     byte[] buf;
     buf = new byte[stream.Length];
     stream.Read(buf, 0, buf.Length);
     File.WriteAllBytes(Server.MapPath("~/Report.pdf"), buf);
     string embed = "<object data=\"{0}\" type=\"application/pdf\" width=\"500px\" height=\"300px\">";
     embed += "If you are unable to view file, you can download from <a href = \"{0}\">here</a>";
     embed += " or download <a target = \"_blank\" href = \"http://get.adobe.com/reader/\">Adobe PDF Reader</a> to view the file.";
     embed += "</object>";
     ltEmbed.Text = string.Format(embed, ResolveUrl("~/Report.pdf"));
}

VB.Net

Private crystalReport As ReportDocument
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        LoadReport()
    End If
End Sub
Protected Sub LoadReport()
    crystalReport = New ReportDocument()
    crystalReport.Load(Server.MapPath("~/CustomerReport.rpt"))
    Dim dsCustomers As Customers = Me.GetData("SELECT TOP 5 * FROM customers")
    crystalReport.SetDataSource(dsCustomers)
    CrystalReportViewer1.ReportSource = crystalReport
End Sub

Private Function GetData(ByVal query As String) As Customers
    Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Dim cmd As New SqlCommand(query)
    Using con As New SqlConnection(conString)
        Using sda As New SqlDataAdapter()
            cmd.Connection = con
            sda.SelectCommand = cmd
            Using dsCustomers As New Customers()
                sda.Fill(dsCustomers, "DataTable1")
                Return dsCustomers
            End Using
        End Using
    End Using
End Function

Protected Sub OnPrint(ByVal sender As Object, ByVal e As EventArgs)
    LoadReport()
    Dim stream As Stream = crystalReport.ExportToStream(ExportFormatType.PortableDocFormat)
    Dim buf As Byte()
    buf = New Byte(stream.Length - 1) {}
    stream.Read(buf, 0, buf.Length)
    File.WriteAllBytes(Server.MapPath("~/Report.pdf"), buf)
    Dim embed As String = "<object data=""{0}"" type=""application/pdf"" width=""500px"" height=""300px"">"
    embed += "If you are unable to view file, you can download from <a href = ""{0}"">here</a>"
    embed += " or download <a target = ""_blank"" href = ""http://get.adobe.com/reader/"">Adobe PDF Reader</a> to view the file."
    embed += "</object>"
    ltEmbed.Text = String.Format(embed, ResolveUrl("~/Report.pdf"))
End Sub