CHAPTER 9 RENDERING REPORTS FROM .NET APPLICATIONS
want to get the values out of to return. If the control is a combo box, the code then casts that control as a
combo box so that we can grab the name and value of the selected item to add to our array of report
parameters. If the value isn’t null or a blank string, we grab the selected value and populate the array. If it
is null or blank, we use a null value in our parameter array.
Listing 9-11. Get Parameters Entered by User
private Microsoft.Reporting.WinForms.ReportParameter[] ViewerParameters() {
int numCtrls = (this.parameterPanel.Controls.Count / 2);
Microsoft.Reporting.WinForms.ReportParameter[] rp =
New Microsoft.Reporting.WinForms.ReportParameter[numCtrls];
int i = 0;
foreach (Control ctrl in this.parameterPanel.Controls) {
if (ctrl.GetType() == typeof(ComboBox)) {
ComboBox a = (ComboBox)ctrl; rp[i] =
new Microsoft.Reporting.WinForms.ReportParameter(); rp[i].Name = a.Name; if (a.SelectedValue
!= null &&
a.SelectedValue.ToString() != String.Empty) {
rp[i].Values.Add(a.SelectedValue.ToString()); }
else {
rp[i].Values.Add(null); }
i++; } } return rp; }
To finish your GetParameters form, you need to add some code to allow you to pass the report
parameters and their values to the viewer form (ViewerRVC.cs), as shown in Listing 9-12. This allows the
parent ViewRVC form to gather the parameters once they have been populated from the web service and
chosen by you through the form. You will also manually add this code to your GetParameters class.
Listing 9-12. Property Used to Get Parameters from the ViewerRVC.cs Form
public Microsoft.Reporting.WinForms.ReportParameter[] Parameters {
get
{
return parameters;
} }
To finally bring it all together, you need to add a button and related code to its click event in the
ViewerRVC.cs form, the main form, to use the new Parameters dialog box, passing in the URL of the
report you want to run. Then, you read the ReportParameter array through the GetParameters form’s
Parameters property and use it to set the report parameters for the Report Viewer control, as shown in
Listing 9-13.
First, open your ViewerRVC class in design mode by simply double clicking the file in the Solution
Explorer. From here, you can just double click the Parameters button that you created when we first
designed this form earlier in the chapter. This will create a new getParameters_Click event method for
you and link it automatically to the click event for that button. Now you can add the code to pull up the
new GetParameters form and return the parameters generated from that form.
Listing 9-13. The getParameters click Event: Retrieving the Parameters and Running the Report
private void getParameters_Click(object sender, EventArgs e)
{
reportURL.Text = "http://localhost/reportserver?