How do I use local database in windows phone 7 to save just a value, then retrieve that value every time the app is loaded (opened)?
Asked
Active
Viewed 1,402 times
1 Answers
7
If all you want to do is store a value for retrieval, then I would recommend using IsolatedStorage
, particularly the ApplicationSettings
class.
An example of it's usage:
using System.IO.IsolatedStorage;
//storing value
int someValue = 10;
IsolatedStorageSettings.ApplicationSettings.Add("MyKey",someValue);
//write or update value
IsolatedStorageSettings.ApplicationSettings["MyKey"] = someValue;
//write to disk
IsolatedStorageSettings.ApplicationSettings.Save();
//reading value
if(IsolatedStorageSettings.ApplicationSettings.Contains("MyKey"))
{
int readValue = (int) IsolatedStorageSettings.ApplicationSettings["MyKey"];
}
Mango now offers MSSqlCE support, but for a set of values, it's overkill. A database is more appropriate if you need to store relational data, as opposed to persisting user/application settings.
While IsolatedStorage is nice, it can be expensive to read and write to. Avoid reading from IsolatedStorage from your UI thread, which will cause your app to appear to be unresponsive.

Alan
- 45,915
- 17
- 113
- 134
-
I keep getting this error:"Value does not fall within expected range", I did check and I have no duplicated value. No matter what Key value I use... – Nghia Nguyen Aug 05 '11 at 06:42
-
@ng_ducnghia that sounds like a new question. And woudl almost certainly require you to show your code for it to eb answered. – Matt Lacey Aug 05 '11 at 08:36
-
1I solved it, just need to add the Clear() function before Add() and it works fine: appSettings.Clear(); – Nghia Nguyen Aug 05 '11 at 08:53
-
I don't recommend using Clear(), as it will clear out the entire settings dictionary. Instead, you can use appSettings['mykey'] = value; I've updated my answer to reflect this. – Alan Aug 05 '11 at 16:56