This is a migrated thread and some comments may be shown as answers.

CSV Export - How to handle file-in-use error

6 Answers 90 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Tim
Top achievements
Rank 1
Tim asked on 16 Sep 2010, 03:56 PM
I wrote a CSV export event handler for a RadGridView based on this example:

http://demos.telerik.com/silverlight/#GridView/ExportingPagedData

private void ExportCSV(object sender, RoutedEventArgs e)
{
    SaveFileDialog dialog = new SaveFileDialog
                                {DefaultExt = ".csv", Filter = "CSV Files|*.csv|All Files|*.*", FilterIndex = 1};
 
    bool? result = dialog.ShowDialog();
 
    if (result == true)
    {
        int originalPageSize = EventDataPager.PageSize;
        int originalPageIndex = EventDataPager.PageIndex;
        EventDataPager.PageSize = 0;
 
        try
        {
            using (Stream stream = dialog.OpenFile())
            {
                EventViewerGridView.Export(stream, GridViewExportOptions);
            }
        }
        catch(IOException ioe)
        {
            RadWindow.Alert(ioe.Message);
        }
 
        EventDataPager.PageSize = originalPageSize;
        EventDataPager.PageIndex = originalPageIndex;
    }
}

If the saved file is open by Excel already, then the dialog.OpenFile() call throws an IOException.  I display this to the user with a RadWindow.Alert() from the catch(IOException...) block, however, I subsequently get the following exception:

Error: An unknown error has occurred.
Detail: System.InvalidOperationException
This operation can only occur on the UI Thread.
   at System.Windows.Hosting.NativeHost.VerifyThread()
   at System.Windows.SaveFileStream.Dispose(Boolean disposing)
   at System.IO.FileStream.Finalize()


I can't check stream.CanWrite because it does not get past the OpenFile() call. 
I put the RadWindow.Alert() on the Dispatcher as a test without any apparent effect.

How can I handle this properly without generating this subsequent error?

Thanks,
Tim

6 Answers, 1 is accepted

Sort by
0
Veselin Vasilev
Telerik team
answered on 20 Sep 2010, 03:52 PM
Hi Tim,

Have you tried replacing the RadWindow.Alert with MessageBox.Show?
It fixes the problem in 99% of the cases. Can you please try?

This is something not directly connected to RadGridView. You can reproduce it with any open file if you try to open it again.

Greetings,
Veselin Vasilev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Tim
Top achievements
Rank 1
answered on 20 Sep 2010, 03:57 PM
Thanks for the reply.  MessageBox.Show() fails in exactly the same way, and it doesn't look like the rest of my app either ;)

Tim
0
Veselin Vasilev
Telerik team
answered on 20 Sep 2010, 04:55 PM
Hello Tim,

To my regret I am not aware of any solution to this general problem so far.

Best wishes,
Veselin Vasilev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
David
Top achievements
Rank 1
answered on 19 Oct 2010, 10:25 PM
This is an issue with the Silverlight runtime. I ran into this problem as well, and I implemented the workaround suggested here:
http://forums.silverlight.net/forums/t/176869.aspx

The workaround is to explicitly ignore that particular unhandled exception in App.xaml.cs.
0
Kevin
Top achievements
Rank 1
answered on 24 May 2011, 10:21 PM

In the 2011.1.419.1040 release, I can't even get the IOException to be thrown anymore. Was there a change made in the Telerik code to automatically suppress this error?

I execute the code below, which trys to save a RadGrid export to an .xml file, which I have open in Excel, and I don't get any errors.  Additionally, the .xml file is not updated!  This is a problem, since users may think are getting new data in their .xml file, but are not.  The save should return an IOException, "The process cannot access the file '<FileName>' because it is being used by another process".  I could then tell the user the file was not updated.

I am in big trouble the first time a user makes a critical business decision using old data, just because they had the file open in Excel when they tried to export and I couldn't prompt them.

private void Button_Click(object sender, RoutedEventArgs e)
{
    string extension = "xml";
    ExportFormat format = ExportFormat.ExcelML;
    SaveFileDialog dialog = new SaveFileDialog()
    {
        DefaultExt = extension,
        Filter = String.Format("Excel XML files (*.{0})|*.{0}|All files (*.*)|*.*", extension),
        FilterIndex = 1
    };
    if (dialog.ShowDialog() == true)
    {
        using (Stream stream = dialog.OpenFile())
        {
            Grid.Export(stream,
                new GridViewExportOptions()
                {
                    Format = format,
                    ShowColumnHeaders = true,
                    ShowColumnFooters = true,
                    ShowGroupFooters = true,
                });
        }
    }
}
0
Vlad
Telerik team
answered on 25 May 2011, 06:57 AM
Hello,

How we can change a code which is not part of our assemblies? The grid export method will use just a stream - it is up to developer to handle the code with SaveFileDialog.

Best wishes,
Vlad
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
GridView
Asked by
Tim
Top achievements
Rank 1
Answers by
Veselin Vasilev
Telerik team
Tim
Top achievements
Rank 1
David
Top achievements
Rank 1
Kevin
Top achievements
Rank 1
Vlad
Telerik team
Share this question
or