Display Report as PDF in browser without showing ASP.Net Crystal Report using C# and VB.Net

on Aug 01, 2021 09:40 PM


I make a invoice and i created it print using crystal report asp.net.

But i want to load direct pdf using crystal report as pdf in browser. it can easily to print without pdf export or download.

This is my design 

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="hp_print.aspx.vb" Inherits="hp_web.hp_print" %>

<%@ Register assembly="CrystalDecisions.Web, Version=13.0.3500.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" namespace="CrystalDecisions.Web" tagprefix="CR" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <form id="form1" runat="server">
        <asp:Label ID="lblref" runat="server" Text="lblref" Visible="False"></asp:Label>
        <br />
        <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="True" GroupTreeImagesFolderUrl="" Height="50px" ReportSourceID="cr_hp_src" ToolbarImagesFolderUrl="" ToolPanelWidth="200px" Width="350px" ToolPanelView="None" PrintMode="ActiveX" />
        <CR:CrystalReportSource ID="cr_hp_src" runat="server">
            <Report FileName="print\hp_bill.rpt">

This is my vb.net code of load report

Imports System.Data.SqlClient
Public Class hp_print
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Me.IsPostBack Then
            Dim ref As String = Request.QueryString("ref")
            lblref.Text = ref
        End If

        Dim rpt As New hp_bill 'The report you created.
        Dim myCon As SqlConnection
        Dim myAdapter1 As SqlDataAdapter
        Dim myAdapter2 As SqlDataAdapter
        Dim myAdapter3 As SqlDataAdapter
        Dim myAdapter4 As SqlDataAdapter
        Dim myAdapter5 As SqlDataAdapter
        Dim myDataset As New hp_data 'The DataSet you created.
        myCon = New SqlConnection(cs)
        myAdapter1 = New SqlDataAdapter("select * from tblhp where ref='" & lblref.Text & "'", myCon)
        myAdapter2 = New SqlDataAdapter("select * from tblhpitems", myCon)
        myAdapter3 = New SqlDataAdapter("select * from tblcus", myCon)
        myAdapter4 = New SqlDataAdapter("select * from tblcom", myCon)
        myAdapter5 = New SqlDataAdapter("select * from tblitem", myCon)
        myAdapter1.Fill(myDataset, "tblhp")
        myAdapter2.Fill(myDataset, "tblhpitems")
        myAdapter3.Fill(myDataset, "tblcus")
        myAdapter4.Fill(myDataset, "tblcom")
        myAdapter5.Fill(myDataset, "tblitem")
        CrystalReportViewer1.ReportSource = rpt
        CrystalReportViewer1.Visible = True
    End Sub
    Protected Sub CrystalReportViewer1_Init(sender As Object, e As EventArgs) Handles CrystalReportViewer1.Init

    End Sub
End Class


Download FREE API for Word, Excel and PDF in ASP.Net: Download
on Aug 02, 2021 02:54 AM
on Feb 03, 2022 10:14 AM

Hi supun.d.kaduw...,

Refer below code.


For this example I have used of Northwind database that you can download using the link given below.

Download Northwind Database


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



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


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



protected void Page_Load(object sender, EventArgs e)
    if (!IsPostBack)
        ReportDocument crystalReport = new ReportDocument();
        Customers dsCustomers = this.GetData("SELECT TOP 5 * FROM customers");
        CrystalReportViewer1.ReportSource = crystalReport;
        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"));
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 Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    If Not IsPostBack Then
        Dim crystalReport As ReportDocument = New ReportDocument()
        Dim dsCustomers As Customers = Me.GetData("SELECT TOP 5 * FROM customers")
        CrystalReportViewer1.ReportSource = crystalReport
        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 If
End Sub

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