Direct PDF Download From RDLC Without Opening Reportviewer

  1.     protected void Page_Load(object sender, EventArgs e)    
  2.     {    
  3.         aid = Session["aid"].ToString();    
  4.     
  5.         if (!IsPostBack)    
  6.         {    
  7.               
  8.             ReportViewer1.ProcessingMode = ProcessingMode.Local;    
  9.             ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/BCReport.rdlc");    
  10.     
  11.             barcode dsCustomers = GetData("select bar_image,Barcode,bar_code from Master_Item  where bar_Code=@ad");    
  12.             ReportDataSource datasource = new ReportDataSource("DataSet1", dsCustomers.Tables[0]);    
  13.             ReportViewer1.LocalReport.DataSources.Clear();    
  14.             ReportViewer1.LocalReport.DataSources.Add(datasource);    
  15.             //ReportViewer1.LocalReport.Refresh();    
  16.     
  17. //Code For Download Direct PDF    
  18.     
  19.             Warning[] warnings;    
  20.             string[] streamIds;    
  21.             string mimeType = string.Empty;    
  22.             string encoding = string.Empty;    
  23.             string extension = string.Empty;    
  24.     
  25.             byte[] bytes = ReportViewer1.LocalReport.Render("PDF"nullout mimeType, out encoding, out extension, out streamIds, out warnings);    
  26.     
  27.     
  28.             // Now that you have all the bytes representing the PDF report, buffer it and send it to the client.    
  29.             Response.Buffer = true;    
  30.             Response.Clear();    
  31.             Response.ContentType = mimeType;    
  32.             Response.AddHeader("content-disposition""attachment; filename= barcode.pdf");    
  33.             Response.BinaryWrite(bytes); // create the file    
  34.             Response.Flush();    
  35.     
  36.                 
  37.         }    
  38.     }    
  39.     private barcode GetData(string query)    
  40.     {    
  41.     
  42.         SqlConnection con = (SqlConnection)(HttpContext.Current.Session["conn"]);    
  43.     
  44.         SqlCommand cmd = new SqlCommand(query);    
  45.     
  46.         using (SqlDataAdapter sda = new SqlDataAdapter())    
  47.         {    
  48.             cmd.Connection = con;    
  49.             cmd.Parameters.AddWithValue("@ad", aid);    
  50.             sda.SelectCommand = cmd;    
  51.             using (barcode dsCustomers = new barcode())    
  52.             {    
  53.     
  54.                 sda.Fill(dsCustomers, "DataTable1");    
  55.                 return dsCustomers;    
  56.             }    
  57.     
  58.         }    
  59.     }