Hi JagadishMohan...,
Refer below sample code for your reference and try to implement in your code as per your code logic.
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>
<asp:Button Text="Jpeg" runat="server" OnClick="JpegPng" />
<asp:Button Text="Png" runat="server" OnClick="JpegPng" />
<br />
<br />
<rsweb:ReportViewer ID="ReportViewer1" runat="server" Width="600">
</rsweb:ReportViewer>
<br />
</form>
</body>
</html>
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!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);
}
}
protected void JpegPng(object sender, EventArgs e)
{
ReportViewer1.ProcessingMode = ProcessingMode.Local;
ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc");
string reportType = "Image";
string outputFormat = (sender as Button).Text.ToLower();
byte[] renderedBytes;
if (outputFormat == "jpg")
{
renderedBytes = ReportViewer1.LocalReport.Render(reportType, "<DeviceInfo><OutputFormat>JPG</OutputFormat></DeviceInfo>");
}
else
{
renderedBytes = ReportViewer1.LocalReport.Render(reportType, "<DeviceInfo><OutputFormat>PNG</OutputFormat></DeviceInfo>");
}
Response.Buffer = true;
Response.Clear();
Response.ContentType = "image/jpeg";
Response.AddHeader("content-disposition", "attachment; filename=" + DateTime.Now.ToString("ddMMyyyyhhmmss") + "." + outputFormat);
Response.BinaryWrite(renderedBytes);
Response.Flush();
}
private Customers GetData()
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlCommand cmd = new SqlCommand("Customers_GetCustomers");
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
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
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
Protected Sub JpegPng(sender As Object, e As EventArgs)
ReportViewer1.ProcessingMode = ProcessingMode.Local
ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc")
Dim reportType As String = "Image"
Dim outputFormat As String = TryCast(sender, Button).Text.ToLower()
Dim renderedBytes As Byte()
If outputFormat = "jpg" Then
renderedBytes = ReportViewer1.LocalReport.Render(reportType, "<DeviceInfo><OutputFormat>JPG</OutputFormat></DeviceInfo>")
Else
renderedBytes = ReportViewer1.LocalReport.Render(reportType, "<DeviceInfo><OutputFormat>PNG</OutputFormat></DeviceInfo>")
End If
Response.Buffer = True
Response.Clear()
Response.ContentType = "image/jpeg"
Response.AddHeader("content-disposition", Convert.ToString("attachment; filename=" + DateTime.Now.ToString("ddMMyyyyhhmmss") + ".") & outputFormat)
Response.BinaryWrite(renderedBytes)
Response.Flush()
End Sub
Private Function GetData() As Customers
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim cmd As New SqlCommand("Customers_GetCustomers")
Using con As New SqlConnection(conString)
Using sda As New SqlDataAdapter()
cmd.Connection = con
cmd.CommandType = CommandType.StoredProcedure
sda.SelectCommand = cmd
Using dsCustomers As New Customers()
sda.Fill(dsCustomers, "DataTable1")
Return dsCustomers
End Using
End Using
End Using
End Function
Screenshot