Search code examples
c#windows-phone-8internet-connectionnetwork-connection

Give error if there is no network connection when attempting to save file to isolated storage


I'd like to give the user an error message and prevent app from crashing, if they do not have a network connection when attempting to save a file to the isolatestorage. What I have does not give me an error when building, but crashes when I attempt to save a file.

 private void LongListSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        LongListSelector selector = sender as LongListSelector;

        // verifying our sender is actually a LongListSelector
        if (selector == null)
            return;

        SoundData data = selector.SelectedItem as SoundData;

        // verifying our sender is actually SoundData
        if (data == null)
            return;


        if (data.IsDownloaded)
        {
            if (audioStream != null)
            {
                audioStream.Close();
                audioStream.Dispose();
            }

            audioStream = IsolatedStorageFile.GetUserStoreForApplication().OpenFile(data.SavePath, FileMode.Open, FileAccess.Read, FileShare.Read);

            AudioPlayer.SetSource(audioStream);
            AudioPlayer.Play();

        }
        else
        {

            WebClient client = new WebClient();
            client.OpenReadCompleted += (senderClient, args) =>
            {
                using (IsolatedStorageFileStream fileStream = IsolatedStorageFile.GetUserStoreForApplication().CreateFile(data.SavePath))
                {
                    if (args == null || args.Cancelled || args.Error != null)
                    {
                        MessageBox.Show("Please check your network/cellular connection. If you have a network connection, verify that you can reach drobox.com");
                        return;
                    }

                    args.Result.Seek(0, SeekOrigin.Begin);
                    args.Result.CopyTo(fileStream);
                    AudioPlayer.SetSource(fileStream);
                    AudioPlayer.Play();


                }
            };
            client.OpenReadAsync(new Uri(data.FilePath));

        }

Solution

  • Why not try checking the parameter before attempting to use it?

    using (IsolatedStorageFileStream fileStream = IsolatedStorageFile.GetUserStoreForApplication().CreateFile(data.SavePath))
    {                    
        if (args == null || args.Cancelled || args.Error != null)
        {
            MessageBox.Show("No connection");
            return;
        }
    
        args.Result.Seek(0, SeekOrigin.Begin);
        args.Result.CopyTo(fileStream);
        AudioPlayer.SetSource(fileStream);
        AudioPlayer.Play();
    }
    

    or like user574632 said, wrap the entire thing in a try/catch. This will allow it to fail gracefully so the user will see whatever error you put in the catch block without crashing the entire application.