Error when opening saved file

Brian J

I created a method to open a saved file based on the name of the file passed from another class, but I'm not sure if I'm calling or passing the file correctly between classes.

In this class the file names are stored in a list, when a name is selected the file name is passed to the other class and my OpenSavedFile method is called.

These are the two methods below,can someone point me in the right direction with this or tell me where I'm going wrong with it?

Method that passes file name to LocationDetails class:

//selects saved note name based on slected index in list
        private void NotesListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
                NavigationService.Navigate(new Uri(string.Format("/LocationDetails.xaml?note={0}", e.AddedItems[0]), UriKind.Relative));

            LocationDetails myFile = new LocationDetails();
            myFile.OpenSavedFile();

        }

My method that is supposed to open the file based on the name passed from the other previous class,but when the first statement is executed it gives this error System.AccessViolationException:

public void OpenSavedFile()
        {
            //breaks when stepping into below statement `System.AccessViolationException`
            string filename = this.NavigationContext.QueryString["note"];
            if (!string.IsNullOrEmpty(filename))
            {
                using (var store = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication())
                using (var stream = new IsolatedStorageFileStream(filename, FileMode.Open, FileAccess.ReadWrite, store))
                {
                    StreamReader reader = new StreamReader(stream);
                    this.NoteTextBox.Text = reader.ReadToEnd();
                    this.FilenameTextBox.Text = filename; reader.Close();
                }
            }
        }

This is the stack trace after the class breaks:

>   CarFind.DLL!CarFind.LocationDetails.OpenSavedFileFromList() Line 25 C#
    CarFind.DLL!CarFind.LocationDetailsList.NotesListBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) Line 38   C#
    System.Windows.ni.dll!System.Windows.Controls.Primitives.Selector.OnSelectionChanged(System.Windows.Controls.SelectionChangedEventArgs e)   Unknown
    System.Windows.ni.dll!System.Windows.Controls.Primitives.Selector.InvokeSelectionChanged(System.Collections.Generic.List<object> unselectedItems, System.Collections.Generic.List<object> selectedItems)    Unknown
    System.Windows.ni.dll!System.Windows.Controls.Primitives.Selector.SelectionChanger.End()    Unknown
    System.Windows.ni.dll!System.Windows.Controls.Primitives.Selector.SelectionChanger.SelectJustThisItem(int oldIndex, int newIndex)   Unknown
    System.Windows.ni.dll!System.Windows.Controls.ListBox.MakeSingleSelection(int index)    Unknown
    System.Windows.ni.dll!System.Windows.Controls.ListBox.HandleItemSelection(System.Windows.Controls.ListBoxItem item, bool isMouseSelection)  Unknown
    System.Windows.ni.dll!System.Windows.Controls.ListBox.OnListBoxItemClicked(System.Windows.Controls.ListBoxItem item)    Unknown
    System.Windows.ni.dll!System.Windows.Controls.ListBoxItem.OnManipulationCompleted(System.Windows.Input.ManipulationCompletedEventArgs e)    Unknown
    System.Windows.ni.dll!System.Windows.Controls.Control.OnManipulationCompleted(System.Windows.Controls.Control ctrl, System.EventArgs e) Unknown
    System.Windows.ni.dll!MS.Internal.JoltHelper.FireEvent(System.IntPtr unmanagedObj, System.IntPtr unmanagedObjArgs, int argsTypeIndex, int actualArgsTypeIndex, string eventName)    Unknown
Mikael Dúi Bolinder

In LocationDetailsList.xaml.cs, make it

//selects saved note name based on slected index in list
private void NotesListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (e.AddedItems.Count > 0)
        NavigationService.Navigate(new Uri(string.Format("/LocationDetails.xaml?note={0}", e.AddedItems[0]), UriKind.Relative));
}

In LocationDetails.xaml.cs, put:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    if(NavigationContext.QueryString == null)
    {
        //QueryString is null
    }
    else if (NavigationContext.QueryString.ContainsKey("note"))
    {
        _openSavedFile(NavigationContext.QueryString["note"]);
    }
    else
    {
        //QueryString does not contain a "note" parameter and QueryString is not null
    }

    base.OnNavigatedTo(e);
}

private _openSavedFile(string filename)
{
    using (var store = IsolatedStorageFile.GetUserStoreForApplication())
    using (var stream = new IsolatedStorageFileStream(filename, FileMode.Open, FileAccess.ReadWrite, store))
    {
        StreamReader reader = new StreamReader(stream);
        this.NoteTextBox.Text = reader.ReadToEnd();
        this.FilenameTextBox.Text = filename;
        reader.Close();
    }
}

Remove your old OpenSaveFile() method from LocationDetails.xaml.cs

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Flask - uploadnotallowed error - when renaming a file to be saved

From Dev

Encoding error when opening an Excel file with xlrd

From Dev

Emacs polymode gives error when opening file

From Dev

Error when opening file for reading in c#

From Dev

XML file to Excel, error when opening

From Dev

phpexcel Fatel Error when opening file

From Dev

Cryptic error in lxml when opening file

From Dev

Memory error when opening the file with read()

From Dev

Libgdx: File not found error 'Internal' when opening external file

From Dev

Error when opening Ruby File (Beginner) + How to run a file?

From Dev

Libgdx: File not found error 'Internal' when opening external file

From Dev

CSV file converter using VBA error when saved

From Dev

SQLiteManager: Error in opening file?

From Dev

Error handling in file opening

From Dev

Opening a file with a name error

From Dev

PHP file opening error

From Dev

PHP file opening error

From Dev

Error, opening json file

From Dev

PHP .zip file download error when opening in windows explorer

From Dev

Prevent Error Message from Appearing When Hitting Cancel of Opening a File

From Dev

File contains corrupted data error when opening Excel sheet with OpenXML

From Dev

Error when "Cancel" while opening a file in PyQt4

From Dev

PHP .zip file download error when opening in windows explorer

From Dev

ECCODES ERROR when opening .GRIB file in Spyder and in Visual Studio Code

From Dev

MVC: Return .xlsx file as saved download rather than opening in Excel

From Dev

Add todays date when opening saved email template

From Dev

Error opening file generated by epplus

From Dev

ZipException: error in opening zip file

From Dev

Weka error opening arff file

Related Related

HotTag

Archive