Download RDL files in to pdf file asynchronously, I have a requirement where I need to download the RDL file to pdf which is working with out any issues. But I have an issue with performance. I have 3 multi select drop down where the first display all the Managers, and on selecting manager corresponding employee will display and on selecting employee I will bind the 3rd one with the list of reports to be converted to RDL.
15 reports to be downloaded for each employee selected, let us say if I select 10 employees I need to download 150 reports
- private async System.Threading.Tasks.Task CreateFolder()
- {
- try
- {
- string reportName = string.Empty;
- string fileName = string.Empty;
- string strPath = string.Empty;
- string strFolderPath = @"c:\My Reports";
-
- foreach (var manager in manager.SelectedItems)
- {
-
-
- if (employee.SelectedItems.Count > 0)
- {
- foreach (var employee in employee.SelectedItems)
- {
- if (checkEmployeeManager(employee.Value, manager.Value))
- {
- foreach (var report in reportList.SelectedItems)
- {
- SetSelectedReportParameters(report.Key, report.Value);
- SetParameterValues(manager.Value.ToString(), "ManagerId");
-
- SetParameterValues(employee.Value.ToString(), "EmployeeId");
-
- strPath = Path.Combine(strFolderPath, manager.Key, employee.Key.Split('-')[0]);
- if (!File.Exists(strPath))
- {
- if (employee.Value != -1)
- strPath = strPath + "_" + employee.Key.Split('-')[1];
- await System.Threading.Tasks.Task.Run(() => Directory.CreateDirectory(strPath));
- if (employee.Value != -1)
- fileName = employee.Key.Split('-')[1] + "_" + report.Key + ".pdf";
- else
- fileName = report.Key.Split('-')[1] + ".pdf";
-
- GenerateReport(strPath, report.Value, fileName);
- }
- }
- }
- }
- }
- }
- var message = string.Format("Report: {0} has been downloaded to the path {1} ", reportName, strPath);
- MessageBox.Show(message, "Success", MessageBoxButton.OK, MessageBoxImage.Information);
- }
- catch (Exception ex)
- {
- var message = string.Format("Encountered an unhandled exception: {0}", ex.Message);
- MessageBox.Show(message, "Unhandled Exception", MessageBoxButton.OK, MessageBoxImage.Hand);
- }
- }
-
- private void GenerateReport(string DirectoryPath, int ReportId = 0, string fileName = null)
- {
- string expression = "ReportId =" + ReportId;
- DataRow[] selectedRows = reportData.Select(expression);
- string reportName = string.Empty;
-
- ReportExecutionService rsExec = new ReportExecutionService
- {
- Credentials = new NetworkCredential(),
- Url = "http://mysite/ReportServer/ReportExecution2005.asmx"
- };
- reportName = selectedRows[0]["ReportFileName"].ToString().Replace(".rdl", string.Empty);
- reportName = reportName.Contains("&") ? reportName.Substring(0, reportName.IndexOf("&")) : reportName;
- string fullPath = "/Reports/" + reportName;
-
- byte[] result = null;
- string reportPath = fullPath;
- string format = "PDF", devInfo = @"<DeviceInfo><Toolbar>True</Toolbar></DeviceInfo>";
- string historyID = null;
-
- ReportExecution2005.DataSourceCredentials[] credentials = new ReportExecution2005.DataSourceCredentials[1];
- credentials[0] = new ReportExecution2005.DataSourceCredentials
- {
- DataSourceName = myds,
- UserName = userid,
- Password = pwd
- };
-
- ReportExecution2005.Warning[] warnings = null;
- string[] streamIDs = null;
-
- ExecutionInfo execInfo = new ExecutionInfo();
- ExecutionHeader execHeader = new ExecutionHeader();
-
- rsExec.ExecutionHeaderValue = execHeader;
-
-
- execInfo = rsExec.LoadReport(reportPath, historyID);
- rsExec.SetExecutionCredentials2(credentials);
- rsExec.SetExecutionParameters2(parms, null);
-
- try
- {
- result = rsExec.Render(format, devInfo, out string extension, out string encoding, out string mimeType, out warnings, out streamIDs);
- execInfo = rsExec.GetExecutionInfo();
- }
- catch (SoapException e)
- {
- throw new Exception(e.Detail.OuterXml);
- }
- try
- {
- fileName = String.Join(string.Empty, fileName.Split(Path.GetInvalidFileNameChars()));
- File.WriteAllBytes(Path.Combine(DirectoryPath, fileName), result);
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message);
- }
- }
I am calling the button click event asynchronously and calling create folder, looking for better code which improves performance - private async void btnReport_Click(object sender, RoutedEventArgs e)
- {
- await CreateFolder();
- }