How To: Pass Parameters from ASPX Page to a Section Based Report
ActiveReports 7 being a powerful and flexible report tool supports parameters and stored procedures. Parameters allow users to display a report in accordance to the values they pass to the report. In ActiveReports 7, the HtmlViewer type of the WebViewer control allow users to pass parameters to the report; however one common question which users have is how to pass parameters over the web from a different aspx page. The answer to this question is pretty simple. Unlike windows applications the parameter dialog box cannot be displayed over a web page therefore the basic thing to understand here is that the parameters collection should be used to perform this task. In addition to this, a very important thing to keep in mind is to set the ShowParameterUI property to false since there is no parameter dialog box involved. Before I actually start explaining the approach with an example it would be nice to check the parameters concepts from our documentation. Having said that let me put a very simple explanation of how to pass parameters over web. Consider we are trying to display reports for different countries. Without parameters the report will display information for all the countries in one go; however we can pass the value of the country to the report to let it know, for which country we need the information and the report will be displayed accordingly. So first of all we can define the datasource for the report in the ReportStart event. Since parameters can be passed from the SQL, we will use it to pass the country field value to the report. Here is a sample code:
public partial class SectionReport1 : GrapeCity.ActiveReports.SectionReport
{
string _param;
public SectionReport1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
}
public SectionReport1(string str)
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
_param = str;
}
private void SectionReport1_ReportStart(object sender, EventArgs e)
{
string constr = null;
constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\\Data\\\NWIND.MDB;Persist Security Info=False";
System.Data.OleDb.OleDbConnection con = new System.Data.OleDb.OleDbConnection(constr);
string queryString = null;
queryString = "SELECT * FROM CUSTOMERS WHERE COUNTRY='" + _param + "'";
System.Data.OleDb.OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter(queryString, con);
DataSet ds = new DataSet();
da.Fill(ds);
this.DataSource = ds.Tables[0];
this.DataMember = ds.Tables[0].TableName;
this.textBox1.DataField = "CustomerID";
this.textBox2.DataField = "City";
this.textBox3.DataField = "Country";
}
}
Notice how we have appended the parameter value to the SQL statement passed to the report. Now the next question is how will we get this value? Well we can create a new session variable to store the parameter value and then pass this variable to the report. In this example I have used a drop down list to show the name of the countries, out of which a user can select any country and see the report for that country in a new window. Since I said new window, the project will have two pages. One page to allow the user to choose the county and the second page to display the report. Let us first write the code to invoke the new window once the user makes his choice:
protected void Page_Load(object sender, EventArgs e)
{
DropDownList1.AutoPostBack = true;
}
protected void DropDownList1_TextChanged(object sender, EventArgs e)
{
Session["Country"] = DropDownList1.Text;
string str = "<script language='javascript'>window.open('ViewerPage.aspx')</script>";
Response.Write(str);
}
Now the next and the final step is to create a session to store the value selected by the user. We can use the Page_Load event of the viewer page to do this:
SectionReport1 rpt;
protected void Page_Load(object sender, EventArgs e)
{
if (Session["Country"] == null)
{
return;
}
else
{
rpt = new SectionReport1(Session["Country"].ToString());
WebViewer1.Report = rpt;
}
}
So once a user selects a country from the drop down list, it is stored in a session variable which is passed to the report and finally the report is displayed in a separate window. A working sample in both C# and VB.NET can be downloaded from the following links. PassingParam_CS PassingParam_VB.NET