private void cmdGenerateExcel_Execute(object obj) { try { string folderPath = string.Empty; if (fromInvoiceDate > toInvoiceDate) { UIHelper.ShowErrorMessage("From Date cannot not be greater than To Date"); return; } ////if ((toInvoiceDate - fromInvoiceDate).Days > 31) ////{ //// UIHelper.ShowErrorMessage("Days Difference should not be greater than 31"); //// return; ////} if (fromSettlementDate > toSettlementDate) { UIHelper.ShowErrorMessage("From Settlement Date cannot not be greater than To Date"); return; } ////if ((toSettlementDate - fromSettlementDate).Days > 31) ////{ //// UIHelper.ShowErrorMessage("Days Difference should not be greater than 31"); //// return; ////} using (System.Windows.Forms.FolderBrowserDialog browse = new System.Windows.Forms.FolderBrowserDialog()) { browse.ShowDialog(); folderPath = browse.SelectedPath; } if (string.IsNullOrEmpty(folderPath)) { UIHelper.ShowErrorMessage("Select Path to save excel file"); return; } using (InvoiceServiceClient pxy = new InvoiceServiceClient()) { ObservableCollection <SettlementDetails> lstInvoiceSettlement = pxy.InvoiceSettlementPatch(fromInvoiceNo, toInvoiceNo, fromInvoiceDate, toInvoiceDate, ProductID, fromSettlementDate, toSettlementDate, Global.UserID); if (lstInvoiceSettlement == null || lstInvoiceSettlement.Count == 0) { UIHelper.ShowMessage("Data not found"); return; } using (ReportViewer reportViewer = new ReportViewer()) { reportViewer.LocalReport.DataSources.Clear(); reportViewer.LocalReport.DataSources.Add(new ReportDataSource("InvoiceSettlementDataSet", lstInvoiceSettlement)); reportViewer.LocalReport.ReportPath = System.Windows.Forms.Application.StartupPath + "//Views//Reports//InvoiceSettlementPatch.rdlc"; reportViewer.RefreshReport(); byte[] Bytes = reportViewer.LocalReport.Render(format: "Excel", deviceInfo: ""); using (FileStream stream = new FileStream(folderPath + "//InvoiceSettlementPatch.xls", FileMode.Create)) { stream.Write(Bytes, 0, Bytes.Length); } } } } catch (FaultException ex) { UIHelper.ShowErrorMessage(ex.Message); } catch (Exception ex) { UIHelper.ShowErrorMessage(ex.Message); } }