protected void Page_Load(object sender, EventArgs e)
{
aid = Session["aid"].ToString();
if (!IsPostBack)
{
ReportViewer1.ProcessingMode = ProcessingMode.Local;
ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/BCReport.rdlc");
barcode dsCustomers = GetData("select bar_image,Barcode,bar_code from Master_Item where bar_Code=@ad");
ReportDataSource datasource = new ReportDataSource("DataSet1", dsCustomers.Tables[0]);
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(datasource);
//ReportViewer1.LocalReport.Refresh();
//Code For Download Direct PDF
Warning[] warnings;
string[] streamIds;
string mimeType = string.Empty;
string encoding = string.Empty;
string extension = string.Empty;
byte[] bytes = ReportViewer1.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamIds, out warnings);
// Now that you have all the bytes representing the PDF report, buffer it and send it to the client.
Response.Buffer = true;
Response.Clear();
Response.ContentType = mimeType;
Response.AddHeader("content-disposition", "attachment; filename= barcode.pdf");
Response.BinaryWrite(bytes); // create the file
Response.Flush();
}
}
private barcode GetData(string query)
{
SqlConnection con = (SqlConnection)(HttpContext.Current.Session["conn"]);
SqlCommand cmd = new SqlCommand(query);
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@ad", aid);
sda.SelectCommand = cmd;
using (barcode dsCustomers = new barcode())
{
sda.Fill(dsCustomers, "DataTable1");
return dsCustomers;
}
}
}From this code snippet, you can download direct pdf using RDLC report without opening reportviewer just add a block of code in your existing code.
