Crystal Reports Custom IsNull Function - function

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}

Related

access macro IsNull multiple variables

If IsNull(delivery) = True Then
This code is running for one variable i.e delivery.
I have check isNull for three variable and I want code for that.
This code is for Access Macro.
Use an And or Or operator depending on the logic that you require, for example using And logic:
If IsNull(delivery) And IsNull(variable2) And IsNull(variable3) Then
' Do something if all three variables are null
End If
Alternatively, using Or logic:
If IsNull(delivery) Or IsNull(variable2) Or IsNull(variable3) Then
' Do something if any of the three variables are null
End If

SSRS: How to filter query with custom function

Using VS 2015 and SSRS version 14.0.608.142
I have a function in the code section of my report properties similar to below and I'm trying to use it to filter out values in my dataset but it's not returning any values or giving any errors. The value inside the function would be changed at run time by another application so the value inside it right now is just a placeholder.
Function FilterBy() As String
FilterBy = "'REMIT'"
End Function
I have the function being used as a default value in a parameter and the parameter is in the where clause of my dataset to filter out the necessary values.
So, inside parameter default value:
=code.FilterBy
Dataset query:
SELECT Blah
FROM dbo.Table
WHERE ID = #parameter;
So, I'm noticing in your function that you're setting a variable but not returning it. If you're trying to set a Variable in the report properties, they can't be modified. Those things are static. You can, however, declare a variable in your code and use set and gets to retrieve them.
dim FilterBy = "REMIT"
Function getFilter() As String
return FilterBy
End Function
Function setFilter(input as String)
FilterBy = input
End Function
Therefore your parameter default value would be:
=Code.getFilter()
Also notice that I'm not adding single quotes to REMIT. When you pass a parameter in your query you do not need to add single quotes to it, it will be added by SSRS. Unless the value is 'REMIT' in your database, then by all means, carry on.

SSRS expression replace NULL with another field value

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)

RDLC Expression Extension Methods

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.

Code not running when Field is NULL - SSRS 2005

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.