Is it possible to write extension methods for expressions behind RDLC fields?
For example, let's say that I have a DateTime field in my datasource that may either have a valid value or may be null. I drag and drop a TextBox onto my RDLC and format its value using the ToShortDateString() method. This works fine for populated DateTime value, but this will also obviously throw an exception at runtime if I try to do a .ToShortDateString() on a NULL field.
I was wondering if I could write an extension method that I could use in my RDLC expressions so that when I'm dealing with ?DateTime values, I could call a method like .ConvertFromNullToEmptyString().
Of course there are other ways to work around this issue, but I was wondering if extension methods for use in RDLC expressions would be a possible approach to my business problem.
Thanks folks!
Yes, this is possible. You can either embed code directly in the report or include a custom assembly.
It is possible to use extension methods, but not AS extension methods on an instance of an object. You would have to call them as a static method call on the type of which they are a member. So instead of myDictionary.Values.Sum() -- calling the Sum method on the Values property of a dictionary instance -- you could use System.Linq.Enumerable.Sum(myDictionary.Values) -- passing the instance into the static Sum method of the Enumerable type (in this example, the report must reference the System.Core assembly). So yes, you can use methods that are also extensions, but (it appears anyway) not as extensions on a particular instance.
While I agree with Corina on the solution to the question, I believe a better solution can be reached without going the route she suggests, using built in expressions. In any case where you have a DateTime coming from SQL, you're correct, it can be null, however, you can easily test for this using an IIF statement (remember that the expressions are basically in VB) to check for null / nothing / empty and as long as it is something, run the desired operation, otherwise return blank. Just be careful, as the resulting type of the IIF will probably be string.
For example, let's say that I have a DateTime field in my datasource that may either have a valid value or may be null. I drag and drop a TextBox onto my RDLC and format its value using the ToShortDateString() method. This works fine for populated DateTime value, but this will also obviously throw an exception at runtime if I try to do a .ToShortDateString() on a NULL field.
There should be no need for a custom function in your case. Just use VB's If() ternary operator:
=If(Fields!MyDate.Value IsNot Nothing, Fields!MyDate.Value.ToShortDateString(), "N/A")
(Personally, I funnel my objects through AutoMapper and let it substitute null values with default values or objects, so that I don't have to deal with null values in the report at all).
It should be mentioned, that the If(condition, true_part, false_part) ternary operator should (with one I) be preferred over the IIf(condition, true_part, false_part) function (with two I's) in most cases.
The If() ternary operator will short-circuit evaluate only the part that corresponds to condition outcome (e.g. a condition that is True will only evaluate the true_part).
The IIf() function will always evaluate the condition and both parts, because it is just a function call and all parameters of the function will be evaluated before the call.
Related
to realize pagination in my pipeline where I execute a REST GET Request I need to pass the next_offset value from the output to the PaginationRules.
I can do this by typing $.next_offset to the dynamic content field but I get an error when the pagination is finished and returns -1 as next_offset value.
Unfortunately I get an exception when the value is lower then zero. Instead it only succeed when the HTTP status code is 204 (No Content), or any of the JSONPath expressions in "paginationRules" returns null.
Because of that I wanted to add an If condition to the dynamic content field which looks like this now:
#if(equals('$.next_offset','-1'),'$.next_offset',null)
Unfortunately it is not possible to use the JSON path expression like this inside of a logical function I guess.
I would be glad if you can tell me how a JSON Path expression needs to be entered in logical functions.
Many Thanks in advance!
Unfortunately, there is no way to use the JSON path expression like this inside of a logical function. I think you need to change to return value of next_offset in your application.
I need to write an SSRS expression to check and replace NULL field value with another field value. Can this be done?
=iif(isNothing(Fields!FV1.Value), Fields!FV2.Value, Fields!FV1.Value)
If you have to do it a bunch of times, you can also make a reusable function to avoid a lot of typing. Here's a solution modeled off of SQL's ISNULL function:
Right click on the Report Document and go to Report Properties.
Navigate to the Code tab and add the following function:
Public Function IsNull(input As Object, defaultValue As Object) As Object
Return IIf(input Is Nothing, defaultValue, input)
End Function
Note - Even though the custom code is expecting valid VB.NET code, you have to use the IIF Ternary operator.
Then you can use it in an expression like this:
=Code.IsNull(Fields!MyField.Value,0)
I am trying to create a custom IsNull Function in Crystal Reports; the function must act the same way as the IsNull Function in MS SQL Server. I want to specify a field, and if the field is null, then it must be returned with a value I have specified.
IsNull({myField},0) or
IsNull({myField},'Hello World')
I have encountered that I have to create a separate function for number fields and a separate function for text fields. I also found that Crystal does not allow the use of standard functions inside of a custom function, for instance the ISNULL Function:
Function(NumberVar param, Numbervar setter)
IF ISNULL(param) THEN setter ELSE param
and
Function(StringVar param, StringVar setter)
IF param = NULL THEN setter ELSE param
Does anyone know how I can create a function like this in Crystal and a work around for the ISNULL inside of a custom function?
You can't pass a null value into a custom function, so it's pointless to use crystal's isnull function inside one. Only option is to write it like...
if isnull({myField}) then 0 else {myField}
I found this issue, in the formula editor there is a drop down in the header that indicates:
Exception for Nulls
Default values for nulls
Select the second one ( Default values for nulls)
I've encountered the same behavior, but I have yet to see a documented reason for this.
I would suggest that you use a SQL Expression:
//{%myField}
(
ISNULL({myField},'Hello World')
)
This worked for me:
if (isnull({dbvalue}) or ({dbvalue} ='')) then
"Display the required text"
else
{dbvalue}
Item in the recordset rstImportData("Flat Size") is = Null
With that, given the following statement:
IIF(IsNull(rstImportData("Flat Size")), Null, cstr(rstImportData("Flat Size")))
Result: Throws error 94: Invalid use of Null
If I change the statement by removing the type conversion upon a false comparison:
IIF(IsNull(rstImportData("Flat Size")), Null, 0)
Result: Null
It returns Null as it should have the first time. It appears that I cannot do a type conversion in an IIF if the value passed in should ever be null even if it passes an IIF test, it still attempts to evaluate it at both the true and false answer. The only reason I'm using IIF like this is because I have a 25 line comparison to compare data from an Import against a matching record in a database to see if I need to append the prior to history.
Any thoughts? The way data is imported there will be null dates and where the spreadsheet import is in a string format I must convert either side to the other to compare the values properly but if either side is null this exception occurs :(
EDIT
Example of why I was using IIF (and considering using a universal function)
If master("one") <> import("one") Or _
master("two") <> import("two") Or _
master("date") <> import("date") Or _ //import("date") comes from a spreadsheet, it comes in as string, CAN be a null value
master("qty") <> import("qty") Or _ //import("qty") comes from spreadsheet, comes in as a string can CAN be null
master("etc") <> import("etc") Then
....stuff....
End If
This code expands for roughly 20 columns to compare in the database. I would prefer to check as part of the statement. I can think of a bunch of solutions but they involve adding much more code. If that is the case power to it, however I'm not one to give in so easily.
Options I see are
Creating temp vars to do the work prior to comparing and using these new vars instead of the recordset
Creating an object to pass the record into to preformat and work with, though extra work would provide this functionality to each import type since there are different files with similar fields
I'm here for ideas, and I'm open to any interesting pieces that can be thrown my way as I get to decide how to do it I'm looking for the most reusable approach.
The simple expedient of changing the value to a string helps tremendously. The trick is that trimming a string which is NULL will get a null string. Which can then be operated on as if it wasn't a database null.
I frequently use the form:
CInt("0" & Trim(SomeVariant & " "))
To get a valid number without having to go through a bunch of hijinks. The null is a nonentity for this problem.
The behavior you described is the standard way IIf operates under VBA. This is part of what Access 2003 Help says about it:
"IIf always evaluates both truepart and falsepart, even though it returns only one of them. Because of this, you should watch for undesirable side effects. For example, if evaluating falsepart results in a division by zero error, an error occurs even if expr is True."
However, if you use an IIf statement in a query, evaluation short circuits after truepart when expr is True --- falsepart is not evaluated in that case. Unfortunately this information is not useful for you ... unless you can incorporate a query into your comparison.
I don't know of any other way to avoid your error with IIf. I would try appending the Excel data into a table whose structure matches that of the table you will compare against, thereby eliminating the need to do a string conversion at the same time you do the comparison.
I have a textbox in my SSRS 2005 report. The expresssion for this textbox is:
=IIF(IsDBNull(Fields!fOrgID), Code.SetMyVar("null"), Code.SetMyVar(Fields!fOrgID.Value))
I have also tried IsNothing(Fields!fOrgID) and a few other variations of checking for nulls.
I have modified the SetMyVar function for testing and it now looks like this:
Public Function SetMyVar (var as String)
MsgBox(var, VbOKCancel, "Test1")
If var Is Nothing Then
Return "NOTHING"
Else
MyVar = var
Return var
End If
End Function
I also have the public variable MyVar:
Public Shared Dim MyVar as String
When my database query returns data, this correctly evaluates, a messagebox is displayed with the value, the textbox gets set with the value, and the world is generally a happier place.
When my database query does not return a value though, I get the error:
The query returned no rows for the data set. The expression therefore
evaluates to null.
and the SetMyVar function never appears to be ran (you never get the messagebox popup). As expected, my emotions range from anger, sadness, and bitter hatred of SSRS.
I read something about SSRS evaluating both sides of an IF statement, so perhaps that is why I get the error (likely then on Code.SetMyVar(Fields!fOrgID.Value))... not sure how I get around that though.
Thoughts? Suggestions? Words of comfort?
From the sound of things, it seems likely that the issue is that SSRS is having a problem displaying zero records. I'd recommend one of the following:
1) Use a control that handles zero records appropriately (Tables do. I think Lists do as well).
2) Modify your query to return a single record with blank values if it would otherwise return zero records.
An answer to the original question:
=IIF(IsNothing(Fields!fOrgID),
Code.SetMyVar("null"),
Code.SetMyVar(IIF(IsNothing(Fields!fOrgID),"Foo",Fields!fOrgID.Value)))
The error was from both sides of IIF being evaluated. The extra IIF in the statement above will avoid Code.SetMyVar from ever being called with a null value.
I believe you're right about about Iif always evaluating both of its value arguments (at least, it does in Visual Basic). I'm not sure why you're getting this precise error (unless strings can't be assigned a value of DBNull?), but you almost certainly want to attack this problem with a different method.
The reason for this is that your current code will likely always call both set methods regardless of the conditional value.
Formula that worked for my SSRS 2008 reports.
=IIf(String.IsNullOrEmpty(Fields!NullableFieldwithPossibleBlankStrings.Value),"Yes","No")
I tried this too (also tried a version with IsNothing)...
=Code.SetField(IsDBNull(Fields!fOrgID))
And changed the function to be one that accepts a boolean. I figure this above function would always return a true or false, but in the event of a NULL, I again get "The query returned no rows for the data set. The expression therefore evaluates to null.".
I need to pass back to my code if the field is null or not (as this will let me know if the datasource is null or not).
Let me know if you can think of a better way because I cannot.