0

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

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Dan
  • 2,304
  • 6
  • 42
  • 69
  • can you attach a debugger and see where the exception is happening? Nearly impossible to tell what is going wrong here based on the information posted (too many possible sources of exceptions here) – JaredPar Nov 30 '13 at 19:21
  • it occurs on the first call: – Dan Nov 30 '13 at 19:29
  • if (ApplicationData.Current.LocalSettings.Values.ContainsKey(key)) – Dan Nov 30 '13 at 19:30
  • What do you pass in as key? – Igor Ralic Dec 01 '13 at 01:00
  • To try and save the info I use SaveInfo("mcsserver", urlText.Text); urlText being the TextBox containing the info I need saving to a key called "mcsserver" – Dan Dec 01 '13 at 02:55

1 Answers1

0

I needed to use the IsolatedStorageSettings feature, as below:

IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;


            settings["test"] = urlText.Text;
            settings.Save();
Dan
  • 2,304
  • 6
  • 42
  • 69
  • Your code is redundant. In other words. It does the same thing no matter what. If the dictionary does not contain the key, it adds the key, it is does, it overrides the key. In either case, the dictionary has the key and value. Simplify your code by not checking if it contains the key and simply do `settings["test"] = urlText.Text;` – Shawn Kendrot Dec 01 '13 at 05:45
  • Thanks for the tip Shawn, I Was just using code from another example – Dan Dec 01 '13 at 19:43
  • The key takeaway if the code as in this answer is slower. With the code you posted, the dictionary needs to do two hash lookups. When only setting via the index `settings["test"] = urlText.Text` it only has to do one hash lookup. 90% of the time two hash lookups are not noticeable. But if you are doing a lot of work with a dictionary (inserting thousands/millions of records), this can become costly. – Shawn Kendrot Dec 01 '13 at 20:22