How to use Lambda functions with embedded code in SSRS 2008 R2 reports? - vb.net

Using SSRS 2008 R2 with embedded code, the VB.Net code compiler seems to err whenever I need to do anything beyond the simplest of statements.
For instance, I want to use the LINQ Select function to perform some operations on the elements of a collection, but BIDS keeps throwing compiler errors on lines of perfectly valid VB.Net code.
Public Function SplitToIDs(ByVal multiValue As String) As Integer()
Dim regex As New System.Text.RegularExpressions.Regex("((?>.*?);#\d*;#)", System.Text.RegularExpressions.RegexOptions.Compiled Or System.Text.RegularExpressions.RegexOptions.ExplicitCapture Or System.Text.RegularExpressions.RegexOptions.CultureInvariant Or System.Text.RegularExpressions.RegexOptions.IgnoreCase)
Dim results As New System.Collections.Generic.List(Of String)()
Dim matches As System.Text.RegularExpressions.MatchCollection = regex.Matches(multiValue)
For Each mvMatch As System.Text.RegularExpressions.Match In matches
results.Add(mvMatch.Value)
Next
'Dim pairs As String() = SplitToPairs(multiValue)
'Dim names As System.Collections.Generic.IEnumerable(Of Integer) = System.Linq.Enumerable.Select(pairs, Function(p) Integer.Parse(Microsoft.VisualBasic.Split(p, ";#")(1)))
Dim names As System.Collections.Generic.IEnumerable(Of Integer) = System.Linq.Enumerable.Select(results, Function(p) Integer.Parse(Microsoft.VisualBasic.Split(p, ";#")(1)))
Return System.Linq.Enumerable.ToArray(names)
End Function
On the line 42, which is the comment immediately above where I call System.Linq.Enumerable.Select, Visual Studio 2008 (the VS Shell installed by SQL Server 2008 R2), gives this error when I attempt to preview my report:
An error occurred during local report processing.
The definition of the report '/XXXX' is invalid.
There is an error on line 42 of custom code: [BC30201] Expression expected.
I have already learned that [BC30201] is a generic error with little relation to a missing expression. As seen in my code, I have fully namespace qualified every function or variable beyond the most basic System namespace classes. In the Report Properties dialog, References tab, I've already referenced both mscorlib and System.Core to make sure all the functions I'm using in my code can be resolved to a System assembly.
So a few questions...
Can Anonymous Functions be called and used in SSRS 2008 R2 Reports' embedded code?
In getting to this point, I was getting the same [BC30201] error when I attempted to call one method in the embedded code from another. Yet, I was sure I had some simpler methods in an earlier iteration of this report (or another report) were this actually worked... Should we be able to call one custom method from another within the embedded code?
Notes:
I'm highly tempted to create a separate code module, but I really don't want to fight the political battle necessary to convince lots of people that we should install custom assemblies on our Reporting servers. But unless, I can call those assemblies directly from various report expressions, I may still need to use the embedded code to make the assemblies' methods available to the report.
The source data comes from a SharePoint 2010 list. Many of the columns are Lookup columns that allow multiple values. The method above strips out the delimiters and values to return a collection of IDs.

Related

visual basic in visual studio 2017 intellisense if statement with parentheses broken

Since updating to Visual Studio 2017 I have observed unwanted behavior when constructing if statements, in that intellisense doesn't recognize variable names when I attempt to surround the clause in parenthesis. Consider the following incomplete code:
class AnythingClass
....
end class
public Sub doSomething()
Dim anythingInstance as new AnythingClass
if anyt
When typing code in this manner, the instance "anythingInstance" will be suggested by autocomplete. However, if I open parentasis when starting the if clause as below
class AnythingClass
....
end class
public Sub doSomething()
Dim anythingInstance as new AnythingClass
if (anyt
then the declared variable will not show in the autocomplete suggestions. The class name AnythingClass will show up in the suggestions, but not the instance
UPDATE
(edit) THIS ANALYSIS IS INCORRECT, please see my answer for details as to why
I have tried creating a brand new project and get the expected behavior. So this only appears to happen in projects that have previously been upgraded from older versions of visual studio.
I have attached a couple of screenshots for the skeptics!
Based on comments from #Maciej Los and #TnTinMn, I have managed to piece together the issue.
If you write the code as:
IF (an...
with a whitespace between the IF and the arguments, Then autocomplete appears to treat it as an If statement and will display instance variables. However if you write the code as
IF(an...
with no whitespace, autocomplete appears to treat it as the If operator and not show declared instances. Quite why this is the case, I don't know. My original analysis of there being a difference between new projects and previously imported ones was incorrect.

Late Binding & Type Issues In VB

I'm trying to run my code which was originally created using Visual Studio through another application where late bindings are disallowed and this option cannot be altered unfortunately. I am very new to programming in general and struggling to get my head around the issue. Here is the code im using in the invoke code stage:
Dim objIEShell As Object = CreateObject("Shell.Application")
Dim objIEShellWindows As Object = objIEShell.Windows
Dim objIEWin As Object
For Each objIEWin In objIEShellWindows
If InStr(objIEWin.LocationURL,"google")>0 Then
objIEWin.Quit
objIEWin = Nothing
End If
Next
The code simply closes all instances of Internet Explorer with "google" in the URL. This is the error message I get when trying to compile it:
Message: Error compiling code
error BC30574: Option Strict On disallows late binding. At line 2
error BC32023: Expression is of type 'Object', which is not a collection type. At line 4
From the research I've done so far I realise the first error message on line 2 is to do with the type difference between objIEShell and the Windows method. I think I have to convert objIEShell like this, CType(objIEShell,?), but I don't know the type of the .Windows method or how to find this out. Also any insight on how the fix the second error would be greatly appreciated as I'm not sure where to start with that one either.
This dates back to the wonky days when Microsoft still had plans to make Explorer behave like a web browser. Makes it pretty hard to arrive at the correct code, it is a combination of two separate COM components that don't have much to do with each other.
You need to first add two references to those components so the compiler understands the names. Use Project > Add Reference > COM tab and tick "Microsoft Internet Controls" and "Microsoft Shell Controls and Automation". That adds the Shell32 and SHDocVw namespaces.
Now you can write the code early-bound like this:
Dim objIEShell = New Shell32.Shell
Dim objIEShellWindows = CType(objIEShell.Windows, SHDocVw.IShellWindows)
Dim objIEWin As SHDocVw.WebBrowser
For Each objIEWin In objIEShellWindows
If InStr(objIEWin.LocationURL, "google") > 0 Then
objIEWin.Quit()
End If
Next
The CType() expression is probably the most unintuitive one, the Shell.Windows property is of type Object to break the dependency between those two components. The cast is the necessary voodoo to keep the compiler happy.

Variable declaration syntax

Basically I follow a spec to create functions in Access 2010. These functions are in VBA. When working with record sets the given declaration in the spec is
Dim obj.Recordset As New ADODB.Recordset
Yet every time I try and write it I get a syntax error so I just use:
Dim Recordset As object
I am not sure if this means the same thing but it compiles and seems to work fine. Basically my question is, is the given declaration for a recordset correct and is my alternative acceptable. Also Access 2010 is used as a user front end and the database is stored in MS- SQL server 2008 backend.
It looks like you are trying to define a variable with a '.' in the variable name. That is not a valid character in a variable name. If I didn't know better, this syntax looks like you are trying to somehow assign a data type of ADODB.Recordset to a property named 'Recordset' of a class object named 'obj' (which would be extremely bizarre and I don't know of a valid syntax for in VBA or why anyone would want to). I would expect the following will compile:
Dim rst As New ADODB.Recordset
Also make sure you have added the appropriate reference in Tools --> References (Microsoft ActiveX Data Objects 2.0 Library or other latest version). As to your second question, that should be a viable alternative but I prefer the strongly typed former.

Visual Basic Member Already Exists in an object module

I am brand new to visual basic today and I was writing my program but I get a compile error
Member already exists in an object module from which this object module derives
My function prototype is
Function calculate(count As Integer) As String
I was wondering why this is giving me this error
It would appear that you are designing a form, and have an object on that form (such as a text box or command button) which is also named calculate, disregarding case, since VBA is generally not case sensitive.
It is because your function has same name as the system's reserve function name, such as AfterInsert()

Invalid Cast Exception when filling datatable wth table adaptor

I am using VB.NET 2010 (Visual Basic 2010 Express) on a WPF-based project. I am also using the SQL Server Express built-in to Visual Basic 2010 express.
I have just about finished refining my code for hooking up my wpf-based form to an existing SQL database (agentroster.sdf). I have a global data source (AGENT_ROSTER) connected to this database. Connections are confirmed to work properly.
This is the first part of the code I'm using, irrelevant code omitted,
Dim table_adaptor As New AGENT_ROSTERTableAdaptors.AGENT_ROSTERTableAdaptor
Dim roster_table As New DataTable("roster_table")
Dim rowposition As Integer
Private Sub ROSTER_Loaded...
table_adaptor.Fill(roster_table)
End Sub
I am getting the following errors:
(In Immediate Window)
A first chance exception of type 'System.InvalidCastException' occured in VBP-WORD4WORD.exe
(In Message, pointing to the line: "table_adaptor.Fill(roster_table)')
InvalidCastException was unhandled
Unable to cast object of type 'System.Data.DataTable' to type 'AGENT_ROSTERDataTable'.
What am I doing wrong, and furthermore, how do I fill roster_table with table_adaptor (or alternate method)?
Assuming that your strongly typed DataSet is called "AGENT_ROSTER":
Dim table_adaptor As New AGENT_ROSTERTableAdaptors.AGENT_ROSTERTableAdaptor
Dim roster_table As New AGENT_ROSTER.roster_table
table_adaptor.Fill(roster_table)
Have a look at Efficient Coding With Strongly Typed DataSets.