Handling ReportViewer Parameters

| No Comments | No TrackBacks

A developer asked a question on MSDN that was similar to a question a few days earlier so I decided to help folks get over the problems of setting report parameters in ReportViewer projects.

Consider that in a Reporting Services report processor-generated report, all of the parameters (both query and report) are handled behind the scenes. In ReportViewer applications these tasks are all in your court.

To illustrate the process of setting report parameters, I wrote an example application that deals with a number of issues. I tried to keep it simple so I could explain the basics:

  1. Setup your data source(s). This might entail almost anything, but you end up with a binable iBindingList object like a DataTable or a TableAdapter (which contains a DataTable). This usually means you've built a parameterized Fill method like the one shown in my example. In my example, the FillByProductLine call passes in a query parameter to the TableAdapter Fill method to populate the DataTable.
  2. Setup your report parameters using the Report Data Window in Visual Studio. While RDLC imported from BIDS or Report Builder might have query parameters, we're going to assume that this report does not. If it does, you'll have to include these as you build your LocalReport.Parameters collection. So, in the example I've provided, I have two report parameters defined: ColorWanted and MaxWeight. This means you'll need to know the number of your report parameters and their names (case-sensitive) to proceed.
  3. Build an array of type ReportParameter that's large enough for ALL parameters--not just the ones you want to set. It's essential that the size of this array matchs the number of report AND data parameters defined IN THE RDLC. (Again, typically if you're not importing from RDL this will be easy to determine—just check the Report Data Parameters list).
  4. For each ReportParameter object in the array, use the New constructor to name the parameter and provide the value.
  5. Once all ReportParameter objects are initialized, use the SetParameters method to push the array into the LocalReport Parameters property.

So, what can go wrong? Lots.

  • The parameter name you specify does not match the RDLC parameter name
  • You don't specify all of the parameters
  • The value you provide does not match the type of value expected.

So... you need to add exception handlers to your code to deal with the stuff that happens. Note that the exception handlers might not return anything meaningful in the top exception--you might have to drill down into the innnerexception (perhaps several times) to see the "real" exception.

Here’s the code that deals with all of these issues including some crude exception handlers:

  Private Sub btnShowReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowReport.Click
Me.ProductsTableAdapter.FillByProductLine(Me.AdventureWorks2008DataSet.Products, tbProductLineWanted.Text)
' Populate ReportViewer1 Report Parameters
lblStatus.Text = String.Format("Searching for color {0} with weight no more than {1}",
lbColorsWanted.SelectedValue.ToString, tbMaxWeight.Text)
Dim paParameters(1) As ReportParameter ' (To reference two parameters)
paParameters(0) = New ReportParameter("ColorWanted", lbColorsWanted.SelectedValue.ToString)
paParameters(1) = New ReportParameter("MaxWeight", tbMaxWeight.Text)
Catch ex As Exception
' MsgBox(ex.Message)
End Try

End Sub

Private Sub tbProductLineWanted_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbProductLineWanted.TextChanged
Me.AvailableColorsTableAdapter.FillByProductLineWanted(Me.AdventureWorks2008DataSet.AvailableColors, tbProductLineWanted.Text)
End Sub

Private Sub ReportViewer1_ReportError(ByVal sender As Object, ByVal e As Microsoft.Reporting.WinForms.ReportErrorEventArgs) Handles ReportViewer1.ReportError
MsgBox(e.Exception.Message & vbCrLf & e.Exception.InnerException.Message & vbCrLf & e.Exception.InnerException.InnerException.Message)

e.Handled = True
End Sub


No TrackBacks

TrackBack URL: http://betav.com/blogadmin/mt-tb.cgi/2452

Leave a comment


Powered by Movable Type 4.37

About this Entry

This page contains a single entry by William Vaughn published on July 15, 2011 5:32 PM.

Denali’s Unfortunate Dependence on SharePoint was the previous entry in this blog.

Making MovableType Changes in IE9 is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.