John Farrell
John Farrell

Reputation: 24754

Sorting by Date which may be empty with Telerik Reports

Have a column in a report which a string date field in the format of MM/dd/yyyy. Some of those columns may contain empty strings. Having a problem sorting a section. I am about 10 hours in to telerik reporting so I may be missing something obvious.

I initially tried to use a expression like this:

=IIf(Fields.ExpirationDate='', CDate('1/1/1990'), CDate(Fields.ExpirationDate))

Does that look correct? This threw an error: An error has occured while processing Report '': Failed to compare two elements in the array.

------------- InnerException ------------- An error has occured while executing function CDate(). Check InnerException for further information. ------------- InnerException ------------- Exception has been thrown by the target of an invocation. ------------- InnerException ------------- String was not recognized as a valid DateTime.

Another developer suggested using a custom method so I created inside the report.cs file

    public DateTime EmptyDateToNow( string expirationDate )
    {
        DateTime parsed;
        if (!DateTime.TryParse(expirationDate, out parsed))
            return DateTime.Now;

        return parsed;
    }

and then tried to call with =EmptyDateToNow(Fields.ExpirationDate)

and this throws the error:

An error has occured while processing Report '': Failed to compare two elements in the array. ------------- InnerException ------------- The expression contains undefined function call EmptyDateToNow().

Upvotes: 1

Views: 1892

Answers (2)

horacioj
horacioj

Reputation: 737

Make the user function static (and if the call still fails, make the call fully qualified. e.g. =Myclass.EmptyDateToNow(Fields.ExpirationDate)).
Make the function receive an object as a parameter (not a string or any other data type).
Inside the function, check for null and then (it not null) convert to the proper data type and work with it.

Upvotes: 0

vbigham
vbigham

Reputation: 191

public static DateTime EmptyDateToNow( string expirationDate )

I think that Telerik Reporting wants your user functions to be static methods.

I am thinking that the problem with the first approach is that CDate(Fields.ExpirationDate) is being executed even if the expression is true thereby causing the exception to be thrown. You might try something like this to get the desired result without defining a user function:

=CDate(IIf(IsNull(Fields.ExpirationDate) Or Fields.ExpirationDate="", "1/1/1990", Fields.ExpirationDate))

I realize this is a bit old, I just wanted to post in case anyone else is having troubles and searching this.

Upvotes: 3

Related Questions