SSRS expression for empty parameter value returns #Error - sql-server

I have a parameter in report that I recently changed it to accept NULL values. It was a required parameter before but now it is not required. Header in the report has below expression to read data from the parameter value. Ever since I modified the parameter to accept NULLvalues, it is erroring out. It shows #Error in the header. I have tried modifying the expression to display result even if the parameter is 'NULL', but nothing has worked so far. I am pretty much stuck here. Here is the expression I am using in SSRS:
=IIF(Code.MultipleValues(Parameters!ProductID.Value(0))="True"
,IIF(IsNothing(First(Fields!ProductName.Value, "ProductHeader")),""
,First(Fields!ProductName.Value, "ProductHeader")).ToString
,IIF(Len(Parameters!ProductID.Value(0)) > 0
,IIF(First(Fields!ProductName.Value, "ProductHeader") is nothing,""
,First(Fields!ProductName.Value, "ProductHeader")),"All Products Selected"))
I believe the error has to do with the VB custom code that is in the report. I am not too familar with the code so I am having trouble troubleshooting it.
VB code:
Public Function MultipleValues(ByVal str As String) As String
If str.Contains(",")
Return "True"
Else
Return "False"
End If
End Function
Any suggestion on how to handle #Error in SSRS? thanks

Replace
Code.MultipleValues(Parameters!ProductID.Value(0))="True"
with
IndexOf(Parameters!ProductID.Value(0).ToString(),",") >= 0

Instead of a function, can you just put the IsNothing check in front of the parameter ?
=IIF(Code.MultipleValues(IsNothing(Parameters!ProductID.Value(0),""))="True"

Related

SQL Server 2012 assembly execution failure - System.NullReferenceException: Object reference not set to an instance of an object

I am trying to execute a UDF which uses a CLR assembly. Each time I try to run it I get the below error:
Msg 6522, Level 16, State 1, Line 45
A .NET Framework error occurred during execution of user-defined routine or aggregate "Geocode":
System.NullReferenceException: Object reference not set to an instance of an object.
at ProSpatial.UserDefinedFunctions.GeocodeUDF(SqlString countryRegion, SqlString adminDistrict, SqlString locality, SqlString postalCode, SqlString addressLine)
It is a geocoding assembly, which is based on the below blog:
https://alastaira.wordpress.com/2012/05/04/geocoding-in-sql-server-with-the-bing-maps-locations-api/
Is there anything I can do to fix it? It was working fine, but just stopped working recently. I changed the Bing Maps key to a new one, but that hasn't resolved the issue. Anyone have any ideas?
Cheers
Edit: I entered a formatted URL into Chrome, to see if I can get the error. I entered the below URL format:
http://dev.virtualearth.net/REST/v1/Locations?countryRegion={0}&adminDistrict={1}&locality={2}&postalCode={3}&addressLine={4}&key={5}&output=xml
All I did was to replace items 0 to 5 with their respective entries. I left the curly brackets in there, and then also tried it without the curly brackets. Without the curly brackets, the URL returned a result with no issues.
In the browser, I am getting geocoded results, but not in SQL any more
Just worked it out. One of the address fields that was being geocoded was null in the database, and the API did not like that. So I removed that from the geocoding list
Looking at the code in that blog, if you are passing in a NULL, then it's not the API that's throwing the error. The error is happening because NULL values are not being handled when the method is first called. The code just casts the SqlString input parameters to string without first checking to see if there is a value in the SqlString variable. SqlString is very similar to a nullable string.
Fortunately, it is rather easy to adjust the code to properly handle NULL values in the input parameters. Starting with a redacted snippet of the original code below, I will show how to do this for one parameter and you can repeat that modification for the others (well, the ones that might actually be NULL in the DB; no need to do this for parameters that are guaranteed to be there due to being in a NOT NULL column).
public static SqlGeography GeocodeUDF(
SqlString countryRegion,
SqlString adminDistrict,
SqlString locality,
SqlString postalCode,
SqlString addressLine
)
{
...
string localAdminDistrict = string.Empty;
if (!adminDistrict.IsNull)
{
localAdminDistrict = adminDistrict.Value;
}
// Attempt to geocode the requested address
try
{
geocodeResponse = Geocode(
(string)countryRegion,
localAdminDistrict,
(string)locality,
(string)postalCode,
(string)addressLine
);
}
All I did was:
Added a local variable for localAdminDistrict, defaulted to an empty string.
Added an if block to set localAdminDistrict if adminDistrict is not null.
Updated the call to Geocode() to use localAdminDistrict instead of (string)adminDistrict.
This should work as long as the Bing Maps API is ok with an empty value for adminDistrict (i.e. ...&adminDistrict=&locality=... ), as opposed to adminDistrict not being present in the GET request (which is easy enough, it just requires an additional modification).
ALSO: As long as you are going to be in there updating the code, you really should move the calls to the Close() and Dispose() methods into a finally block for that try / catch.
For more info on working with SQLCLR in general, please visit: SQLCLR Info

Report column is showing #Error when using Subscriptions

As titled, when using SSRS 2017 subscriptions to generate a report, a column with expression is showing #Error. However, when I try to run manual in web and in the report builder, it can be shown.
The Expression is:
=Code.Decode(Fields!Comments.Value)
The function is:
Function Decode(ByVal EncodedString AS String) AS String
Return System.Web.HttpUtility.HTMLDecode(EncodedString)
End Function
Any suggestion or help will be welcome.
I resolve the problem by myself. Use the following method instead
Function Decode(ByVal EncodedString AS String) AS String
Return System.Net.WebUtility.HtmlDecode(EncodedString)
End Function

Lua - Evaluate script as a value using C API

I would like to use Lua not only for writing scripts in separate files, but I would also like to embed Lua expressions into configuration properties.
For example :
{
"MyConfigProperty" : "Hello mister {{ app:getUserName() }} !"
}
Here app:getUserName() would return a string and the the whole script contained in {{ }}, which is actually just an expression, could be evaluated to a string value.
How could I "call" this piece of Lua code so that it is evaluated and leaves a string value on the stack ?
One solution could be to wrap it into a "return (" + expression + ")" as suggested in the Lua documentation.
Running this should (I guess) leave the resulting string on the stack as a return value. But this could be a problem because then I would not be able to determine when a string was successfully returned and when an error message was returned do to a failure in the execution.
How could I do that using the C API without losing the capability of detecting errors ? Thank you.
EDIT : Another solution could be to prefix with return nil, and then check the type of the first return value. If the first return value is nil, get the successfully evaluated value which is the second return value. Else if the first return value is a string, an error occured. There might have been a cleaner approach though.

Split array as input parameter

In a class module named, I have
Private pARRactivityPred() As String
Public Property Let predArray(Value() As String)
pARRactivityPred = Value
End Property
And calling it:
record.predArray = Split(string1, ",")
However, i am not sure why i get the following error:
"Compile error: Definitions of property procedures for the same
property are inconsistent, or property procedure has an optional
parameter, a ParamArray, or an invalid set or final parameter"
Does anyone know whats going on?
This works:
Dim s() As String
s = Split("a,b,c,d", ",")
record.predArray = s
record.predArray expects a String array as input, but Split returns a Variant array, which causes a type mismatch error. Here I convert the output of Split to a String array and it works. This conversion can be done automatically using the assignment operator = as above, but it won't work through the input parameter of a procedure like predArray. The parameter has to be of the specific type specified in the procedure declaration: Value() As String.
I see that #mehow pressed the "answer" button a minute before I did :-) However I think that using a loop to convert from Variant array to String array like he does is unnecessarily long-winded.
However I am unable to reproduce your exact error. With your code I get a compile-time "type mismatch" error for the reasons outlined above -- not the error you describe.

change image in report builder using iif function

I'm working with sql server report builder and I'm trying to change the value of an image according to a chosen date from a parameter.
I've selected external as source and in the expression window I have something like this:
=iif(Parameters!Date.Value <> 11.04.2013, "http://rack.0.mshcdn.com/media/ZgkyMDEyLzEyLzA0L2I1L3doZXJlZG9nb29nLmJoTi5qcGcKcAl0aHVtYgk5NTB4NTM0IwplCWpwZw/4931e287/304/where-do-google-doodles-come-from--ff2932470c.jpg", " ")
but I receive the following message:
Argument not specified for parameter 'TruePart' of 'Public Function IIf(Expression As Boolean, TruePart As Object, FalsePart As Object) As Object'.
I'm pretty new to work with the report builder, so please someone help me find out the solution.
I pasted your expression into a sample report set up with a parameter Date, data type Date/Time, and got the same error when using the expression.
I resolved this by changing the data being compared to the parameter in the expression into a string that can readily be converted to a date, so from 11.04.2013 to "11-Apr-2013":
=iif(Parameters!Date.Value <> "11-Apr-2013", "http://rack.0.mshcdn.com/media/ZgkyMDEyLzEyLzA0L2I1L3doZXJlZG9nb29nLmJoTi5qcGcKcAl0aHVtYgk5NTB4NTM0IwplCWpwZw/4931e287/304/where-do-google-doodles-come-from--ff2932470c.jpg", " ")
This stopped the error occurring for me and worked as expected when I selected different dates; i.e. the URL on all dates except 11-Apr-2013.

Resources