Adding custom code to Local Reports in Visual Studio.NET 2005 (Problems & Solutions)
If you are one of the people who used and enjoyed SQL Server Reporting Services (SSRS) in SQL 2000 and you wanted to use it in your windows/web applications without the server side components (just like what you are doing with Crystal Reports). So you would be much exited about the new ReportViewer Controls. ReportViewer controls are two controls for Windows and Web applications that can either show reports produced by SQL Server Reporting Services or process and render reports locally.
These reports are called Local Report and it's intended to provide reporting features without SSRS. You can use them without having a license for SSRS but be aware that you would lose some features like
- Report parameters in client report definitions (.rdlc) do not map to query parameters. There is no parameter input area in a client report definition that accepts values that are subsequently used in a query.
- Client report definitions do not include embedded query information. You must define data sources that return ready-to-use data for the report. So you have to provide a DataSource for your report like a DataSet, DataTable…
- Browser-based printing through the RSClientPrint ActiveX control is not available for client report definitions that run in the ReportViewer Web server control. The print control is part of the report server feature set.
- You would loose all collaboration activities of SSRS like scheduling to send reports to some users at specific time
I used ReportViewer for Windows forms in small report in which I had to add custom code in it and I had some problems that I would like to share with you solutions for them.
First we would go quickly through the process of creating simple report as following:
1) Create a new windows forms project and open its form. Drag the ReportViewer control from the Data tab as shown in the image below
2) Use the smart tag tasks to dock the report in all the form (you can use properties window as well). Then from the smart tag choose "Design new report". If you didn't see this option you can simply create new report. From solution explorer, right click on the project name then choose "add new item" then choose report.
3) The report file would have the extension "rdlc" not "rdl" as it would be in server reports.
4) Local Reports need to have a data source so go to Data Sources window and create your data source (let's say that it will get all Category name and description from categories table in Northwind)
5) From the ToolBar window, drop a table inside the report
6) From the Data Source window, drag & drop the CategoryName and Description fields to the details row of the table. Now you finished creating the report. Let's bind it to the ReportViewer
7) Return back to the ReportViewer and from the smart tag menu choose, select your report name from the drop down menu.
8) Run the application.
Now you have the report. Let's go to add custom code to the report.
Why would you add custom code?
You may want to add custom code to the report to do more actions than what's provided with the report functions. So it gives you a base for extending your report. You may want to add custom code to implement simple string manipulation task or sophisticated data access manipulation.
Cod added to Local Report can be one of two types:
Embedded Code: Added directly inside the rdlc file. If you open the file in notepad, you can see the code inside the <Code> tag.
Custom Assemblies: You can add a reference in external assembly and use functions inside it.
How to add a custom code in a local report?
According to MSDN library, you add it as following:
Embedded Code:
- On the Report menu, click Report Properties.
- On the Code tab, in Custom code, type the code.
Custom Assemblies:
- On the Report menu, click Report Properties.
- On the References tab, do the following:
- In References, click the add (...) button and then select or browse to the assembly from the Add Reference dialog box.
- In Classes, type name of the class and provide an instance name to use within the report. That's in the case of instance members, in the case of static (shared in VB) members, you don't need to add anything in Classes.
What's the code that I can add?
You can add class members only (properties, methods, fields) but you can't add classes because these members are encapsulated in a class at runtime, this class called Code class.
Take a look at this code snippet.
Public ReadOnly Property getSomeData() As String
Get
Return sharedMember
End Get
End Property
Dim sharedMember As String = "Omar"
Public Function toUpperCase(ByVal s As String)
Dim conn As New System.Data.SqlClient.SqlConnection()
Return s.toUpper()
End Function
You can use this code inside a textBox or a table column as following =Code.toUpperCase(Code.getSomeData)
What are the namespaces that I can use inside my custom code?
All .NET namespaces can be used inside your custom code but be aware that by default only Microsoft.VisualBasic, System.Convert, and System.Math namespaces are referenced inside your reporting custom code.
Now let's jump into the problems.
Adding code that uses types that are not in these namespaces (Microsoft.VisualBasic, System.Convert, and System.Math)
Let's say that you are using a code that access SQL Server database to get some data to use it inside your report. You are using SqlConnection in your code. You would receive an error like this
There is an error on line 9 of custom code: [BC30002] Type 'System.Data.SqlClient.SqlConnection' is not defined.
Because System.Data.SqlClient resides in the System.Data.dll assembly and this assembly is not referenced by default by the report custom code. To solve this problem you need to add a reference to the System.Data.dll assembly in the References tag in the report properties window.
But this is not the end of the story; you would face another problem which described in the next step
How to make an assembly trusted for your report code
Because Local Reports like SQL Server Reporting Services reports are written using open format (Report Definition Language (RDL)). Any user can add any code or reference any assembly from your rdl file at the production environment which would cause a BIG security hole. You need to mark these assemblies as trusted inside your Windows Application code. Or you would get an error like this
The report references the code Module 'System.Data, Version=2.0.0.0, Culture=neutral, Publickey Token=b77a5XXXXXXXX', which is not a trusted assembly
You need to use the AddTrustedCodeModuleInCurrentAppDomain method of the LocalReport class. You can add the following line at the Form_load event handler of your windows application.
this.reportViewer1.LocalReport.AddTrustedCodeModuleInCurrentAppDomain("System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
And again it's not the end of the story; you will face another problem ;). Please be patient with me because I faced all these problems sequentially and I didn't find any resources to help me.
What are the Code Access Security (CAS) permissions assigned to any code running inside LocalReport by default
By default the only permission bound to the code running inside the LocalReport is ExecutePermission So your code can't access file system, databases, registry… unless you give it explicitly the required permissions.
For example, if you are trying to connect to a database from your code, you will get this error
Request of the permission of type 'System.Data.SqlClient.SqlClientPermission, System.Data, Version=2.0.0.0, Culture=neutral, PublicKey Token=b77a5c561934e089' failed.
To give the assemblies running in the report the appropriate permissions, you need to use the ExecuteReportInCurrentAppDomain Method of the LocalReport class. Which causes the expressions in the report to be executed inside the current appDomain instead of executing them in a sandbox.
The ExecuteReportInCurrentAppDomain method takes one parameter of the type Evidence.
In the following example I'm using the evidence associated with my current appDomain so the report code will have all permissions that I have in my application
this.reportViewer1.LocalReport.ExecuteReportInCurrentAppDomain(AppDomain.CurrentDomain.Evidence);
By working on these steps, your code will run smoothly inside your report.
For the people who are trying to move their code in separated assembly and reference it from their report, they would face another problem.
If you are following the steps mentioned in MSDN regarding how to reference custom code (mentioned above). You will end up with this message
Error while loading code module: ‘ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null’. Details: Could not load file or assembly 'ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
After a lot of trials with this error, I found that I have to do the following.
- Select the report file from Solution Explorer, Go to Properties Window and change its BuildAction to Content
- Set Copy to output Directory property to Copy always or Copy if newer
- In the form designer class (the partial class created by Visual Studio according to naming schema <FormName>.Designer.CS or <FormName>.Designer.VB) replace the line which instruct the ReportViewer to load the report file from resource file with another line that instruct the ReportViewer to load the report from current directory.
- So this line
this.reportViewer1.LocalReport.ReportEmbeddedResource = "testLocalReport.Report1.rdlc";
- With this line
this.reportViewer1.LocalReport.ReportPath = "Report1.rdlc";
- Copy the dll that you are referencing to the Debug or Release directory of your application
- Make the line that executes the report in the current appDomain first, then add the lines that trust the referenced assembly and all assemblies referenced by this assembly (see above)
I hope this would help you to really enjoy VS 2005 features
//Mohamed
Comments
Anonymous
December 20, 2005
being a long time access reports user, i am missing the "OnFormat, OnPrint etc.." report events which is not available in reporting services.
anyone knows how to overcome this limitation of reporting services ??Anonymous
December 22, 2005
Hello Sazzad,
There are plenty of useful events of the ReportViewer control. Please take a look at this article http://msdn2.microsoft.com/en-us/library/ms255387.aspxAnonymous
April 17, 2006
Great Article !
Thank You :)Anonymous
May 09, 2006
Simple as usual, full of data as usual too, so lovely blog contains nice articles with easy implementation, I know I have not been taking too many looks over your blog, but It is really nice. Good Luck with it.Anonymous
May 10, 2006
After many frustrating hours trying to get a custom assembly to work with ReportViewer I found your blog. Everything now works fine. My sincere thanks!Anonymous
May 16, 2006
The comment has been removedAnonymous
May 24, 2006
The comment has been removedAnonymous
May 24, 2006
The comment has been removedAnonymous
June 05, 2006
The comment has been removedAnonymous
June 19, 2006
I got round the file not found error by placing the file in "C:Program FilesMicrosoft Visual Studio 8Common7IDE". I still can't seem to reference the code, but this is a step closer.
ttfn
DavidAnonymous
July 26, 2006
The comment has been removedAnonymous
August 09, 2006
Anyone find solution for web apps yet?Anonymous
August 15, 2006
The comment has been removedAnonymous
August 16, 2006
Excellent Article. Thanks a lot.
Microsoft should learn something from this guy.Anonymous
August 16, 2006
Really great article. That helped a LOT. I'm thinking if Microsoft intended to made this cryptic... anyway, thanks again!!!Anonymous
August 24, 2006
Thank you Mohamed for the helpful post!
If anybody accesses code from an custom assembly, don't forget to add the AllowPartialyTrustedCallers into the AssemblyInfo. If not, your report will run, but you may get an #Error where you use a method or member of the assembly.
Found here, and very useful anyway....
http://www.c-sharpcorner.com/UploadFile/balajiintel/CustomAssemblyinRS06302005081435AM/CustomAssemblyinRS.aspx?ArticleID=f3904516-e30a-401a-aa01-463636d78f18Anonymous
September 26, 2006
How do you get ClickOnce to deploy the rdlc files if you put the reports into a separate assemblyAnonymous
October 04, 2006
The comment has been removedAnonymous
October 04, 2006
it seems that I have my report viewer installed on D: and not in C: so in the C:windowsassembly where report viewer is looking by default is maybe the problem. How do I move my reportviewer to the GAC of the C?Anonymous
October 04, 2006
You have to provide the right path to the report: this.Viewer.LocalReport.ReportPath = Environment.CurrentDirectory + @"" + reportToLoad + ".rdlc"; this.Viewer.ProcessingMode = ProcessingMode.Local; this.Viewer.LocalReport.ExecuteReportInCurrentAppDomain(AppDomain.CurrentDomain.Evidence); this.Viewer.LocalReport.ReportPath = Environment.CurrentDirectory + @"" + reportToLoad + ".rdlc"; FileIOPermission filePerm = new FileIOPermission(FileIOPermissionAccess.Read, this.Viewer.LocalReport.ReportPath); filePerm.Assert(); this.Viewer.LocalReport.AddTrustedCodeModuleInCurrentAppDomain(System.Reflection.Assembly.GetAssembly(typeof(GDS.User.WinLanguage)).FullName); this.Viewer.LocalReport.DataSources.Clear(); foreach (System.Data.DataTable dataTable in ReportDataSet.Tables) { this.Viewer.LocalReport.DataSources.Add( new ReportDataSource(ReportDataSet.DataSetName + "" + _dataTable.TableName, _dataTable)); }Anonymous
October 24, 2006
Great article!!! A very good exam of an interesting and very useful tool... Thank you!!!Anonymous
October 27, 2006
Great post!, but what about localization? How can I localize my column headers?Anonymous
November 20, 2006
Hi I have a problem in Printing Report from ReportViewer. If I print the report, when it is in NORMAL LAYOUT...it is not getting printed. But if I print the same by setting PRINT LAYOUT...coming good. Is there any way to show the report in PRINT LAYOUT by default? OR Is there any way to set the LAYOUT mode programaticaly? Thank you Pradeep.Anonymous
December 22, 2006
This worked fine for me untill I installed the SP1 for Visual Studio. Now I got back the "Error while loading code module: ‘ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null’. Details: Could not load file or assembly 'ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified."-Error. I am still investigating. Any soloution?Anonymous
January 04, 2007
Very nice article. But I need to know how to use custom assemblies on web based report rendering. My custom assembly need to access reportserver.exe.config file, which result sin FileIO security exception. I am sure some one must have done this before.Anonymous
January 31, 2007
Hey dude, great article. For those having the "Error while loading code module:" error in web applications, try installing VS2005 SP1. But after that you win reports projects will have that error according to this: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1099668&SiteID=1 Still looking for the solution...Anonymous
February 13, 2007
Is it possible to convert rdl to rdlc and render the report at run time ?Anonymous
February 13, 2007
Is this the only way to convert RDL to RDLC and could this be done at run-time ? RDL-to-RDLC Conversion When converting a .rdl file to .rdlc format, you must manually replace the data source and query information in the report definition with data constructs provided in your application. 1. Rename the .rdl file to use the .rdlc file extension. 2. In Visual Studio 2005, open the solution or project that will contain the report. 3. Create or add the dataset that defines the data you want to use to your projector solution. For more information, see Creating Data Sources for a ReportViewer Report. 4. On the Project menu, click Add Existing Item and then select the .rdlc file that you created in the first step. 5. In the project, open the form or Web page that will contain the report. 6. From the Toolbox, in the Data group, drag a ReportViewer control onto the form or Web page. 7. In the ReportViewer Tasks smart tags panel, in Choose Reports, select the .rdlc file to use with the control. 8. In the ReportViewer Tasks smart tags panel, in Choose Data Sources, select the dataset you want to use. The dataset object, binding source object, and table object will appear at the bottom of the workspace. For more information about updating data source references, see Updating and Rebinding Data Source References. 9. Save all files, and then build or deploy the project to verify that the report contains the data you expect.Anonymous
February 19, 2007
If you are trying to access a custom assembly code in the reports and if you are getting the following error. Error while loading code module: ‘ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null’. Details: Could not load file or assembly 'ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified. Then copy the Custom Assembly dll into the following folder. PROGRAMFILESVisual Studio 8Common7IDEPublicAssemblies See the following post for more details. http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1099668&SiteID=1 Good Post Mohamed :-)Anonymous
February 23, 2007
Great article! This report control is a mess and it feels just like no more than an “idea”. Thanks to this article and all comments I finally managed to run something... Here are few pointers for those trying to display images from DB on the report. In my example I used Categories table from Northwind DB Image control available for the report control needs raw bytes to be passed in Value property. Note: System.Windows.Forms.PictureBox.Image property uses Image class. Image.MIMEType=image/bmp Image.Source=Database Image.Value=Utils.Utils.GetRawBytes(Fields!Picture.Value) Where GetRawBytes() is a static method in a referenced assembly Utils using System; using System.Drawing; using System.IO; using System.Configuration; namespace Utils { public class Utils { public static byte[] GetRawBytes(byte[] bytes) { // remove OLE header if present MemoryStream ms = null; try { int offset = 0; // use offset=78 for Northwind DB if (ConfigurationManager.AppSettings["ImageOffset"].ToString().Length > 0) offset = Convert.ToInt32(ConfigurationManager.AppSettings["ImageOffset"]); if (offset > 0) { ms = new MemoryStream(bytes, offset, bytes.Length - offset); bytes = ms.ToArray(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { if (ms != null) { ms.Close(); ms = null; } } return bytes; } } }Anonymous
April 04, 2007
Print button is not visible when i use rdl files as local reports through report viewer. Print button is not displaying and only two types of export options.Anonymous
April 16, 2007
Hi, Just sharing a "feature" on winform reportviewer which did cost a day to figure out: I got "Some parameters or crednetials have not been specified". Strange thing, I did not get them when running through visual studio (either debug or release), but ONLY when run apart from VS. (go figure that one). After working through tips here on the site, THANKS, (a few I discovered the "hard way" already), I finally figured it and just wanted to share: First do a localreport refresh, than the viewer itself: reportViewer1.LocalReport.Refresh(); reportViewer1.RefreshReport(); For some reason I had it the other way around, resulting in that mysterious error, but not within VS environment. Cheers, PieterAnonymous
April 16, 2007
Thanks for the post. What you can also do is add the copying of the dll to the PublicAssemblies folder to the post build event for the assembly in your code so you don't have to remember to do it after each build. e.g. copy E:projectsvsdev<project>CommonbinDebugProject.Common.dll "C:Program Files (x86)Microsoft Visual Studio 8Common7IDEPublicAssemblies"Anonymous
June 05, 2007
How do you format Boolean columns to display a checkbox?Anonymous
June 25, 2007
The comment has been removedAnonymous
June 25, 2007
For web apps to work with local reports and custom assemblies that are NOT installed in GAC, you have to run Microsoft Report Viewer Redistributable 2005 SP1. This is available here - http://www.microsoft.com/downloads/details.aspx?FamilyId=35F23B3C-3B3F-4377-9AE1-26321F99FDF0&displaylang=enAnonymous
June 26, 2007
The comment has been removedAnonymous
June 26, 2007
I have a report that is part of my project (is not in a DLL) so is there a way to access textboxes that I've placed on the report to populate with a value? Or do I need to put my report in a seperate DLL? I guess the part that is confusing me is when you browse to add a reference.....is this referring to the fact that the report is in a DLL? Thanks and great blogAnonymous
July 01, 2007
I have a problem with deploying reports from Visual Studio where it is remembering an old user name to try to use for deploying - it does not have sufficient permissions. Do you know how to force it to prompt for a user? For deploying, we only need to supply a URL in the project properties.Anonymous
July 09, 2007
The comment has been removedAnonymous
July 24, 2007
Excellent, concise article. Very informative will save me hours of investigation. Many thanks.Anonymous
August 12, 2007
Displaying checkboxes in microsoft reports is very simple and an interesting method can be found right here http://www.xhydra.com/dotnet/displaying-checkboxes-in-microsoft-reports.htmlAnonymous
August 21, 2007
Thanks for this amazing blog.. i have visited this blog for the first time and I am fan of it now. This article is amazing. Thank you so much.Anonymous
August 30, 2007
AS, I have used your solution but the problem of adding a data source name was not included - had to get from MS support. I still have the problem: T"he report references the code module 'System.Windows.Forms', which is not trusted" Can you help ? Regards Samee QayumAnonymous
September 19, 2007
Hello, good article, but i have donde all hte steps, and i still have the: "The report references the code module ‘System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’, which is not a trusted assembly". I have a report that in the 'Code' has a function that uses system.drawing, i write in my load event the two lines (AddTrustedCodeModuleInCurrentAppDomain, ExecuteReportInCurrentAppDomain) but it dont solves my problem, any idea please??Anonymous
September 26, 2007
Really thanks for this useful blog!! ^^Anonymous
September 26, 2007
The comment has been removedAnonymous
October 03, 2007
And that does it!! Thanks Ceasar. I inverted the AddTrustedCodeModuleInCurrentAppDomain and ExecuteReportInCurrentAppDomain and there you have it, my barcode generation code loaded. Now just to get it to format the datastream. Now I have a solution to replace lovely Crystal Reports.Anonymous
October 15, 2007
mohammed, you are the man. ever think about writing a book ;)Anonymous
October 19, 2007
Hi... I am having a report name "Report1.rdlc" Now,I want to add a textbox at runtime...How can i?? Thanks in advanceAnonymous
December 04, 2007
Thank For Your Try And Soloution...Anonymous
January 22, 2008
I found a way to deal with the problem that Raja says above at: "http://blogs.msdn.com/mosharaf/archive/2005/12/20/LocalReportCustomCode.aspx#606223" After a lot of checking, at last I found out that if you disable the "ClickOnce Security Settings" from the security section of properties of your project, it works fine. I don't know exactly why, but it works.Anonymous
January 22, 2008
sorry, in the previous comment that I posted, I typed an extra double quotation at the end of the link.Anonymous
March 02, 2008
Report Preprocessing: SQL Server Reporting Server Group PageTotals Walkthrough, Part I: the RDL and the ServerAnonymous
March 17, 2008
i have created a report rdlc and got connection to the database .... the databse has only two columns fname and lname... the query in the table adapter is: SELECT fname, lname FROM dbo.sunu where lname=@lastname i have set a report parameter "lastname" also .. i have passed the parameter value as "s" through the set parameter method and obtained it in the report also as i can see the parameter printed on my report when it is rendered... but the data is not filtered based on lname="s"?? any ideas as to what i am doing wrong??Anonymous
March 17, 2008
one more thing .. iam not using reportviewer instead i am directly writing the report to a fileAnonymous
April 02, 2008
When i try to use your function toUpperCase i get the following error message: An error occurred during local report processing. Failed to load expression host assembly. Details: Parameter count mismatch. I double checked the parameters i`m passing to the method and cannot figure out where is the problem. Even on the most simple example like =Code.toUpperCase("aaa") i get the same message! Can you please give me some clue?Anonymous
April 23, 2008
hi how to display subtotal of previous page in the top of next page in rdlc report. Ex: page 1 name acc bal a 6 10 b 7 20 sutotal 30 page 2 subtotal of first page 30 name acc bal aa 6 10 bb 9 60 sutotal 70 page 3 subtotal of second page 30 can anyone plz.. help me its urgent. Thanks in advance, umaAnonymous
April 28, 2008
Reference custom code assembly in ReportViewer controlAnonymous
May 04, 2008
The comment has been removedAnonymous
June 05, 2008
I am trying to add code to to my report. I am not very familiar with referencing the assembly. With the following code can you point me in the right direction as to how I would refrence the assembly and classes Function SetLanguage(ByVal value AS Object) As Object If value = "EUR" Return "French" ElseIf value = "GBP" Return "French" Else Return "French" End If End FunctionAnonymous
August 01, 2008
The comment has been removedAnonymous
August 29, 2008
Do you know how to refer to the report parameters in the custom codeAnonymous
September 08, 2008
It worked for me with just first step. Thanks Mohamed.Anonymous
September 13, 2008
Re: your blog - good stuff! I am just getting into Microsoft Reports, initially doing reports for display in a reportviewer and using local mode rather than a server as part of a billing system I am building in VS2005 - the objective was to find a way to avoid endless hours of typing printdocument code and to take advantage of the PDF/Excel export feature. The learning curve got pretty steep when I started to do a report that would generate an invoice. Part of the complexity is due to the fact that the viewer will be used to display multiple reports. Hopefully the following code (triggered when a selection is made from a listbox) will be helpful in giving others who might be trying to do something similar not necessarily answers but instead insights into the process. There is a well documented problem when printing with the 2005 viewer. On some printers - including both my Canon printers - the margins get messed up unless the margins are set to .5in or more. The easy solution is to just set the margins to .5in and accept the fact that they're wider than we'd prefer. I would agree with the observation that Reports and the Reportviewer are works in progress but that seems to be the case with lots of Microsoft stuff - seems like whenever we want to do something that goes beyond basic functionality that things get ugly. I wonder if any of the people who develop things there ever try to put them to use in the real world. My comments in the code are notes to myself to remind me what I did and why. switch (listBoxReportList.Text.ToString()) { //You have to manually set the datasource when doing the subreport as below //We want to display different reports in the same report viewer. //The tricky part is that we build the reports in the development environment //using the PMABillingDataSet but the program actually uses an instance of it called pmaBillingDataSet1. //After we've built the report, the we need to go into the rdlc file code for the report using the XML editor //and change the PMABillingDataSet references to pmaBillingDataSet1 to get the thing to work. //Perhaps the answer is to pass the datasetname as a parameter. //In the report viewer, we can't use embedded resource value because it would lock in the data source so we need to use //file path to get the report. In the development environment, the path is bindebugReports. //In the code below, note that the datasource(ReportDataSource - "pmaBillingDataSet1_Customer") values have to match //to match the values in bindingSourceCustomer. //Also note that if you do not uses the Reset method as shown below, the viewer holds the reference to the last //instance used and doesn't do the new report because it's trying to use the old datasource. Yuk! //Also note that after we've done it all, we have to call RefreshReport to get the report to display. //Unless the following is done, the reportviewer will not display a report containing subreports //The standard approach include private void SubreportBasicInfoEventHandler(object sender, SubreportProcessingEventArgs e) to handle the event //this.reportViewer1.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(SubreportBasicInfoEventHandler); //it and call it in Form_Load. For the moment, I created the handler here. //Note that the sequence in which things happen needs to be right. case "Customer List": System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor; buttonShowReport.BackColor = System.Drawing.Color.Chocolate; listBoxReportCustomer.SelectedIndex = 0; listBoxReportMonth.SelectedIndex = -1; listBoxReportYear.SelectedIndex = -1; reportViewer1.LocalReport.DataSources.Clear(); reportViewer1.Reset(); reportViewer1.LocalReport.ReportPath = Application.StartupPath + @"ReportsReportCustomerList.rdlc"; ReportDataSource mydatasource = new ReportDataSource("pmaBillingDataSet1_Customer", bindingSourceCustomer); reportViewer1.LocalReport.DataSources.Add(mydatasource); reportViewer1.RefreshReport(); System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default; break; case "Current Invoice": System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor; buttonShowReport.BackColor = System.Drawing.Color.Chocolate; listBoxReportCustomer.SelectedIndex = 0; listBoxReportMonth.SelectedIndex = -1; listBoxReportYear.SelectedIndex = -1; reportViewer1.LocalReport.DataSources.Clear(); reportViewer1.Reset(); reportViewer1.LocalReport.ReportPath = Application.StartupPath + @"ReportsReportInvoiceContainer.rdlc"; reportViewer1.LocalReport.EnableExternalImages = true; //the following values are for test purposes only //if the parameters are left empty, we get errors CompanyName = "Test Company Name"; CompanyAddress = "1000 Main St."; InvoiceImage1 = Application.StartupPath + "/images/InvoiceImage1.jpg"; InvoiceImage2 = Application.StartupPath + "/images/InvoiceImage2.jpg"; //This sets the string value for a parameter ("ParamaterName", value). The Parameter "CompanyNameParameter" //is declared in the report as an empty parameter. ReportParameter companynamevalue = new ReportParameter("CompanyNameParameter", CompanyName); reportViewer1.LocalReport.SetParameters(new ReportParameter[] {companynamevalue}); ReportParameter companyaddressvalue = new ReportParameter("CompanyAddressParameter", CompanyAddress); reportViewer1.LocalReport.SetParameters(new ReportParameter[] {companyaddressvalue}); //This parameter is used in the filter ReportParameter invoiceidnumber = new ReportParameter("InvoiceIDNumberParameter", textBoxInvoiceNumber.Text.ToString()); reportViewer1.LocalReport.SetParameters(new ReportParameter[] {invoiceidnumber}); ReportParameter imagelogo1 = new ReportParameter("ImagePath1Parameter", InvoiceImage1); reportViewer1.LocalReport.SetParameters(new ReportParameter[] { imagelogo1 }); ReportParameter imagelogo2 = new ReportParameter("ImagePath2Parameter", InvoiceImage2); reportViewer1.LocalReport.SetParameters(new ReportParameter[] { imagelogo2 }); ReportDataSource mydatasourceinvoice = new ReportDataSource("pmaBillingDataSet1_InvoiceLineItem2008", bindingSourceInvoiceDetail); reportViewer1.LocalReport.DataSources.Add(mydatasourceinvoice); this.reportViewer1.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(SubreportInvoiceEventHandler); reportViewer1.RefreshReport(); System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default; break;Anonymous
September 16, 2008
The comment has been removedAnonymous
November 05, 2008
Thank you so much!!! I've been trying to do this for ages. For a while I was still getting the permission error, but thanks to Ceasar everything works great. AMAZING!Anonymous
November 07, 2008
I am creating reports in SSRS 2005 that will be displayed directly from ReportServer, not within an application. In this situation, do you know of a way to hide controls based upon whether the report is rendered on screen or printed. (I want to hide some controls on printed output.) Thanks in advance, DougAnonymous
December 10, 2008
The comment has been removedAnonymous
March 10, 2009
The comment has been removed