This is a migrated thread and some comments may be shown as answers.

Code Behind - Loading, Passing Params

1 Answer 522 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Kenneth
Top achievements
Rank 1
Kenneth asked on 25 Oct 2018, 03:50 PM

I've looked through other posts, and I think I'm doing this correctly...but I must be missing something. I have a report that I created within VS2015 using 2018 R2. I don't want to show the report in a viewer. When I click a button, I have it rendering straight to a PDF. I only want the report to show info for the record I'm on in the application, so I created a parameter. When I pass the parameter, it doesn't use what I'm passing. Maybe I'm not understanding parameters. I want the report to pull info using the following SQL: select id, employee_id, employ_badge from employee where id = @id. (this is an example of what I want). I've included the code below. What am I missing? Am I loading the report wrong? Have I misunderstood parameters? Also, how do I set the datasource in the code behind? I've looked at the example that attached with a similar post and it looks like what I have.

protected void btnPrint_Click(object sender, EventArgs e)
{
Telerik.Reporting.Processing.ReportProcessor reportProcessor =
new Telerik.Reporting.Processing.ReportProcessor();
// set any deviceInfo settings if necessary
System.Collections.Hashtable deviceInfo = new System.Collections.Hashtable();
Telerik.Reporting.TypeReportSource typeReportSource = new Telerik.Reporting.TypeReportSource();
// reportName is the Assembly Qualified Name of the report
typeReportSource.TypeName = typeof(Report1).AssemblyQualifiedName;
int RowID = Convert.ToInt32(ViewState["FORMROWID"]);
typeReportSource.Parameters.Add(new Telerik.Reporting.Parameter("id", RowID));

Telerik.Reporting.Processing.RenderingResult result = reportProcessor.RenderReport("PDF", typeReportSource, deviceInfo);
string fileName = result.DocumentName + "." + result.Extension;
string path = System.IO.Path.GetTempPath();
string filePath = System.IO.Path.Combine(path, fileName);
using (System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Create))
{
fs.Write(result.DocumentBytes, 0, result.DocumentBytes.Length);
}
WebClient User = new WebClient();
Byte[] FileBuffer = User.DownloadData(filePath);
if (FileBuffer != null)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", FileBuffer.Length.ToString());
Response.BinaryWrite(FileBuffer);
}
}

1 Answer, 1 is accepted

Sort by
0
Todor
Telerik team
answered on 30 Oct 2018, 10:02 AM
Hi Kenneth,

I suspect that the SqlDataSource parameter (@id) is not linked to the corresponding Report Parameter (id). We strongly recommend to do this in the report definition - check the attached screenshot for clearance. You may go through Using Parameters with the SqlDataSource component help article for more details and examples.

We do *not* recommend to set the DataSource of a Report or data item in the code behind, as this is part of the report definition. We provide other tools for dynamically modifying the layout/data source properties of a report - for example with Bindings. The SqlDataSource connection string and query can also be modified run time based on report parameters - check Change Connection String dynamically through a report parameter KB article.
If setting DataSource through code is necessary, you should instantiate/deserialize the report, change/set the data source and wrap the modified report in an InstanceReportSource to pass it to the report processor.

Regards,
Todor
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
General Discussions
Asked by
Kenneth
Top achievements
Rank 1
Answers by
Todor
Telerik team
Share this question
or