Reputation: 71
I stored multiple data in isolated storage setting but when retrive data at that time only one data is displayed
I use for loop but its not giving perfect out put
c# code is:
for (int i = 0; i <= IsolatedStorageSettings.ApplicationSettings.Count; i++)
{
cityname = IsolatedStorageSettings.ApplicationSettings["CityDetail"] as string;
ads.Add(new Transaction(cityname));
}
saving code for isolated storage setting
if (!setting.Contains("CityDetail"))
{
setting.Add("CityDetail", cityname);
}
else
{
setting["CityDetail"] = cityname;
}
setting.Save();
Upvotes: 0
Views: 273
Reputation: 1445
The Isolated Storage uses Key-Value pair to save data.
So for example if you do:
if (!setting.Contains("CityDetail"))
{
setting.Add("CityDetail", "abc");
}
else
{
setting["CityDetail"] = "abc";
}
setting.Save();
So after doing this, the value for the key - CityDetail is abc.
After this if you do:
if (!setting.Contains("CityDetail"))
{
setting.Add("CityDetail", "def");
}
else
{
setting["CityDetail"] = "def";
}
setting.Save();
Then, the value for the key CityDetail will be replaced and the new value will be def.
The essence is "In Key-Value storage method, there can be only one value for one key."
To save multiple values against a single key, you can do something like this:
void addCityName(string cityName)
{
List<string> existingList = getCityNames();
if(existingList==null)
{
existingList = new List<string>();
existingList.Add(cityName);
}
string json = JsonConvert.SerializeObject(exitingList);
if(!IsolatedStorageSettings.ApplicationSettings.Contains("CityDetails"))
{
IsolatedStorageSettings.ApplicationSettings.Add("CityDetails",json);
}
else
{
IsolatedStorageSettings.ApplicationSettings["CityDetails"] = json;
}
}
List<string> getCityNames()
{
string json = IsolatedStorageSettings.ApplicationSettings["CityDetails"] as string;
if(json == null)
return null;
JArray arr = JArray.parse(json);
List<string> list = new List();
for(int i=0; i < arr.length; i++)
{
list.add(arr[i].toString());
}
return list;
}
For this you need the Json.Net library.
Upvotes: 1