0
votes

I have a class that I wrote that saves and retrieves any objects to a windows phone isolated storage system. Have a look...

public class DataCache
{
    // Method to store an object to phone ************************************
    public void StoreToPhone(string key, Object objectToStore)
    {
        var settings = IsolatedStorageSettings.ApplicationSettings;

        try
        {
            if (existsInStorage(key))
            {
                settings.Remove(key);
                settings.Add(key, objectToStore);
            }
            else
            {
                settings.Add(key, objectToStore);
            }
        }
        catch (Exception e)
        {
            MessageBox.Show("An error occured while trying to cache data: " + e.Message);
        }
    }

    // Method to retrieve an object ******************************************
    public Object retrieveFromPhone(string key)
    {            
        var settings = IsolatedStorageSettings.ApplicationSettings;            
        Object retrievedObject = null;

        try
        {
            if (existsInStorage(key))
            {
                settings.TryGetValue<Object>(key, out retrievedObject);
            }
            else
            {
                MessageBox.Show(string.Format("Cannot find key {0} in isolated storage", key));
            }
        }
        catch(Exception e)
        {
            MessageBox.Show("An error occured while trying to retrieve cache object: "+e.Message);
        }
        return retrievedObject;
    }

    // Helper method to check if there is space on the phone to cache the data
    private bool IsSpaceAvailable(long spaceReq)
    {
        using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
        {
            long spaceAvail = store.AvailableFreeSpace;
            if (spaceReq > spaceAvail)
            {
                return false;
            }
            return true;
        }
    }

    // Method to check if key exists in isolated storage *********************
    public bool existsInStorage(string key)
    {
        var settings = IsolatedStorageSettings.ApplicationSettings;
        bool objectExistsInStorage = settings.Contains(key);
        return objectExistsInStorage;
    }
}

When I run my app and try and store some data using my StoreToPhone() method I get the following error:

An error occurred while trying to cache data: Value does not fall within the expected range

I don't exactly know what this means.. Is it not expecting this type of object? I'm not sure... I'm passing it a custom class I wrote fyi.

1
Does it fail even when saving a simple string?trydis

1 Answers

1
votes

Some times before I too ran into same problem.

It seems the error says 'there may be duplicate value in the storage'. so i used 'remove' before 'add', and then also i found not everything was saved, so i used 'save' function after 'add'.

It worked for me...

IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;
  appSettings.Remove("name");
  appSettings.Add("name", "James Carter");
  appSettings.Save();
  tbResults.Text = (string)appSettings["name"];