Report parameter ignored in codebehind

2 Answers 90 Views
Report Parameters
Mark
Top achievements
Rank 2
Iron
Iron
Iron
Mark asked on 26 Jul 2023, 04:11 PM
On a button click I want to export the report as a PDF. I need to pass the shipmentId but its not getting passed in. Is my code wrong? My storedProcd is set up as a SQLDatasource in the report. the parameter is set as a string. the code ignored what I am passing in. 
 // Create report source
                    var reportSource = new Telerik.Reporting.UriReportSource
                    {
                        Uri = Server.MapPath("Manifest.trdp") // Replace "PathToYourReport" with the actual path to your report
                    };
                    // add parameters to report source
                    reportSource.Parameters.Add(new Telerik.Reporting.Parameter("@shipmentId", shipmentsId.Value.ToString()));                                       

                    // Create report processor
                    var reportProcessor = new Telerik.Reporting.Processing.ReportProcessor();
                    
                     // Render the report to PDF
                     var result = reportProcessor.RenderReport("PDF", reportSource, null);

                    if (result.HasErrors)
                    {                        
                        throw new Exception("There were errors during report processing: " + result.Errors[0].Message.ToString());
                    }

                    // Set the content type to PDF
                    Response.ContentType = "application/pdf";

         
                    // Write the PDF data to the output stream
                    Response.OutputStream.Write(result.DocumentBytes, 0, result.DocumentBytes.Length);

                    // End the response to force it to client
                    Response.End();

2 Answers, 1 is accepted

Sort by
0
Accepted
Mark
Top achievements
Rank 2
Iron
Iron
Iron
answered on 31 Jul 2023, 11:53 AM
I was able to get it to work by 'opening the report' then attaching the datasource  
       private void CreatePDF(ShipmentEntity shipmentEntity)
        {
            // Create a SqlDataSource
            Telerik.Reporting.SqlDataSource sqlDataSource = new Telerik.Reporting.SqlDataSource();

            // Set the connection string
            sqlDataSource.ConnectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;

            // Set the SQL command
            sqlDataSource.SelectCommand = "xxxxxx";

            // Set the command type to StoredProcedure
            sqlDataSource.SelectCommandType = Telerik.Reporting.SqlDataSourceCommandType.StoredProcedure;

            // Add parameter to SqlDataSource
            sqlDataSource.Parameters.Add("@xxxxx", DbType.Int32, shipmentEntity.ShipmentId);

            // Load the report definition
            Telerik.Reporting.Report report = new Telerik.Reporting.Report();

            var reportPackager = new ReportPackager();
            using (var sourceStream = System.IO.File.OpenRead(Server.MapPath("xxxxx.trdp")))
            {
                report = (Report)reportPackager.UnpackageDocument(sourceStream);
            }

            // Assign the SqlDataSource to the DataSource property of the report
            report.DataSource = sqlDataSource;

            // Create InstanceReportSource
            var reportSource = new Telerik.Reporting.InstanceReportSource();

            // Assigning Report object to InstanceReportSource
            reportSource.ReportDocument = report;

            // Create report processor
            var reportProcessor = new Telerik.Reporting.Processing.ReportProcessor();

            // Render the report to PDF
            var result = reportProcessor.RenderReport("PDF", reportSource, null);

            if (result.HasErrors)
            {
                throw new Exception("There were errors during report processing: " + result.Errors[0].Message.ToString());
            }

            // Get the bytes of the PDF file
            var documentBytes = result.DocumentBytes;

            // Set the filename
            string filename = "xxx_Name" + ".pdf";

            // Set the directory path
            string directoryPath = Server.MapPath("~/XXXXX_Upload");

            // Create the directory if it doesn't exist
            if (!System.IO.Directory.Exists(directoryPath))
            {
                System.IO.Directory.CreateDirectory(directoryPath);
            }

            // Combine the directory path and the filename
            string filePath = System.IO.Path.Combine(directoryPath, filename);

            // Check if the file already exists
            if (System.IO.File.Exists(filePath))
            {
                // If file exists, delete it
                System.IO.File.Delete(filePath);
            }

            // Write the PDF data to the file
            System.IO.File.WriteAllBytes(filePath, documentBytes);
        }

0
Todor
Telerik team
answered on 31 Jul 2023, 09:59 AM

Hi Mark,

Based on the code, I suspect that you have a SqlDataSource parameter "@shipmentId", and you need to pass a value to this parameter.

You need to link the SqlDataSource parameter to a Report Parameter which may receive value from the ReportSource passed to the Reporting engine. I suggest the article Using Parameters with the SqlDataSource Component for further details.

Note that in the general case, the SqlDataSource parameter may be set with a complex Expression that includes different Global Objects.

Regards,
Todor
Progress Telerik

Stay tuned by visiting our roadmap and feedback portal pages, enjoy a smooth take-off with our Getting Started resources, or visit the free self-paced technical training at https://learn.telerik.com/.
Tags
Report Parameters
Asked by
Mark
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Mark
Top achievements
Rank 2
Iron
Iron
Iron
Todor
Telerik team
Share this question
or