If I call ExportAsync with the destination file already opened, the AsynchronousExportError event is properly triggered. However, the "Export complete" success message still shows at the end of the process even though the records were not successfully exported. Is there a way to change this message or otherwise show that there was a problem? I trap the AsynchronousExportError event and show an appropriate error message, but then it seems strange that it gets followed with a message that the export was successful.
Hi,
What is your expectation here as far as the message goes. I understand that the grid should not display a message that the export operation succeeded. That's clearly a bug.
But is it necessary, or desirable, for the grid to show a message at all in this case?
Right now, the grid only shows a success message, there is never a failure message. If the user cancels the export the grid's asynchronous status display simply goes away and returns the grid to it's normal appearance.
I would think that the same thing should happen here. The asynch UI would simply go away and the error is communicated to the user via a MessageBox - or you could cancel it like you are doing in your sample and display it in some other way.
Does that seem right?
Hi Mike-
If there's a way to cancel the export via code while in AsynchronousExportError, that would be optimal. As you mentioned, the async UI would go away and the error could be communicated to the user via a MessageBox. However, if the export is not cancelled via code I think an error message in the async UI would be appropriate.
I looked into this some more and I found a much better workaround.
The way you are currently doing this, there is a big potential for a lot of wasted effort. By the time the export operation fails, most of the work is already done, and all of that work was unnecessary since it failed. It makes a lot more sense to create the file stream up front and make sure it's not locked before you start exporting.
So you could change your code like this:
//ultraGridExcelExporter1.ExportAsync(ultraGrid1, ofd.FileName); FileStream fileStream = new FileStream(ofd.FileName, FileMode.CreateNew); Workbook wb = new Workbook(WorkbookFormat.Excel2007); ultraGridExcelExporter1.ExportAsync(ultraGrid1, wb); wb.Save(fileStream);
Put a try... catch around the creation of the FileStream and if an expceiotn exception is raised, you will know that the file is in use and can handle it without ever starting the export and wasting all that processing.
This is a great solution that I'll be adopting, thanks Mike! One minor tidbit: there should be a fileStream.Close after the wb.Save call. If you try to open the file while your form with the exporter is still open, the export file is still locked.
After some more testing with a grid containing 10K records, I've found that the workbook must be saved and the file stream closed in the ExportEnded event instead. In your previous sample, the wb.Save gets hit before the ExportAsync has completed.
Yes, you are correct on both counts. Sorry... I guess I got a little over-enthusiastic about finding a workaround. :)
We are still going to fix it so that the CancelAsynchronousExport works when called inside of the AsynchronousExportError event. But you really don't need it, now.
Thanks for your help on the issue Mike, much appreciated! Having the CancelExport work inside the error event will also be a nice addition in the future.