Shiv Kumar Yadav

Shiv Kumar Yadav

  • NA
  • 269
  • 25.8k

Error while Downloading RDLC Report automatically in MVC

Mar 31 2018 1:54 AM
Hi Everyone,
I am Getting an error While Downloading a RDLC Report in MVC (Controller).
But Same Code is Working on ASPX Page 
 
Error : Microsoft.ReportingServices.ReportProcessing.ReportProcessingException: The specified operation is not valid.
 
at : byte[] bytes = ReportViewer1.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamids, out warnings); 
  1. void ReportDownLoad(string id)  
  2. {  
  3.     scon = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);  
  4.   
  5.     ReportViewer ReportViewer1 = new ReportViewer();  
  6.     ReportViewer1.ProcessingMode = ProcessingMode.Local;  
  7.   
  8.     //set path of the Local report    
  9.     ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report/SingleReport.rdlc");  
  10.   
  11.     DataSet ds = new DataSet();  
  12.   
  13.     SqlDataAdapter adapt = new SqlDataAdapter("SELECT * from table1 where id='"+id+"'", scon);  
  14.   
  15.     adapt.Fill(ds, "DataTable1");  
  16.     scon.Close();  
  17.   
  18.     //Providing DataSource for the Report  
  19.     ReportDataSource rds = new ReportDataSource("DataSet1", ds.Tables[0]);  
  20.     ReportViewer1.LocalReport.DataSources.Clear();  
  21.   
  22.     //Add ReportDataSource  
  23.     ReportViewer1.LocalReport.DataSources.Add(rds);  
  24.   
  25.   
  26.     //Code For Download Direct PDF  
  27.     Warning[] warnings;  
  28.     string[] streamIds;  
  29.     string mimeType = string.Empty;  
  30.     string encoding = string.Empty;  
  31.     string extension = string.Empty;  
  32.     byte[] bytes = ReportViewer1.LocalReport.Render("PDF"nullout mimeType, out encoding, out extension, out streamIds, out warnings);  
  33.     // Now that you have all the bytes representing the PDF report, buffer it and send it to the client.      
  34.     Response.Buffer = true;  
  35.     Response.Clear();  
  36.     Response.ContentType = mimeType;  
  37.     Response.AddHeader("content-disposition""attachment; filename= ReferenceNo" + id + ".pdf");  
  38.     Response.BinaryWrite(bytes); // create the file      
  39.     Response.Flush();  
  40. }  
 
 Note Same Code is Working  in ASPX Page.
 
Please Help me. 

Answers (1)