views:

705

answers:

3

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?

A: 

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.

AaronSieb
A: 

I tried this too (also tried a version with IsNothing)...

=Code.SetField(IsDBNull(Fields!fOrgID))

And changed the fuction 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.

Kris
If you set the expression to =IsDBNull(Fields!fOrgID) as a diagnostic measure, what happens? It may be an issue with custom code and empty data sets.
AaronSieb
I get the same error. Not sure what that tells me.
Kris
It tells you that the issue is either with the SSRS architecture (which seems to be the case based on others having this issue), or with your overall report design. The problem is not in your user-defined code segment.
AaronSieb
In other words, it may be useful to be a bit more descriptive about WHERE this formula is. Is it in a table cell? A sub report?
AaronSieb
It's in a textbox, not inside anything else. The report is just a bunch of textboxes with field values mixed with other descriptive non-dynamic textboxes and the occasional rectangle or line.
Kris
Weird, I didn't realize that a report body could be linked directly to a dataset without using aggregate expressions. When your query returns data, is it the sort of query that returns no more than one record?
AaronSieb
Yes, that's exactly it. The user searches for a company in the web app and then clicks a link to view a report specific for that company.As a side not, you can take these single page reports and create a new report that includes them as a sub report so you can have multiples in one report.
Kris
+1  A: 

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.

AaronSieb
1 isn't really an option, as it would require a radical redesign of the report (it's almost like a letter).2 would be doable if the DBA's allow it. If I cannot get it to work as desired, this might be what I have to do. Thanks.
Kris