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

How to get datasource from report file programmatically

1 Answer 997 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Omer47
Top achievements
Rank 1
Omer47 asked on 11 Sep 2019, 10:30 AM

I created a report with subreport in Standalone Report Designer. I want to export it to PDF programmatically. I created an example Windows Form Application which gets parameters from user with textboxes. And when user writes values to its indicated textbox and then presses export to pdf. Then my application export it to pdf. But I have something wrong there. I must get the datasource or connection from my report file in order to set it in the program. How can I do this ?

I will share my app's picture.

Here is my code : 

 

01.    private void BtnExportPDF_Click(object sender, EventArgs e)
02.    {
03.        var reportProcessor = new Telerik.Reporting.Processing.ReportProcessor();
04. 
05.        // set any deviceInfo settings if necessary
06.        var deviceInfo = new System.Collections.Hashtable();
07. 
08. 
09. 
10.        //                  -2-
11.        // ***Declarative (TRDP/TRDX) report definitions***
12.        var reportSource = new UriReportSource();
13. 
14.        // reportName is the path to the TRDP/TRDX file
15.        reportSource.Uri = @"C:\Users\OE\Desktop\TelerikReport\Deneme.trdp";
16.        //var reportPackager = new ReportPackager();
17.        //using (var sourceStream = System.IO.File.OpenRead(@"C:\Users\OE\Desktop\TelerikReport\Deneme.trdp"))
18.        //{
19.        //    var report = (Report)reportPackager.UnpackageDocument(sourceStream);
20.        //}
21.        //Telerik.Reporting.SqlDataSource sqlDataSource = new Telerik.Reporting.SqlDataSource();
22.        //sqlDataSource.ProviderName = "System.Data.SqlClient";
23.        //sqlDataSource.ConnectionString = "Data Source=(local)\\SQLEXPRESS;Initial Catalog=AdventureWorks;Integrated Security=True";
24.         
25. 
26.        //                  -2-
27. 
28.        //                  -3-
29.        // ***Instance of the report definition***
30.        //var reportSource = new Telerik.Reporting.InstanceReportSource();
31. 
32.        // Report1 is the class of the report. It should inherit Telerik.Reporting.Report class
33.        //reportSource.ReportDocument = new Report1();
34.        //                  -3-
35.        object parameterValue = txtBasDep.Text;
36.        reportSource.Parameters.Add("basdepid", parameterValue);
37.        object parameterValue1 = txtBitDep.Text;
38.        reportSource.Parameters.Add("bitdepid", parameterValue1);
39.        object parameterValue2 = txtSonDogum.Text;
40.        reportSource.Parameters.Add("sondogumtar", parameterValue2);
41. 
42.        Telerik.Reporting.Processing.RenderingResult result = reportProcessor.RenderReport("PDF", reportSource, deviceInfo);
43. 
44.        // Pass parameter value with the Report Source if necessary
45. 
46. 
47.        string fileName = result.DocumentName + "." + result.Extension;
48.        string path = @"C:\Users\OE\Desktop\TelerikReport";
49.        string filePath = System.IO.Path.Combine(path, fileName);
50. 
51.        using (System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Create))
52.        {
53.            fs.Write(result.DocumentBytes, 0, result.DocumentBytes.Length);
54.        }
55. 
56.        MessageBox.Show("The report has been exported!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
57.    }
58.}

1 Answer, 1 is accepted

Sort by
0
Silviya
Telerik team
answered on 16 Sep 2019, 07:51 AM

Hi Ömer,

I'm not sure why are you trying to get the data source at run-time. You can take advantage of our SqlDataSource component instead.

I suggest introducing Report Parameters that will be mapped to data source parameters. To accomplish this you can use parameterized query for the SqlDataSource component. The next step is to map each DataSource parameter to a Report Parameter using expressions. This is the most efficient way to save bandwidth is to filter data records before they are retrieved by the DataSource component. This is called filtering on the “server side”. 

For example, on step 4 of SqlDataSource wizard, you can set the value (=Parameters.basdepid.Value and so on) for each data source parameter. It is convenient to map the value of data source parameter to the value of report parameter. 

Then the provided code will work and will fetch the required data based on the passed parameter values.

Best Regards,
Silviya
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
Omer47
Top achievements
Rank 1
Answers by
Silviya
Telerik team
Share this question
or