简体   繁体   中英

Apply a Report filter in code

My particular, specific issue is probably too localized, but the general question I'm about to ask is something I'm sure others will ask and have wondered:

General question: In Sql Server Reporting Services, is it possible to apply a filter to a report in code? If not, is it possible to use branching in the report filter based on the value of a variable, and can you point me to documentation or explain how to do it.


My specific example follows, to expand on what I mean by the above, in case I worded it badly:

I'm learning SSRS and the docs and Google are coming up short.

The desired effect is that we have a report based on an incident tracking system. In this system, we have various teams that can track incidents: IT Ops, Development, Security, etc. Each of these teams have team members assigned.

  • We have a base report that displays ALL incidents.
  • We have added a boolean parameter named "LimitByTeam", which produces a CheckBox on the report as you'd expect.
  • We have added a String parameter that accepts multiple values. The allowed values come from a data set that lists the teams. This has added the expected drop-down list to the report, allowing users to select one or more teams.
  • We added a dataset that contains team and team member.

If the CheckBox is NOT selected, we want to display all incidents. (The default)

If it IS selected, we want to have the report to filter based on the login ID of the person who created the incident ticket.

Were I to do this in SQL, I'd do it as

Select 
   (ticket fields) 
From 
   Table 
WHERE TicketCreator IN (
             Select LoginId FROM TeamMembersTable 
             WHERE TeamName in ('IT Ops', 'Developers'))

In SQL, or in VB, etc, this would be simple.

In SSRS I'm not figuring out quite how to do this. I've gotten as far as figuring out that I can use Custom Code to do more complex logic using VB (and it appears to be VB.NET. HOORAY! Familiar territory)

So I've added custom code and verified that I can read the value of the report parameter, but I can't figure out for the life of me how to apply a filter if the parameter value is True. Here's what I've got.

Public Sub ApplyTeamFilter() 

    ' Report.Parameters("LimitByTeam") is a 
    ' boolean report parameter that I'm able to access
    ' so I've got the IF statement worked out


    If Report.Parameters("LimitByTeam").Value = True Then
                 ' Pseudo-code - I'm looking for something like Report.Filters.Add(filterstatement)
                 ' Alternatively a way to change this to a function to return a list of items from 
                 ' the Team MembersTable table and use it in a custom expression.
     End If
End Sub

The problem is that I can't seem to find a Filters property on the Report object, or any method that lets me find it. Since I couldn't find it there, I expanded my search to everything in this section of the MSDN library and can't find it, or anything even remotely resembling a way to do what I'm attempting.

I'm also trying to do something like this because I think I see a way to use this function:

Public Function IsLoginIdInTeam(ByVal LoginId as String, byVal Team As String) As Boolean

    '  Report.Parameters("LimitByTeam") is a 
    ' boolean report parameter that I'm able to access
    ' so I've got the IF statement worked out


    If Report.Parameters("LimitByTeam").Value = False Then
        Return True ' Default
    Else
        ' Access the TeamMembers table and look for a match
                ' Something like 
                ' Convert.ToBoolean(Report.DataSets!TeamMembers.Compute(Count, "TeamName = '" & Team & "' AND LoginId = '" & LoginId & "'")

     End If
End Function

But I can't figure out how to access the Data Sets in the report, either, much less the syntax for interacting with them. The pseudocode works with a System.Data.DataTable, but I suspect SSRS DataSets are a different beast.

Am I missing something blindingly obvious? Should I be giving up on filtering this way in the report, and try another track, like changing the original query in the DataSet?

I'm not a huge fan of boolean parameters in SSRS (or BIT in SQL for that matter.) You can't write things like

WHERE @MyBool OR @MyOtherBool

It needs to be

WHERE @MyBool = 1 or @MyOtherBool = 1

So if I have an SSRS report with a boolean called MyBoolParam and a multivalue text parameter called MyMultiSelectTextParam , a SQL query like this will work:

SELECT 
   MyField,
   MyOtherField
FROM
   MyTable t
WHERE
   @MyBoolParam = 1
   OR
   t.MyField IN ( @MyMultiSelectTextParam)

(My preferred alternative to boolean parameters is actually a string with possible values, but it would be handled in the query in the same way.)

If you can't easily change your SQL query (such as using a third party SP) then you can also accomplish the same thing by adding a filter to the SSRS DataSet. Right click on the DataSet in the Report Data pane and look at the Dataset Properties. The Filters pane will let you add criteria that are evaluated for each row. I haven't used these as much with multivalue parameters, but the IN Operator should work for that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM