Kishore

Kishore

  • NA
  • 65
  • 8.9k

Download RDL files in to pdf file asynchronously,

Jul 13 2018 7:58 AM
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
 
  1. private async System.Threading.Tasks.Task CreateFolder()  
  2.         {  
  3.             try  
  4.             {  
  5.                 string reportName = string.Empty;  
  6.                 string fileName = string.Empty;  
  7.                 string strPath = string.Empty;  
  8.                 string strFolderPath = @"c:\My Reports";  
  9.   
  10.                 foreach (var manager in manager.SelectedItems)  
  11.                 {  
  12.   
  13.   
  14.                     if (employee.SelectedItems.Count > 0)  
  15.                     {  
  16.                         foreach (var employee in employee.SelectedItems)  
  17.                         {  
  18.                             if (checkEmployeeManager(employee.Value, manager.Value))  
  19.                             {  
  20.                                 foreach (var report in reportList.SelectedItems)  
  21.                                 {  
  22.                                     SetSelectedReportParameters(report.Key, report.Value);  
  23.                                     SetParameterValues(manager.Value.ToString(), "ManagerId");  
  24.   
  25.                                     SetParameterValues(employee.Value.ToString(), "EmployeeId");  
  26.                                       
  27.                                     strPath = Path.Combine(strFolderPath, manager.Key, employee.Key.Split('-')[0]);  
  28.                                     if (!File.Exists(strPath))  
  29.                                     {  
  30.                                         if (employee.Value != -1)  
  31.                                             strPath = strPath + "_" + employee.Key.Split('-')[1];  
  32.                                         await System.Threading.Tasks.Task.Run(() => Directory.CreateDirectory(strPath));  
  33.                                         if (employee.Value != -1)  
  34.                                             fileName = employee.Key.Split('-')[1] + "_" + report.Key + ".pdf";  
  35.                                         else  
  36.                                             fileName = report.Key.Split('-')[1] + ".pdf";  
  37.   
  38.                                         GenerateReport(strPath, report.Value, fileName);  
  39.                                     }  
  40.                                 }  
  41.                             }  
  42.                         }  
  43.                     }  
  44.                 }  
  45.                 var message = string.Format("Report: {0} has been downloaded to the path {1} ", reportName, strPath);  
  46.                 MessageBox.Show(message, "Success", MessageBoxButton.OK, MessageBoxImage.Information);  
  47.             }  
  48.             catch (Exception ex)  
  49.             {  
  50.                 var message = string.Format("Encountered an unhandled exception:  {0}", ex.Message);  
  51.                 MessageBox.Show(message, "Unhandled Exception", MessageBoxButton.OK, MessageBoxImage.Hand);  
  52.             }  
  53.         }  
  54.   
  55. private void GenerateReport(string DirectoryPath, int ReportId = 0, string fileName = null)  
  56.         {  
  57.             string expression = "ReportId =" + ReportId;  
  58.             DataRow[] selectedRows = reportData.Select(expression);  
  59.             string reportName = string.Empty;  
  60.   
  61.             ReportExecutionService rsExec = new ReportExecutionService  
  62.             {  
  63.                 Credentials = new NetworkCredential(),  
  64.                 Url = "http://mysite/ReportServer/ReportExecution2005.asmx"  
  65.             };  
  66.             reportName = selectedRows[0]["ReportFileName"].ToString().Replace(".rdl", string.Empty);  
  67.             reportName = reportName.Contains("&") ? reportName.Substring(0, reportName.IndexOf("&")) : reportName;  
  68.             string fullPath = "/Reports/" + reportName;  
  69.   
  70.             byte[] result = null;  
  71.             string reportPath = fullPath;  
  72.             string format = "PDF", devInfo = @"<DeviceInfo><Toolbar>True</Toolbar></DeviceInfo>";  
  73.             string historyID = null;  
  74.   
  75.             ReportExecution2005.DataSourceCredentials[] credentials = new ReportExecution2005.DataSourceCredentials[1];  
  76.             credentials[0] = new ReportExecution2005.DataSourceCredentials  
  77.             {  
  78.                 DataSourceName = myds,  
  79.                 UserName = userid,  
  80.                 Password = pwd  
  81.             };  
  82.   
  83.             ReportExecution2005.Warning[] warnings = null;  
  84.             string[] streamIDs = null;  
  85.   
  86.             ExecutionInfo execInfo = new ExecutionInfo();  
  87.             ExecutionHeader execHeader = new ExecutionHeader();  
  88.   
  89.             rsExec.ExecutionHeaderValue = execHeader;  
  90.   
  91.   
  92.             execInfo = rsExec.LoadReport(reportPath, historyID);  
  93.             rsExec.SetExecutionCredentials2(credentials);  
  94.             rsExec.SetExecutionParameters2(parms, null);  
  95.   
  96.             try  
  97.             {  
  98.                 result = rsExec.Render(format, devInfo, out string extension, out string encoding, out string mimeType, out warnings, out streamIDs);  
  99.                 execInfo = rsExec.GetExecutionInfo();  
  100.             }  
  101.             catch (SoapException e)  
  102.             {  
  103.                 throw new Exception(e.Detail.OuterXml);  
  104.             }  
  105.             try  
  106.             {  
  107.                 fileName = String.Join(string.Empty, fileName.Split(Path.GetInvalidFileNameChars()));  
  108.                 File.WriteAllBytes(Path.Combine(DirectoryPath, fileName), result);  
  109.             }  
  110.             catch (Exception e)  
  111.             {  
  112.                 Console.WriteLine(e.Message);  
  113.             }  
  114.         }  
I am calling the button click event asynchronously and calling create folder, looking for better code which improves performance 
  1. private async void btnReport_Click(object sender, RoutedEventArgs e)  
  2.         {  
  3.             await CreateFolder();  
  4.         }