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 (fromInvoiceNo > toInvoiceNo) { UIHelper.ShowErrorMessage("From Invoice No. cannot not be greater than To Invoice No."); 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 <IMS.InvoiceServiceRef.ProductDetails> lstInvoiceProduct = pxy.InvoiceProductPatch(fromInvoiceNo, toInvoiceNo, fromInvoiceDate, toInvoiceDate, ProductID, Global.UserID); if (lstInvoiceProduct == null || lstInvoiceProduct.Count == 0) { UIHelper.ShowMessage("Data not found"); return; } using (ReportViewer reportViewer = new ReportViewer()) { reportViewer.LocalReport.DataSources.Clear(); reportViewer.LocalReport.DataSources.Add(new ReportDataSource("InvoiceProductDataSet", lstInvoiceProduct)); reportViewer.LocalReport.ReportPath = System.Windows.Forms.Application.StartupPath + "//Views//Reports//InvoiceProductPatch.rdlc"; reportViewer.RefreshReport(); byte[] Bytes = reportViewer.LocalReport.Render(format: "Excel", deviceInfo: ""); using (FileStream stream = new FileStream(folderPath + "//InvoiceProductPatch.xls", FileMode.Create)) { stream.Write(Bytes, 0, Bytes.Length); } } } UIHelper.ShowMessage("File generated!"); } catch (FaultException ex) { UIHelper.ShowErrorMessage(ex.Message); } catch (Exception ex) { UIHelper.ShowErrorMessage(ex.Message); } }