Answered by:
RDLC - Export directly to Excel or PDF from codebehind

Question
-
User1310743171 posted
Is is possible to export directly to Excel or PDF from a code behind file without showing the reportviewer first and without the user having to click the export button. So that only the generating functionality is used?
I am using VB and LocalReport processing.
Monday, May 10, 2010 2:55 PM
Answers
-
User-1528094326 posted
This also is very simple. It's done in 3 steps:
1: Instantiate your DataSet in code behind
2: Create a new report datasource against that DataSet
3: Add that datasource to the local report
Step 1: Instantiating your DataSet
// Assumes your xsd file is called MyDataSet
MyDataSetTableAdapters.YourTableAdapterHere ds = new MyDataSetTableAdapters.YourTableAdapterHere();Step 2: Create a new report datasource against that DataSet
// Create a new report datasource item and setup its context
ReportDataSource rds = new ReportDataSource("MyDataSourceName", ds.GetData());Step 3: Add that datasource to the local report
// Add it to the local report instance
viewer.LocalReport.DataSources.Add(rds);So all in all it should look like this:
private void CreatePDF(string fileName) { // Setup DataSet MyDataSetTableAdapters.YourTableAdapterHere ds = new MyDataSetTableAdapters.YourTableAdapterHere(); // Create Report DataSource ReportDataSource rds = new ReportDataSource("MyDataSourceName", ds.GetData()); // Variables Warning[] warnings; string[] streamIds; string mimeType = string.Empty; string encoding = string.Empty; string extension = string.Empty; // Setup the report viewer object and get the array of bytes ReportViewer viewer = new ReportViewer(); viewer.ProcessingMode = ProcessingMode.Local; viewer.LocalReport.ReportPath = "YourReportHere.rdlc"; viewer.LocalReport.DataSources.Add(rds); // Add datasource here byte[] bytes = viewer.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=" + fileName + "." + extension); Response.BinaryWrite(bytes); // create the file Response.Flush(); // send it to the client to download }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, May 11, 2010 12:49 PM
All replies
-
User-1528094326 posted
Yes this functionality is supported and is easy. You can execute it in probably 10 lines of code. You will use the ReportViewer.LocalReport.Render() method and specify that it is a PDF or Excel file you want. You will then receive an array of bytes. From that you will just buffer it and send it to the client for downloading with the Response methods.
Monday, May 10, 2010 3:59 PM -
User-1528094326 posted
I wanted to post an example of how this can be done as well. The example below is in c# code behind.
private void CreatePDF(string fileName) { // Variables Warning[] warnings; string[] streamIds; string mimeType = string.Empty; string encoding = string.Empty; string extension = string.Empty; // Setup the report viewer object and get the array of bytes ReportViewer viewer = new ReportViewer(); viewer.ProcessingMode = ProcessingMode.Local; viewer.LocalReport.ReportPath = "YourReportHere.rdlc"; byte[] bytes = viewer.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=" + fileName + "." + extension); Response.BinaryWrite(bytes); // create the file Response.Flush(); // send it to the client to download }
Monday, May 10, 2010 5:30 PM -
User1310743171 posted
Thanks for your replies, they were very helpful in understanding what method to use.
What I am struggling with now is figuring out how to bind the datasource to the reportviewer using this method. The RDLC report is using a table adapter (XSD) as it's datasource and I have it stored in the App_Code folder. I keep getting an error message stating A data source instance has not been supplied for the data source "MyDataSourceName"
Tuesday, May 11, 2010 10:29 AM -
User-1528094326 posted
This also is very simple. It's done in 3 steps:
1: Instantiate your DataSet in code behind
2: Create a new report datasource against that DataSet
3: Add that datasource to the local report
Step 1: Instantiating your DataSet
// Assumes your xsd file is called MyDataSet
MyDataSetTableAdapters.YourTableAdapterHere ds = new MyDataSetTableAdapters.YourTableAdapterHere();Step 2: Create a new report datasource against that DataSet
// Create a new report datasource item and setup its context
ReportDataSource rds = new ReportDataSource("MyDataSourceName", ds.GetData());Step 3: Add that datasource to the local report
// Add it to the local report instance
viewer.LocalReport.DataSources.Add(rds);So all in all it should look like this:
private void CreatePDF(string fileName) { // Setup DataSet MyDataSetTableAdapters.YourTableAdapterHere ds = new MyDataSetTableAdapters.YourTableAdapterHere(); // Create Report DataSource ReportDataSource rds = new ReportDataSource("MyDataSourceName", ds.GetData()); // Variables Warning[] warnings; string[] streamIds; string mimeType = string.Empty; string encoding = string.Empty; string extension = string.Empty; // Setup the report viewer object and get the array of bytes ReportViewer viewer = new ReportViewer(); viewer.ProcessingMode = ProcessingMode.Local; viewer.LocalReport.ReportPath = "YourReportHere.rdlc"; viewer.LocalReport.DataSources.Add(rds); // Add datasource here byte[] bytes = viewer.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=" + fileName + "." + extension); Response.BinaryWrite(bytes); // create the file Response.Flush(); // send it to the client to download }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, May 11, 2010 12:49 PM -
User1310743171 posted
Okay that all makes sense. Again, thanks for the response.
Now let's say I wanted to provide a link or button on my aspx page to export the report to PDF using the code you supplied. How would I go about calling that so that the report exports directly to PDF when the link or button is clicked?
Tuesday, May 11, 2010 1:25 PM -
User-1528094326 posted
That is a complete stand alone function that both creates and then sends it to the client for downloading. The last line "Response.Flush();" actually sends the PDF that was just created in the buffer on the server straight to the client in the form of a download. So basically all you would do is something like this...
Create a fancy reports page to display the user, and then create a standard asp button that says something like "Get Report". When they click it, have it fire a click event like so:
protected void YourButton_Click(object sender, EventArgs e)
{
CreatePDF(YourMeaningfulFileNameHere);
}Really thats all there is too it. It will create the report, then send it to the user as a download all by itself. Easy right?
As a more detailed explanation, When you finally get the array of bytes, that is the Report itself in Raw format. After that you start all the "Response" stuff. That is created a buffer that then writes the binary data into an actually file hence the Response.BinaryWrite() method. After its finished writing the array of bytes into an actual file, it is completed finished and is ready to be sent to the client for download. Thats where Response.Flush() comes in. It sends it to the client via HTTP.
Tuesday, May 11, 2010 1:34 PM -
User1310743171 posted
I tried adding a standard ASP button to call the function, but I'm once again getting the A data source instance has not been supplied for the data source "DirectToExcelData_ACTG_ACTUAL_CASH" error. I converted your code into VB, so maybe something got lost in translation along the way. Here is my codeI tried adding a standard ASP button to call the function, but I'm getting the A data source instance has not been supplied for the data source "MyDataSet_DataTable1" error on button click. So it appears I'm doing something wrong when setting up the dataset and creating the report datasource.
I converted your code into VB, so maybe something got lost in translation along the way. Below is my code. What am I doing wrong?
Imports System.Web.UI.WebControls Imports Microsoft.Reporting.WebForms Imports System.Data.SqlClient Imports System.Data Imports System.IO Partial Class Reports_DirectToPDF Inherits System.Web.UI.Page Private Function CreatePDF(ByVal FileName As String) As String ' Setup DataSet Dim ds As New MyDataSetTableAdapters.DataTable1TableAdapter() ' Create Report DataSource Dim rds As New ReportDataSource("DataTable1", ds.GetData()) ' Variables Dim warnings As Warning() = Nothing Dim streamids As String() = Nothing Dim mimeType As String = Nothing Dim encoding As String = Nothing Dim extension As String = Nothing ' Setup the report viewer object and get the array of bytes Dim viewer As New ReportViewer() viewer.ProcessingMode = ProcessingMode.Local viewer.LocalReport.ReportPath = "reports\report1.rdlc" viewer.LocalReport.DataSources.Add(rds) Dim bytes As Byte() = viewer.LocalReport.Render("PDF", Nothing, mimeType, encoding, extension, streamids, _ 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=" & FileName & ".") + extension) Response.BinaryWrite(bytes) ' create the file ' send it to the client to download Response.Flush() Return FileName End Function Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click CreatePDF("TestFile") End Sub End Class
Tuesday, May 11, 2010 4:31 PM -
User-1528094326 posted
The error is coming from the datasource obviously. It looks like your not passing in the correct data source name the report elements are tryin to bind to.
You are trying to bind it to "DataTable1" but it looks its trying to bind to MyDataSet_DataTable1 if you copy and pasted that. The best way to find out is to fire up visual studio. Do the following:1: Open your website
2: Right Click on your RDLC file and click Open With...
3: Select XML Editor and click Ok
4: Look for the <DataSets> tag, and then look for the <DataSet Name="........"> element inside of it.
5: Copy the name
6: Paste that name into your code where it currently says "DataTable1"Save it and fire it up and try again.
Tuesday, May 11, 2010 6:30 PM -
User1310743171 posted
Yep, you were right, I had the wrong name for the data source. Everything is working as expected now.
Thanks a lot for your assistance!
Tuesday, May 11, 2010 7:35 PM -
User336451782 posted
Gentlemen,
This looks exactly like what I am needing myself! I am extremely new at SSRS Reporting, and still pretty green in .Net coding as well, so let me say thank you in advance for answering my simplistic questions.
I'm needing the VB version of this code, it looks like you just created a seperate class for this code? If so, how/where are you calling it from your Report Viewer codebehind? I know that Scott says to call it from the ReportViewer.LocalReport.Render() function, but when I look at my method options for the ReportViewer class the only Render method I see is the PreRender method -- is this where I call it from?
Thanks again for your help!
Thursday, July 8, 2010 8:37 AM -
User336451782 posted
My apologies, I asked my questions before I took time to study what was going on...after taking a closer look I think I have a good grasp for what's happening here. Since this is something I think I'll be doing fairly often -- sending the report output straight to PDF, I decided to create a separate class for the code. Below is what I have so far.
I am stuck with one spot, and I'm hoping maybe you all can help me with it. My .rdlc is using a stored procedure with one parameter getting passed in, which is a Session variable. I set up the call to the stored procedure inside of the .rdlc. I've played around with how to make this work in my function, but no matter what I do it keeps erroring out when calling the ReportViewer.LocalReport.Render method. I have tried commenting out the dataset, as you see below, and also commenting out the creation of the ReportDataSource altogether. This feels like something simple that I'm overlooking. I'm going to keep playing around myself, but am hoping somebody can help save me some time!
Imports Microsoft.VisualBasic Imports Microsoft.Reporting.WebForms Imports System.Data.SqlClient Imports System.Data Imports System.IO Public Class ReportCommon Public Function CreatePDF(ByVal FileName As String, ByVal WebPage As Object) As String ' Setup DataSet 'Dim ds As New MyDataSetTableAdapters.DataTable1TableAdapter() ' Create Report DataSource Dim rds As New ReportDataSource("DEV_AssessmentTestDataSet_sp_GetWritingMSBarcodes") ', ds.GetData()) ' Variables Dim warnings As Warning() = Nothing Dim streamids As String() = Nothing Dim mimeType As String = Nothing Dim encoding As String = Nothing Dim extension As String = Nothing Try ' Setup the report viewer object and get the array of bytes Dim viewer As New ReportViewer() viewer.ProcessingMode = ProcessingMode.Local viewer.LocalReport.ReportPath = "writing\WritingResults.rdlc" viewer.LocalReport.DataSources.Add(rds) Dim bytes As Byte() = viewer.LocalReport.Render("PDF", Nothing, mimeType, encoding, extension, streamids, warnings) ' Now that you have all the bytes representing the PDF report, buffer it and send it to the client. WebPage.Response.Buffer = True WebPage.Response.Clear() WebPage.Response.ContentType = mimeType WebPage.Response.AddHeader("content-disposition", ("attachment; filename=" & FileName & ".") + extension) WebPage.Response.BinaryWrite(bytes) ' create the file ' send it to the client to download WebPage.Response.Flush() Catch ex As Exception Throw New Exception("Error Creating PDF File: " + ex.InnerException.Message) End Try Return FileName End Function End Class
Thursday, July 8, 2010 10:07 AM -
User336451782 posted
I did it, and I am so pleased with myself! It takes so little to make me happy
Below is the class I created to send the results of an SSRS report directly to a PDF file. I'm going to clean it up just a little bit more, so that the hard-coded stuff is passed in as parameters, but hopefully this will help anybody else who is looking to do something similar...
Imports Microsoft.VisualBasic Imports Microsoft.Reporting.WebForms Imports System.Data.SqlClient Imports System.Data Imports System.IO Public Class ReportCommon Inherits System.Web.UI.Page Dim dc As New DataCommon Public Function CreatePDF(ByVal FileName As String, ByVal WebPage As Object) As String Dim ds As New DataSet 'Initialize Connection String dc.Init("AssessmentTest") 'Call the Stored Proc and load the Dataset dc.ExecStoredProc("sp_GetWritingMSBarcodes", "@GroupID", Session("GroupID"), SqlDbType.Int) dc.sqlDA.Fill(ds, "DataTable1") ' Create Report DataSource Dim rds As New ReportDataSource("DEV_AssessmentTestDataSet_sp_GetWritingMSBarcodes", ds.Tables("DataTable1")) ' Variables Dim warnings As Warning() = Nothing Dim streamids As String() = Nothing Dim mimeType As String = Nothing Dim encoding As String = Nothing Dim extension As String = Nothing Try ' Setup the report viewer object and get the array of bytes Dim viewer As New ReportViewer() viewer.ProcessingMode = ProcessingMode.Local viewer.LocalReport.ReportPath = "writing\WritingResults.rdlc" viewer.LocalReport.DataSources.Add(rds) Dim bytes As Byte() = viewer.LocalReport.Render("PDF", Nothing, mimeType, encoding, extension, streamids, warnings) ' Now that you have all the bytes representing the PDF report, buffer it and send it to the client. WebPage.Response.Buffer = True WebPage.Response.Clear() WebPage.Response.ContentType = mimeType WebPage.Response.AddHeader("content-disposition", ("attachment; filename=" & FileName & ".") + extension) WebPage.Response.BinaryWrite(bytes) ' create the file ' send it to the client to download WebPage.Response.Flush() Catch ex As Exception Throw New Exception("Error Creating PDF File: " + ex.InnerException.Message) End Try Return FileName End Function End Class
Thursday, July 8, 2010 10:39 AM -
User-1528094326 posted
Congrats you are now a pro :)
Thursday, July 8, 2010 12:54 PM -
User-1247285196 posted
hi mr,
could you please give this same code in vb.net windows application. because i have created RDLC report during i export it to excel format some columns are merged and data can be segregated in individula cell. so i could not upload it the our software. if u have any solution for it please advice me...
thank you..i am waiting.
Sunday, September 19, 2010 8:39 AM -
User-1247285196 posted
what u got it or not
Sunday, September 19, 2010 8:40 AM -
User-1528094326 posted
Do the same thing. The WinForms version of the reportviewer has the same Render() function for ServerReports or LocalReports.
Friday, September 24, 2010 1:50 PM -
User-619793096 posted
how to create gridview grouping
Thursday, February 10, 2011 6:08 AM -
User-1528094326 posted
in an RDLC file? or in the <asp:GridView /> control?
Monday, February 14, 2011 2:45 PM -
User-619793096 posted
GridView Control
Monday, February 14, 2011 11:10 PM -
Tuesday, February 15, 2011 1:05 PM
-
User2108234275 posted
Hi
Can you give me the coding for winform application also?
Bcozz I'm struggling to convert the ssrs report to pdf I'm getting error while rendering...
The error is "An Error Occured during Local Report Processing"
Padhu
Friday, February 18, 2011 9:53 AM -
User-1528094326 posted
For windows forms you use LocalReport.ReportEmbeddedResource = YourRDLCFile.rdlc; instead of ReportPath. That will fix your error.
Friday, February 18, 2011 1:26 PM -
User-619793096 posted
thank you evilscott....
Then how to convert my asp.net webpage from http to https
Tuesday, March 15, 2011 2:37 AM -
User-1528094326 posted
HTTPS uses SSL certificates and that is managed by IIS, not the aspx pages themselves or in your code.
You will need to either create a certificate on your own using Certificate Authority in Windows if this is an internal website only, however if this is a generic website for many users on the internet then you will want to purchase an SSL cert from a reputable service such as Godaddy.com or NetworkSolutions.com
When you get your certificate you'll import it into your IIS website and setup the appropriate SSL settings against the website in IIS Manager.
Tuesday, March 15, 2011 7:53 PM -
User-89493104 posted
OK looks very good, i'm very new to this, but I have one problem
I'm trying to do this in my winform applictions using visual studio 2010
And when I start to use response.anything............ he says that response is not declared , did I forgot a namespace or something
Imports
Microsoft.Reporting.WinForms
System.IO
System.Data
Thursday, August 25, 2011 4:06 AM -
User-1528094326 posted
The Response object is used for web applications only, hence why its called the HttpResponse object. For a windows Forms application you will generate the PDF file using the same Render method, but then you would normally export it to the user using the SaveFileDialog or some other transport.
Thursday, August 25, 2011 4:15 AM -
User-89493104 posted
can I save this programmatically without the user has to click a button ?
could you give me the code to do? because i'm very very new to this tnx a lot
Thursday, August 25, 2011 4:24 AM -
User-1528094326 posted
Yes you can use the StreamWriter to write the file to the users file system. I don't have a test project for this so I can't give you a bunch of code for it, would take me too much time to set it up, but basically all you need to do is perform the Render method against the ReportViewer and that returns an array of bytes (byte[])
You can then take that array of bytes and shove it into a memory stream or write it to a file anyway you choose.
Thursday, August 25, 2011 4:29 AM -
User-89493104 posted
the report i use is rpt_fouten.rdlc and de bindingsource I use is :
* Report Data Source = "gegevens_fouten"
* Data Source instance = ViewoverzichtBindingsource
Do I have to note that also? or does the program automatically knows the datasource of the report , if i call rpt_fouten.rdlc
Now I use this code :
Private Sub createpdf(ByVal filename As String)
Dim reportType As String = "PDF"
Dim mimeType As String
Dim encoding As String
Dim fileNameExtension As String
Dim deviceInfo As String = "<DeviceInfo><OutputFormat>PDF</OutputFormat>" & _
"<PageWidth>8.5in</PageWidth><PageHeight>11in</PageHeight><MarginTop>0.5in</MarginTop><MarginLeft>1in</MarginLeft><MarginRight>1in</MarginRight><MarginBottom>0.5in</MarginBottom></DeviceInfo>"
Dim warnings As Warning()
Dim streams As String()
Dim renderedBytes As Byte()
'Render the report
'Problem here is also that he doesnt recognise reportviewer1, because the button is not on the form where the reportviewer isrenderedBytes = ReportViewer1.LocalReport.Render(reportType,
deviceInfo, mimeType, encoding, fileNameExtension, streams, warnings)
Dim fs As FileStream =
System.IO.File.Create("c:\rapport\" & filename & ".pdf")
fs.Write(renderedBytes, 0, renderedBytes.Length)
fs.Close()end sub
Thursday, August 25, 2011 4:38 AM -
User-1528094326 posted
2 options
1) You can just pass in the form where the ReportViewer is contained to the new form where the button is
or
2) You can just create a new ReportViewer in code on the new page, either way will work.
Thursday, August 25, 2011 4:42 AM -
User-89493104 posted
so when i use in my code "rpt_fouten.ReportViewer1.LocalReport.Render " because rpt_fouten is name of the form, en reportviewer1 is name van de reportviewer.
Does the then know that rpt_fouten.rdlc is behind this reportviewer?
OK my code works BUT
I don't get any data in my table , where do I have to bind my data to the report before I render it ?
Thursday, August 25, 2011 4:46 AM -
User-1528094326 posted
Let me see if I can create something really quick to help you out. Hang on...
Thursday, August 25, 2011 4:48 AM -
User-89493104 posted
code already works now, BUT no data in my tabel
Public Sub createpdf(ByVal filename As String)
Dim reportType As String = "PDF"
Dim mimeType As String = Nothing
Dim encoding As String = Nothing
'Dim fileNameExtension As String
Dim deviceInfo As String = "<DeviceInfo><OutputFormat>PDF</OutputFormat>" & _
"<PageWidth>8.5in</PageWidth><PageHeight>11in</PageHeight><MarginTop>0.5in</MarginTop><MarginLeft>1in</MarginLeft><MarginRight>1in</MarginRight><MarginBottom>0.5in</MarginBottom></DeviceInfo>"
Dim warnings As Warning() = Nothing
'Dim streams As String()
Dim renderedBytes As Byte()
'Render the report
renderedBytes = rpt_fouten.ReportViewer1.LocalReport.Render(reportType, Nothing, mimeType, encoding, Nothing, Nothing, warnings)
Dim fs As FileStream =
System.IO.File.Create("c:\ascii\" & filename & ".pdf")
fs.Write(renderedBytes, 0, renderedBytes.Length)
fs.Close()
End SubThursday, August 25, 2011 4:51 AM -
User-1528094326 posted
Okay, even though this is an ASP.NET website and NOT windows forms, several people have asked for help converting this to Windows forms, so here is a class I wrote in c# that will perform the actions for you.
This class has 2 basic functions that will either auto save the file to a specified location, or open a save file dialog for the user to save it that way. This class also allows you to export to PDF, Excel, as well as Word (Word ONLY WORKS WITH REPORT VIEWER 2010!!!)
To use the class just copy and paste it to your project, make sure you have the necessary Microsoft.Reporting references, and you should be good. Here is how to use the class:
// To automatically save the file to the file system PDF_Generator g = new PDF_Generator(); g.DataSourceName = "YourDataSourceNameHere"; g.DataSourceValue = YourDatahere; g.FileStorageLocation = "C:\\MyAutoSavedReports"; g.ReportPath = "YourAssemblyNamehere.YourReportName.rdlc"; g.AutoSaveFile(PDF_Generator.ReportType.<type you want here>, "file name + extension here"); // To ask for a save file dialog prompt PDF_Generator g = new PDF_Generator(); g.DataSourceName = "YourDataSourceNameHere"; g.DataSourceValue = YourDatahere; g.ReportPath = "YourAssemblyNamehere.YourReportName.rdlc"; g.SendToSaveDialog(PDF_Generator.ReportType.<type you want here>, "file name + extension here");
Here is the class itself, it is provided with no warranties and is given AS IS.
using System.IO; using System.Windows.Forms; using Microsoft.Reporting.WinForms; namespace TestRDLC { public class PDF_Generator { #region Constructors public PDF_Generator() { DataSourceName = string.Empty; DataSourceValue = null; ReportPath = string.Empty; FileStorageLocation = string.Empty; } #endregion #region Public Methods public byte[] GetFile(string reportType) { Warning[] warnings; string[] streamIds; string mimeType = string.Empty; string encoding = string.Empty; string extension = string.Empty; ReportDataSource rds = new ReportDataSource(DataSourceName, DataSourceValue); ReportViewer viewer = new ReportViewer(); viewer.ProcessingMode = ProcessingMode.Local; viewer.LocalReport.ReportEmbeddedResource = ReportPath; viewer.LocalReport.DataSources.Add(rds); return viewer.LocalReport.Render(reportType, null, out mimeType, out encoding, out extension, out streamIds, out warnings); } public void AutoSaveFile(ReportType type, string fileName) { if (!string.IsNullOrEmpty(DataSourceName) && DataSourceValue != null && !string.IsNullOrEmpty(ReportPath) && !string.IsNullOrEmpty(FileStorageLocation)) { byte[] file = GetFile(type.ToString()); FileStream stream = new FileStream(FileStorageLocation + "\\" + fileName, FileMode.Create, FileAccess.Write); stream.Write(file, 0, file.Length); stream.Close(); } else { MessageBox.Show("Required information missing before your file can be automatically processed", "Error creating requested file"); } } public void SendToSaveDialog(ReportType type, string fileName) { if (!string.IsNullOrEmpty(DataSourceName) && DataSourceValue != null && !string.IsNullOrEmpty(ReportPath)) { byte[] file = GetFile(type.ToString()); FileStream stream; SaveFileDialog dialog = new SaveFileDialog(); dialog.Filter = "All files (*.*)|*.*"; dialog.RestoreDirectory = true; dialog.FileName = fileName; if (dialog.ShowDialog() == DialogResult.OK) { if ((stream = (FileStream)dialog.OpenFile()) != null) { stream.Write(file, 0, file.Length); stream.Close(); } } } else { MessageBox.Show("Required information missing before your file can be processed", "Error creating requested file"); } } #endregion #region Properties public string DataSourceName { get; set; } public object DataSourceValue { get; set; } public string ReportPath { get; set; } public string FileStorageLocation { get; set; } #endregion #region Enum public enum ReportType { PDF, Excel, Word } #endregion } }
Thursday, August 25, 2011 5:45 AM -
User-89493104 posted
ok I used your system but I still don't get any data in my pdf report.
this is my code I use
what am I doing wrong
Public Sub createpdf()
Dim mimeType As String = String.Empty
Dim encoding As String = String.Empty
' gegevens op vragen
cmd.Connection = conn
cmd.CommandType = CommandType.Text
cmd.CommandText = "select * from View_overzicht_fouten"
adapter = New SqlDataAdapter(cmd)
ds = New DataSet
adapter.Fill(ds, "klanten")
' einde gegevens
Dim rds As New ReportDataSource("DS_View_overzicht_fouten", ds.Tables("klanten"))
rpt_fouten.repview_fouten.ProcessingMode = ProcessingMode.Local
rpt_fouten.repview_fouten.LocalReport.ReportEmbeddedResource = "klantprogramma.rpt_fouten.rdlc"
rpt_fouten.repview_fouten.LocalReport.DataSources.Add(rds)
rpt_fouten.repview_fouten.LocalReport.Refresh()
Dim deviceInfo As String = "<DeviceInfo><OutputFormat>PDF</OutputFormat>" & _
"<PageWidth>8.5in</PageWidth><PageHeight>11in</PageHeight><MarginTop>0.5in</MarginTop><MarginLeft>1in</MarginLeft><MarginRight>1in</MarginRight><MarginBottom>0.5in</MarginBottom></DeviceInfo>"
Dim warnings As Warning() = Nothing
'Dim streams As String()
Dim renderedBytes As Byte()
'Render the report
renderedBytes = rpt_fouten.repview_fouten.LocalReport.Render("PDF", Nothing, mimeType, encoding, Nothing, Nothing, warnings)
Dim tijdcode As String = jaarnaar8(Date.Now())
MsgBox(tijdcode)
Dim fs As FileStream =
System.IO.File.Create("c:\ascii\rpt_fouten" & tijdcode & ".pdf")
fs.Write(renderedBytes, 0, renderedBytes.Length)
fs.Close()
End SubThursday, August 25, 2011 7:12 AM -
User-1528094326 posted
You need to either render the data from your current report viewer object on the form, or you need to make sure you put the ReportDataSource on the new page to provide the data.
Thursday, August 25, 2011 6:56 PM -
User-89493104 posted
Don't I add the data to the report here ?
ds = New DataSet
adapter.Fill(ds, "klanten")
' einde gegevens
Dim rds As New ReportDataSource("DS_View_overzicht_fouten", ds.Tables("klanten"))
rpt_fouten.repview_fouten.LocalReport.DataSources.Add(rds)with this code??
or do i make a mistake?
Friday, August 26, 2011 5:12 AM