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