How to pass parameters to the report
Suppose we want the title of the report to be dynamic. We have the desired title in a session variable. How do we pass this value to a text field in the report's title band using VB? I don't seem to find a discussion of this in the manual.
Thanks
Thanks
Comments
This is in the programmer's manual:
<script language="vb" runat="server">
Protected Sub WebReport1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles WebReport1.Load
WebReport1.Load("testreportinside.frx")
WebReport1.SetParameterValue("ListName", "My Committee")
End Sub
</script>
produces syntax errors.
"To use this method in ASP.NET, you need to create the event handler for the StartReport event of the WebReport component. The report can be accessed through the WebReport.Report property:
webReport1.Report.SetParameterValue("MyParam", 10);
"
Are you putting this in your Code Behind file (page.aspx.vb)?
I code in c# but maybe this would help you get started. Should be fairly easy to convert to VB.
In C#, to set the parameter to a session variable, you'd do something like:
Rpt.SetParameterValue("ParamName", Session["SessionVariable"].ToString());
In your report band, set the contents of the textobject to be "[ParameterName]".
I have a report that I pass several parameters to and export to pdf and then return to the browser. I'm not using the WebReport viewer, just FastReports directly. If you are trying to do the same, here is my Page_Load function (from default.aspx.cs) which works well.
protected void Page_Load(object sender, EventArgs e)
{
int Id=0,Batch=0;
string IdText="",Hash="";
IdText = Request.QueryString["InvoiceNumber"];
Hash = Request.QueryString["Security"];
if (string.IsNullOrEmpty(IdText)==false)
if (int.TryParse(IdText,out Id)==false) Id=0;
if (string.IsNullOrEmpty(Hash)) Hash="";
if (Hash.IndexOf(' ')>0) Hash = "";
// **** THIS IS IMPORTANT TO MAKE WORK on A WEB SERVER. FR WILL THROW A .NET ERROR LOOKING FOR A CONFIG FILE IF YOU DON'T SET THIS 'TRUE'
FastReport.Utils.Config.WebMode=true;
if (Id != 0 && Hash != "")
{ SqlConnection oSQLConn = new SqlConnection();
oSQLConn.ConnectionString = "XXXXXXX";
string queryString = "select invoicenumber,invoicehash from alnonlineinvoices where InvoiceNumber="+Id.ToString();
queryString+=" and invoicehash='"+Hash+"'";
SqlCommand command = new SqlCommand(queryString, oSQLConn);
oSQLConn.Open();
SqlDataReader reader = command.ExecuteReader();
int count = 0;
if (!reader.Read()) Id=0;
FastReport.Report Rpt = new FastReport.Report();
Rpt.Load(Server.MapPath("./App_Data/ALNInvoices.Frx"));
Rpt.SetParameterValue("InvoiceBatch", 0);
Rpt.SetParameterValue("InvoiceNumber", Id);
Rpt.SetParameterValue("SendAll", "Y");
Rpt.SetParameterValue("AccountToPrint", "");
Rpt.SetParameterValue("PrintOrEMail", "A");
Rpt.Prepare();
FastReport.Export.Pdf.PDFExport export = new FastReport.Export.Pdf.PDFExport();
export.PrintOptimized = false;
export.OpenAfterExport = false;
System.IO.MemoryStream s = new System.IO.MemoryStream();
Rpt.Export(export, s);
s.Position = 0;
Response.ClearHeaders();
Response.Clear();
Response.Cache.SetMaxAge(new TimeSpan(0));
Response.Cache.SetExpires(new DateTime(0));
Response.Cache.SetNoServerCaching();
Response.ContentType = System.Net.Mime.MediaTypeNames.Application.Pdf;
Response.AppendHeader("Content-Disposition", "inline;filename=\"Invoice" + Id.ToString() + "\"");
Byte[] buffer = new Byte[s.Length];
s.Read(buffer, 0, (int)s.Length);
Response.BinaryWrite(buffer);
Response.Flush();
Response.End();
}
}
Thanks pin. I guess that helps somewhat. As near as I can tell the SetParameterValue needs to be executed sometime after the report is loaded. I have placed it in the page.prerender.complete event like this.
Protected Sub Page_PreRenderComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRenderComplete
WebReport1.Report.SetParameterValue("ListName", "My Committee")
End Sub
In my case I have the report object on the page so I don't have to do a lot of the manual loading etc that you have.
In the report designer I created a parameter named ListName and added a text label to the page header. The text value of the label is
[ListName]. This all seems to run OK but the result is that the text label is blank.
Protected Sub WebReport1_StartReport(ByVal sender As Object, ByVal e As System.EventArgs) Handles WebReport1.StartReport
WebReport1.Report.SetParameterValue("ListName", Session("CommitteeName"))
End Sub
Thanks to all.