Hi eleveth,
Check this example. Now please take its reference and correct your code.
Refering below article i have created the example.
Print RDLC Report without Print Preview using JavaScript in ASP.Net
HTML
<%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<rsweb:ReportViewer ID="ReportViewer1" runat="server" Width="450" Height="250">
</rsweb:ReportViewer>
<br />
<asp:Button Text="Print PDF" runat="server" OnClick="Print" CommandName="PDF" />
<asp:Button Text="Print Word" runat="server" OnClick="Print" CommandName="Word" />
<asp:Button Text="Print Excel" runat="server" OnClick="Print" CommandName="Excel" />
</form>
</body>
</html>
Namespaces
C#
using System.Data;
using System.Web.UI.WebControls;
using Microsoft.Reporting.WebForms;
VB.Net
Imports System.Data
Imports Microsoft.Reporting.WebForms
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
ReportViewer1.ProcessingMode = ProcessingMode.Local;
ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc");
Customers dsCustomers = GetData();
ReportDataSource datasource = new ReportDataSource("Customers", dsCustomers.Tables[0]);
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(datasource);
}
}
private Customers GetData()
{
using (Customers dsCustomers = new Customers())
{
DataTable dt = dsCustomers.Tables["DataTable1"];
dt.Rows.Add("ALFKI", "Maria", "Boise", "Germany");
dt.Rows.Add("ANATR", "Ana Trujillo", "México D.F.", "Mexico");
dt.Rows.Add("ANTON", "Antonio Moreno", "Montréal", "Mexico");
dt.Rows.Add("AROUT", "Thomas Hardy", "Mannheim", "Sweden");
dt.Rows.Add("BERGS", "Christina Berglund", "Luleå", "Sweden");
return dsCustomers;
}
}
protected void Print(object sender, EventArgs e)
{
Warning[] warnings;
string[] streamIds;
string mimeType = string.Empty;
string encoding = string.Empty;
string extension = string.Empty;
string deviceInfo = "<DeviceInfo>"
+ "<OutputFormat>EMF</OutputFormat>"
+ "<PageWidth>8.5in</PageWidth>"
+ "<PageHeight>11in</PageHeight>"
+ "<MarginTop>0.25in</MarginTop>"
+ "<MarginLeft>0.25in</MarginLeft>"
+ "<MarginRight>0.25in</MarginRight>"
+ "<MarginBottom>0.25in</MarginBottom>"
+ "</DeviceInfo>";
byte[] bytes = ReportViewer1.LocalReport.Render((sender as Button).CommandName, deviceInfo, out mimeType, out encoding, out extension, out streamIds, out warnings);
Response.Buffer = true;
Response.Clear();
Response.ContentType = mimeType;
Response.AddHeader("content-disposition", "attachment; filename=Customers." + extension);
Response.BinaryWrite(bytes);
Response.Flush();
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
ReportViewer1.ProcessingMode = ProcessingMode.Local
ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc")
Dim dsCustomers As Customers = GetData()
Dim datasource As New ReportDataSource("Customers", dsCustomers.Tables(0))
ReportViewer1.LocalReport.DataSources.Clear()
ReportViewer1.LocalReport.DataSources.Add(datasource)
End If
End Sub
Private Function GetData() As Customers
Using dsCustomers As New Customers()
Dim dt As DataTable = dsCustomers.Tables("DataTable1")
dt.Rows.Add("ALFKI", "Maria", "Boise", "Germany")
dt.Rows.Add("ANATR", "Ana Trujillo", "México D.F.", "Mexico")
dt.Rows.Add("ANTON", "Antonio Moreno", "Montréal", "Mexico")
dt.Rows.Add("AROUT", "Thomas Hardy", "Mannheim", "Sweden")
dt.Rows.Add("BERGS", "Christina Berglund", "Luleå", "Sweden")
Return dsCustomers
End Using
End Function
Protected Sub Print(ByVal sender As Object, ByVal e As EventArgs)
Dim warnings As Warning()
Dim streamIds As String()
Dim mimeType As String = String.Empty
Dim encoding As String = String.Empty
Dim extension As String = String.Empty
Dim deviceInfo As String = _
"<DeviceInfo>" + _
" <OutputFormat>EMF</OutputFormat>" + _
" <PageWidth>8.5in</PageWidth>" + _
" <PageHeight>11in</PageHeight>" + _
" <MarginTop>0.5in</MarginTop>" + _
" <MarginLeft>0.5in</MarginLeft>" + _
" <MarginRight>0.5in</MarginRight>" + _
" <MarginBottom>0.5in</MarginBottom>" + _
"</DeviceInfo>"
Dim bytes As Byte() = ReportViewer1.LocalReport.Render((TryCast(sender, Button)).CommandName, deviceInfo, mimeType, encoding, extension, streamIds, warnings)
Response.Buffer = True
Response.Clear()
Response.ContentType = mimeType
Response.AddHeader("content-disposition", "attachment; filename=Customers." & extension)
Response.BinaryWrite(bytes)
Response.Flush()
End Sub