Custom code in Reporting Services report - reporting-services

In Reporting Services I would like to add a parameter that contains data from a custom code block. Ideally, I would be able to run the following code (this is a simple testing example):
Function GetPeriods() As String()
Dim values As System.Collections.ArrayList =
New System.Collections.ArrayList()
For i as integer = 1 to 24
values.Add(i)
Next
Return values.ToArray()
End Function
and put the following in the "Text Field" of the parameter:
=Code.GetPeriods()
However, when I run the report, the parameter I apply this to is disabled and empty. Is there a different technique that should be used? Or am I doing something wrong?

If you're using SQL 2008 Reporting Services then you can have a look at this page which introduces the concept of using custom assemblies.
If you're using SQL 2005 Reporting Services then this link is the one you want.
It's a mostly trivial thing, simply compile your code into a class library and follow the instructions provided to allow your report to reference it.

You are returning an array item (an array of strings) into a text field. Instead, try returning a plain string. That should work. If you would still like to return an array list, you must basically bind it to a list control in your RDL. You can definitely do that with dataset extensions. However, I am not sure if there is any other easy way. Check the proprties of the list control and see if it allows you to directly bind to an array list.

You can create the same stored procedure on SQL Server and load parameter values from that procedure.

To access your members/functions implemented in custom code of SSRS report you should set the access modifier to "Public":
Public Function GetPeriods() As String
...
see article Writing Custom Code in SQL Server Reporting Services

I've been trying to do this same thing, set a simple list of parameter values from report code. None of the links in any of these answers shows how to do this and after quite a bit of digging around I don't think it's even possible. Yes it is possible to get the values from a database query, from a web service, or from a custom assembly, but each of these creates a lot of overhead compared to getting the list from a simple function call like =Code.GetValues(), where the function uses a For loop to create the values.
msvcyc is correct in pointing out that the parameter is expecting a string value, but the function is returning an array. I changed the return type to Array as suggested by prashant sable, but the select list is still grayed out, it does not work. And coldice is correct in saying that the access modifier should be Public.
In my digging around I found an article by James Kovac from 2005 that pointed out why this is not possible. The Parameters class has a get method, but no set method. In the VS 2008 object browser for SSRS 2008 the object name has changed, but it still does not contain a set method (see Microsoft.ReportingServices.Interfaces.IParameter.Name or .Value).
My current workaround is to just hard code the list of values, but if your value list needs to be dynamic then your only choices are database queries, web services, or custom assemblies. I think the easiest workaround of these three is to get the values from the database engine, as suggested by oleksiy.t, as long as you can write a query to return the value list you want. Your list of integers, or my list of time intervals, would both be easy queries to write. Otherwise you will need to use one of the other two workarounds.

I checked your code. The only thing that's wrong is your function returns String(). When I changed your method signature to return Array, it worked fine, in my report.
Change the signature to Function GetPeriods() As Array

Everything I've seen requires parameters and their respective settings to be part of the RDL.
That being said, if you're going to "hardcode" the values, you could create a dataset just for the report, perhaps in XML, or if it needs to be programmatically driven, do it in a web service.

Related

Passing Variables Between Forms

Forgive me if this has already been asked- I can’t seem to find a well written answer.
I am developing a small application for personal use.
Essentially what I have is two forms. Form 1 is a master view of all my contacts listed on a data grid view. Form 2 will be loaded on the cell/row double click of a particular record in order to edit it’s details.
My question is, what is the best practice/method for achieving this? I have seen many different methods.
Should I:
Pass only the primary key of the selected row then populate the fields on form 2 load
Pass all fields as a variable within a class then populate form 2 from that
Maybe I’m headed in the complete wrong direction though.
I have tried both ways, but wondering what the best method is for scalability.
My personal preference would be to pass a datarow into the opening argument of the form (rather than the PK / all the variables). You can then use the datarow inside your Form2 to bind to your controls or set their values, whichever you think is appropriate.
There are some useful examples on working with a datarow if you're unsure, alternatively you can also check out Microsoft Docs.
Public Sub New(ByVal row As DataRow)
InitializeComponent()
' your code for working with row here
End Sub
Edit:
In terms of "Binding" vs "Setting", you can either have your controls linked to your data to be two way (as you edit the data in a control at run time you alter the data in your database) or you can just set the values of the controls.
E.G. TextBox1.Text = row(0)("ColumnName")
You can find more on data binding on the Microsoft Docs page

SSRS - Programmatically flag when a report is created as a snapshot

Is there a way to programmatically determine if a report is being run as a snapshot?
I have a suite of reports that need to behave differently when a snapshot is created than when run by the end user.
Differences include:
Tablix and column visibility
Multi-parameters (both available choices and defaults)
Expressions in textboxes in header
Expressions in tablix cells
I have been able to work around most of these issues but it is a series of kludges. Ideally I would like an IsSnapshot() function that I can use throughout the report.
I have been able to use IIf(User!UserID = "",True,False) in expressions to flag scheduled snapshot (but not manually triggered ones).
Unfortunately SSRS realises that User!UserID is volatile and so won't let me use it if I also want to use Data Driven Subscriptions: "You cannot create a data-driven subscription on a report that contains the User!UserID expression"
In order to work round this I made my own public function in the report "Code" properties:
Public Function UserName()
Try
If IsDBNull(Report.User!UserID) Then Return ""
If IsNothing(Report.User!UserID) Then Return ""
Return Report.User!UserID
Catch
Return ""
End Try
End Function
My report therefore uses IIf(Code.UserName() = "",True,False) as a snapshot flag.
My main issue with this is that I am then using the absence of a username to work out if I should show all data (as I want to in my snapshot). This seems like a security flaw in the making.
I would much rather have an explicit Snapshot flag and not rely on some byproduct of the snapshotting process!

MySQL result to array VB.NET

I am creating a program in VB.NET which uses an online MySQL database to retrieve certain data. I have now succeeded in connecting and getting some basic stuff out of it. Now, what I want to do is that when an user presses a button it has to update the list. What happens is that the stuff that is already in the database also gets resent and so the list just doubles himself, although it adds the new database value.
How can I make sure that it only adds new values to the list, instead of adding all values from the list again? I have read that you can use the Preserve keyword with arrays, though this isn't an array and neither have I figured out how to convert my data into an array.
Private Sub generateList()
DB.writeQuery("SELECT snacks.ID, snacks.naam, snacks.baktijd FROM snacks")
While DB.DR.Read
ListBox1.Items.Add(DB.DR.Item("naam"))
End While
DB.closeConnection()
End Sub
This is the piece of code I use to generate the list, I reuse this code to refresh the list. As you can see, it uses my own written MySQL class. I know it makes an connection so there is nothing wrong with that.
Any help would be appreciated.
What you will want to do is to clear the ListBox before you reload the list. You can do this like so:
ListBox1.Items.Clear()
Then when you reload the list, the items will not be duplicated.
Looks like you need to clear your listbox before adding the items.
ListBox1.Items.Clear()
Try this my friend:
If Not listBox1.Items.Contains(DB.DR.Item("naam")) Then
ListBox1.Items.Add(DB.DR.Item("naam"))
else
'this item already exists.
end if

failed to lock variable during move and rename of files in SSIS

Newbie question. I believe this is one of the most common errors. I found several of them on msdn forum itself but probably there are many ways on achieving this error? Please help.
I am trying to move and rename some images from one folder to another (and yes I have seen the blog by Rafael Salas and many others but none of them helps).
Like moving from \server1\images\123-456.jpg to \server2\images\123456.jpg
I am using a foreach.
Source variable is built dynamically. In the first iteration #imagePath = \server1\images\123-456.jpg ( i checked using messagebox.show)
I have defined #remoteImagePath = \server2\images\ (which never changes) and #revisedImageName = 123456.jpg (built dynamically in script task using string replace - also checked using messagebox.show)
In FileSystem Task, I am using SourceVariable as #imagePath and using Expressions to define Destination as in #[User::remoteImagePath] + "\" + #[User::revisedImageName]
Dont know for what reason, I am getting this error
Failed to lock variable "\server2\images\123456.jpg" for read access with error 0xC0010001 "The variable cannot be found. This occurs when an attempt is made to retrieve a variable from the Variables collection on a container during execution of the package, and the variable is not there. The variable name may have changed or the variable is not being created.".
Set the IsSourcePathVariable to True and set the SourceVariable to a SINGLE variable.
If you use the expression editor to set Source using multiple variables, or anything else apart from a SINGLE variable it will not work. If you want to concatenate hard-coded strings to variables, or multiple variables, do this by creating a new variable in the package, and use the Expressions tab in the variables tab to build the variable as a single variable.
I believe expressions editor needs syntax
#[User::remoteImagePath] + "\\" + #[User::revisedImageName]

Problem with the row count transform

I currently deployed an SSIS package (Developed on the 2005 version) (developed on my local server) in a pre production environment for testing. I have used the Row count transform to get a count of good/bad records. It works fine on my local system . However when i deploy this on the pre prod server, the row count does not work! (as in it does not recognize the vairbales i have assigned to the relevant transofm - no drop down abvaliable in the variables attribute part. tried deleting and adding a new transoform.. no luck.
Strangely this does not work for any of the other packages also present/deployed on the same server (tried this out by dropping an rc tramsform onto an existing package... same problem)
Any suggestions?
Thanks a tonne
If you are having problem with the row count transform, another alternative that we use here at my company is creating a script component and incrementing a rowcnt variable by one. The performance is just as good-just add this code:
Public Overrides Sub PostExecute()
MyBase.PostExecute()
Me.Variables.rowcnt = Me.Variables.rowcnt + 1
End Sub
This certainly seems odd. Are you saying that when you are in the Advanced Editor for Row Count, under Custom Properties, that the drop down beside VariableName has no options? You should at least see all of the System:: variables.
If the User:: variables are not listed, my first suspicion is that they do not have the correct scope to be visible in the Data Flow Task where you have your RowCount.
When you go up to the Control Flow, and get the Variables list, do you see your user variable there? What is the scope of it?
Note that I recognize that none of this fits with "it works locally but doesn't work when copied to the server", but it is at least where I would start...