Search code examples
c#visual-studio-2012windows-phone-8storageapplication-data

Unhandled Exception when trying to save data in Win Mobile 8 c#


I'm using the code below to try and save to the local setting in Windows Mobile 8 using c#.

 public void SaveInfo(string key, string value)
        {
            if (ApplicationData.Current.LocalSettings.Values.ContainsKey(key))
            {
                if (ApplicationData.Current.LocalSettings.Values[key].ToString() != null)
                {
                    // do update
                    ApplicationData.Current.LocalSettings.Values[key] = value;
                }
            }
            else
            {

                // do create key and save value, first time only.
                ApplicationData.Current.LocalSettings.CreateContainer(key, ApplicationDataCreateDisposition.Always);
                if (ApplicationData.Current.LocalSettings.Values[key] == null)
                {
                    ApplicationData.Current.LocalSettings.Values[key] = value;
                }
            }
        }

When calling the code the debug crashes with the exception below:

An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in System.Windows.ni.dll

Any idea


Solution

  • I needed to use the IsolatedStorageSettings feature, as below:

    IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
    
    
                settings["test"] = urlText.Text;
                settings.Save();