ca88d2a28f18639c0470da4e80f8ab67094843b8
PreExecuteReportSet.md
| ... | ... | @@ -0,0 +1,149 @@ |
| 1 | +**Hidden Filter C#** |
|
| 2 | + |
|
| 3 | +This is a code sample showing how to add a hidden filter to a report. The first filter uses the _strArray_, but you will need to write a function which returns the columns which you wish to filter upon. |
|
| 4 | + |
|
| 5 | +``` |
|
| 6 | +public override void PreExecuteReportSet(Izenda.AdHoc.ReportSet reportSet) |
|
| 7 | + |
|
| 8 | +{ |
|
| 9 | + /* First Example, multi valued filter */ |
|
| 10 | + //columns to filter - write your own function |
|
| 11 | + |
|
| 12 | + string[] strArray = new string[3] {"Beverages", "Seafood", "Produce"}; |
|
| 13 | + |
|
| 14 | + if(reportSet.Source.Contains("[dbo].[Categories]")) |
|
| 15 | + { |
|
| 16 | + Izenda.AdHoc.Filter newFilter = new Izenda.AdHoc.Filter(); |
|
| 17 | + newFilter.Column="CategoryName"; |
|
| 18 | + newFilter.Operator=OperatorTypes.In; |
|
| 19 | + newFilter.Values = strArray; |
|
| 20 | + |
|
| 21 | + reportSet.Filters.AddHidden(newFilter); |
|
| 22 | + // will only show the category names corresponding to the array values |
|
| 23 | + } //end if |
|
| 24 | + |
|
| 25 | + /* Second Example, single valued filter */ |
|
| 26 | + if (reportSet.Source.Contains("[dbo].[OtherCategories]")) |
|
| 27 | + { |
|
| 28 | + Izenda.AdHoc.Filter newFilter = new Izenda.AdHoc.Filter(); |
|
| 29 | + newFilter.Column = "CategoryName"; |
|
| 30 | + newFilter.Operator = OperatorTypes.Like; |
|
| 31 | + newFilter.Value = "Sea"; |
|
| 32 | + reportSet.Filters.AddHidden(newFilter); |
|
| 33 | + // will only show category names having the exact string "Sea" in them |
|
| 34 | + } //end if |
|
| 35 | +}//end method |
|
| 36 | +``` |
|
| 37 | + |
|
| 38 | +#The PreExecuteReportSet() Method |
|
| 39 | + |
|
| 40 | +[[_TOC_]] |
|
| 41 | + |
|
| 42 | +Essentially, to use a multi valued filter, you must use the In operator (basically an equals operator with comma separated values) and the .Values specifier. |
|
| 43 | + |
|
| 44 | +To use a single valued operator such as "Like" (basically a case sensitive contains), you can not use an array and must use the .Value specifier. |
|
| 45 | + |
|
| 46 | +##Hidden Filter with sql override |
|
| 47 | + |
|
| 48 | +``` |
|
| 49 | +Public Overrides Sub PreExecuteReportSet(ByVal reportSet As Izenda.AdHoc.ReportSet) |
|
| 50 | + |
|
| 51 | + MyBase.PreExecuteReportSet(reportSet) |
|
| 52 | + 'If reportSet.Source.Equals("[dbo].[Customers]") Then |
|
| 53 | + Dim filter As New Izenda.AdHoc.Filter() |
|
| 54 | + filter.Column = "email" |
|
| 55 | + filter.SqlOverride = "(aspnet_Membership.email = 'greg@edelinsolutions.com' or |
|
| 56 | + aspnet_Membership.email='gedelin@yahoo.com')" |
|
| 57 | + reportSet.Filters.AddHidden(filter) |
|
| 58 | + 'End If |
|
| 59 | +End Sub |
|
| 60 | +``` |
|
| 61 | + |
|
| 62 | +##Hidden Filter VB.NET |
|
| 63 | + |
|
| 64 | +``` |
|
| 65 | +Public Overrides Sub PreExecuteReportSet(ByVal reportSet As Izenda.AdHoc.ReportSet) |
|
| 66 | + MyBase.PreExecuteReportSet(reportSet) |
|
| 67 | + Dim emails(1) As String |
|
| 68 | + emails(0) = "greg@edelinsolutions.com" |
|
| 69 | + emails(1) = "gedelin@yahoo.com1" |
|
| 70 | + Dim filter As New Izenda.AdHoc.Filter() |
|
| 71 | + filter.Column = "email" |
|
| 72 | + filter.Operator = OperatorTypes.In |
|
| 73 | + filter.Values = emails |
|
| 74 | + reportSet.Filters.AddHidden(filter) |
|
| 75 | +End Sub |
|
| 76 | +``` |
|
| 77 | + |
|
| 78 | +##Using a stored procedure |
|
| 79 | + |
|
| 80 | +This example uses a stored procedure to populate a table before report design or execution. As shown in the comments, the table StoredProcResults must already exist. Every time a report is created or viewed this stored procedure will update the results of the _StoredProcResults_ table. |
|
| 81 | + |
|
| 82 | +```csharp |
|
| 83 | +// Customize a report on the fly prior to execution on a per user basis |
|
| 84 | +public override void PreExecuteReportSet(Izenda.AdHoc.ReportSet reportSet) |
|
| 85 | +{ |
|
| 86 | + |
|
| 87 | + /*this sample uses the adventure works database |
|
| 88 | + |
|
| 89 | + Here is the definition of the table and |
|
| 90 | + stored procedure created for this report. |
|
| 91 | + |
|
| 92 | + CREATE TABLE [dbo].[StoredProcResults] |
|
| 93 | + [ProductID] [int] NOT NULL, |
|
| 94 | + [OrderQuantity] [int] NOT NULL, |
|
| 95 | + [Total] [int] NOT NULL, |
|
| 96 | + [DueDate] [smalldatetime] NOT NULL |
|
| 97 | + ) ON [PRIMARY] |
|
| 98 | + |
|
| 99 | + CREATE PROCEDURE DoCustomAction ( |
|
| 100 | + @date1 as smalldatetime, |
|
| 101 | + @date2 as smalldatetime |
|
| 102 | + ) |
|
| 103 | + AS |
|
| 104 | + BEGIN |
|
| 105 | + |
|
| 106 | + insert into StoredProcResults |
|
| 107 | + select ProductID,OrderQty,LineTotal,ModifiedDate |
|
| 108 | + from Sales.SalesOrderDetail |
|
| 109 | + where ModifiedDate >= @date1 and ModifiedDate <= @date2 |
|
| 110 | + |
|
| 111 | + END |
|
| 112 | + */ |
|
| 113 | + |
|
| 114 | + string currentReportName = |
|
| 115 | + HttpContext.Current.Request.QueryString["rn"]; |
|
| 116 | + |
|
| 117 | + if (currentReportName == "StoredProcExample") |
|
| 118 | + { |
|
| 119 | + SqlConnection myConnection = new |
|
| 120 | + SqlConnection(Izenda.AdHoc.AdHocSettings.SqlServerConnectionString); |
|
| 121 | + SqlCommand myCommand = new SqlCommand("DoCustomAction", myConnection); |
|
| 122 | + |
|
| 123 | + // Mark the Command as a SPROC |
|
| 124 | + myCommand.CommandType = System.Data.CommandType.StoredProcedure; |
|
| 125 | + |
|
| 126 | + // Add Parameters to SPROC |
|
| 127 | + SqlParameter parameterdate1 = new SqlParameter |
|
| 128 | + ("@date1", System.Data.SqlDbType.SmallDateTime); |
|
| 129 | + parameterdate1.Value = "1/1/2003"; |
|
| 130 | + myCommand.Parameters.Add(parameterdate1); |
|
| 131 | + |
|
| 132 | + SqlParameter parameterdate2 = new SqlParameter |
|
| 133 | + ("@date2", System.Data.SqlDbType.SmallDateTime); |
|
| 134 | + parameterdate2.Value = "12/31/2003"; |
|
| 135 | + myCommand.Parameters.Add(parameterdate2); |
|
| 136 | + |
|
| 137 | + try |
|
| 138 | + |
|
| 139 | + myConnection.Open(); |
|
| 140 | + myCommand.ExecuteNonQuery(); |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + finally |
|
| 144 | + { |
|
| 145 | + myConnection.Close(); |
|
| 146 | + } |
|
| 147 | + } |
|
| 148 | +} |
|
| 149 | +``` |
|
| ... | ... | \ No newline at end of file |