Hi priyajsr,
I have created sample by refering the below article.
Print RDLC Report without Print Preview using JavaScript in ASP.Net
HTML
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<rsweb:ReportViewer ID="ReportViewer1" runat="server" Width="450" Height="250">
</rsweb:ReportViewer>
<br />
<asp:Button Text="Send" runat="server" OnClick="Email" />
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 Email(object sender, EventArgs e)
{
using (MailMessage mm = new MailMessage("sender@gmail.com", "receiver@gmail.com"))
{
mm.Subject = "RDLC Report PDF example";
mm.Body = "RDLC Report PDF example";
mm.Attachments.Add(new Attachment(ExportReportToPDF(Server.MapPath("~/Files/"), "Invoice.pdf")));
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
NetworkCredential credential = new NetworkCredential();
credential.UserName = "sender@gmail.com";
credential.Password = "xxxxx";
smtp.UseDefaultCredentials = true;
smtp.Credentials = credential;
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.Send(mm);
}
}
// Generate repord in a path
private string ExportReportToPDF(string path, string reportName)
{
Warning[] warnings;
string[] streamids;
string mimeType;
string encoding;
string filenameExtension;
byte[] bytes = ReportViewer1.LocalReport.Render("PDF", null, out mimeType, out encoding, out filenameExtension, out streamids, out warnings);
string filename = path + reportName;
using (var fs = new System.IO.FileStream(filename, System.IO.FileMode.Create))
{
fs.Write(bytes, 0, bytes.Length);
fs.Close();
}
return filename;
}
VB.Net
Partial Class VB
Inherits System.Web.UI.Page
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 Email(sender As Object, e As EventArgs)
Using mm As New MailMessage("sender@gmail.com", "receiver@gmail.com")
mm.Subject = "RDLC Report PDF example"
mm.Body = "RDLC Report PDF example"
mm.Attachments.Add(New Attachment(ExportReportToPDF(Server.MapPath("~/Files/"), "Invoice.pdf")))
mm.IsBodyHtml = True
Dim smtp As New SmtpClient()
smtp.Host = "smtp.gmail.com"
Dim credential As New NetworkCredential()
credential.UserName = "sender@gmail.com"
credential.Password = "xxxxx"
smtp.UseDefaultCredentials = True
smtp.Credentials = credential
smtp.Port = 587
smtp.EnableSsl = True
smtp.Send(mm)
End Using
End Sub
' Generate repord in a path
Private Function ExportReportToPDF(path As String, reportName As String) As String
Dim warnings As Warning()
Dim streamids As String()
Dim mimeType As String
Dim encoding As String
Dim filenameExtension As String
Dim bytes As Byte() = ReportViewer1.LocalReport.Render("PDF", Nothing, mimeType, encoding, filenameExtension, streamids, warnings)
Dim filename As String = path & reportName
Using fs = New System.IO.FileStream(filename, System.IO.FileMode.Create)
fs.Write(bytes, 0, bytes.Length)
fs.Close()
End Using
Return filename
End Function
End Class
It will generate the report in Files folder inside the project from that you can send the email.