ActiveReports popular for its flexibility allows developers to do various kind of customizations to completely control the report processing engine to fit their needs. This blog looks to showcase a requirement where developer would like to pass a parameter directly to a SectionReport without requiring end users to input the values. Ideally, with parameteric report, a side bar is displayed where users can input the parameter values. However, this blog shows, how to pass parameters to a SectionReport in a Silverlight application without asking for user inputs. For this blog implementation, we have a combobox with the name of few cities. Selecting a city will fetch the report containing the corresponding records for that city. Before we look ahead with the implementation, lets see how our final output would appear. SLParameteric

Design the Silverlight Page

To begin with, create a Silverlight application with Web Project included. Add a ComboBox control and a SilverlightViewer object to the MainPage of the Silverlight project.


<Grid x:Name="LayoutRoot" Background="White">  
  <Grid.RowDefinitions>  
     <RowDefinition Height="35"/>  
     <RowDefinition/>  
  </Grid.RowDefinitions>  
  <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Height="30" Margin="10,0,0,0">  
      <TextBlock Text="Select a city from dropdown list" Padding="10"/>  
      <ComboBox Name="cityCombo" Width="125" SelectionChanged="cityCombo_SelectionChanged">  
        <ComboBoxItem Content="London"/>  
        <ComboBoxItem Content="Seattle"/>  
        <ComboBoxItem Content="Tacoma"/>  
        <ComboBoxItem Content="Kirkland"/>  
        <ComboBoxItem Content="Redmond"/>  
      </ComboBox>  
  </StackPanel>  
  <ActiveReports:Viewer Name="SLViewer" Grid.Row="1"/>  
</Grid>  

Add SectionReport to the Web Project

In this phase, you add a SectionReport and design all the required fields. However, we will not add any datasource details in this Report. DataSource will be assigned at runtime. You simply have to design the layout by setting the textboxes and mapping their DataField property to the column names in the datasource.

Working with Parametric WebForm

In this blog implementation, the core concept is to pass the parameter as query string to a web form which will run the reports object using the parametric query string in its datasource command. Report is saved in RDL format and returned back as stream to be consumed by the Silverlight Page. Add a web form 'ReportParameters.aspx' and use the code mentioned below. Following code block is the heart of this blog implementation.


protected void Page_Load(object sender, EventArgs e)  
{  
    if (Request.QueryString["ReportType"] == "Section")  
    {  

        #region Loading Parametric Section Report  
        ParametricSectionReport rpt = new ParametricSectionReport();                  

        if (Request.QueryString["ParamData"] != "")  
        {  
            rpt.ShowParameterUI = false;  

            string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("~/App_Data/NWIND.MDB") + ";Persist Security Info=False";  
            string paramString = Request.QueryString["ParamData"];  
            string sqlString = "SELECT * FROM Employees WHERE City like '" + paramString+ "'";  

            rpt.DataSource = new GrapeCity.ActiveReports.Data.OleDBDataSource(connectionString, sqlString, 100);  
            rpt.Run();  

            System.IO.MemoryStream ms = new System.IO.MemoryStream();  
            rpt.Document.Save(ms);  
            rpt.Document.Save(Server.MapPath("~") + "\\\ParameterReport.rdf");  
            Response.BinaryWrite(ms.ToArray());  
            Response.End();  
        }  
        #endregion  
    }  
}  

Generate Report Based on ComboBox Selection

This section shows how the query string is passed to the webform and retrieve the RDL stream for display in the Silverlight Viewer


/// <summary>  
/// Selection Changed Event triggers the redirection to the   
/// query WebForm page with the parameters using query string  
/// </summary>          
private void cityCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)  
{  
      var currenthost = string.Format("http://{0}:{1}/",  
                      Application.Current.Host.Source.DnsSafeHost,  
                      Application.Current.Host.Source.Port);  

      Uri codeuri = new Uri(currenthost + "ReportParameter.aspx?ReportType=Section&ParamData=" + ((ContentControl)(cityCombo.SelectedValue)).Content.ToString());  
      WebClient codereport = new WebClient();  
      codereport.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);  
      codereport.OpenReadAsync(codeuri);  
}  

private void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)  
{  
      if (e.Result != null)  
     {  
         Stream rdf = (Stream)e.Result;  
         LoadReportFromStream(rdf);  
     }  
}  

private void LoadReportFromStream(Stream stream)  
{  
     //Loads the RDF stream and displays in the Silverlight Viewer  
     GrapeCity.Viewer.Common.StreamDocumentLoader rdfdocument = new GrapeCity.Viewer.Common.StreamDocumentLoader(stream, GrapeCity.Viewer.Common.DocumentFormat.Rdf);  
     SLViewer.LoadDocument(rdfdocument);  
}  

Similarly for multiple parameters, you can use multiple query string objects while redirecting to the query web form page. Refer to the attached sample for complete implementation. Download Sample