In this article I will explain with an example, how to open Crystal Report in new Tab (Window) in ASP.Net with C# and VB.Net.
The Crystal Report will be opened as PDF in new Browser Tab (Window) using HyperLink in ASP.Net with C# and VB.Net.
Database
Here I am making use of Microsoft’s Northwind Database. You can download it from here.
HTML Markup
The HTML Markup consists of a CrystalReportViewer control and a HyperLink to open Crystal Report as PDF in new Tab (Window) of the Browser.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src='<%=ResolveUrl("~/crystalreportviewers13/js/crviewer/crv.js")%>' type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="true"
Height="400" Width="600" BestFitPage="False" ToolPanelView="None" />
<br />
<a href = "View.aspx" target = "_blank">View PDF Report</a>
</form>
</body>
</html>
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
VB.Net
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Designing and populating the Crystal Report from Database
Inside the Page Load event, the Crystal Report is populated from database.
The DataSet used to populate the Crystal Report is saved in a Session variable which will be used on the other Page where the PDF of the Crystal Report will be opened in new Tab (Window) of the Browser.
C#
protected void Page_Load(object sender, EventArgs e)
{
ReportDocument crystalReport = new ReportDocument();
crystalReport.Load(Server.MapPath("~/CustomerReport.rpt"));
Customers dsCustomers = this.GetData("SELECT TOP 5 * FROM customers");
crystalReport.SetDataSource(dsCustomers);
CrystalReportViewer1.ReportSource = crystalReport;
Session["Customers"] = dsCustomers;
}
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;
}
}
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim 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
Session("Customers") = dsCustomers
End Sub
Private Function GetData(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
Opening Crystal Report in new Tab (Window) of Browser in ASP.Net
When the View PDF Report HyperLink is clicked, the View Page is opened in new Tab (Window) of the Browser.
Inside the Page Load event, the DataSet is fetched from the Session variable and the Crystal Report is displayed as PDF Report in Browser.
C#
protected void Page_Load(object sender, EventArgs e)
{
ReportDocument crystalReport = new ReportDocument();
crystalReport.Load(Server.MapPath("~/CustomerReport.rpt"));
Customers dsCustomers = (Customers)Session["Customers"];
crystalReport.SetDataSource(dsCustomers);
crystalReport.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, false, "Crystal");
Response.End();
}
VB.Net
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Dim crystalReport = New ReportDocument()
crystalReport.Load(Server.MapPath("~/CustomerReport.rpt"))
Dim dsCustomers As Customers = CType(Session("Customers"), Customers)
crystalReport.SetDataSource(dsCustomers)
crystalReport.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, False, "Crystal")
Response.End()
End Sub
Screenshot
Demo
Downloads