Update data in isolated storage

Go To StackoverFlow.com

3

I have a code that adds email id and name in Isolated space. But it is not able to add multiple data. Also, how can I update in case any data was entered incorrectly?

namespace IsoStore
{

    public partial class MainPage : PhoneApplicationPage
    {

        // Constructor
        public MainPage()
        {
            InitializeComponent();
            IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;
        }


        private void button1_Click(object sender, RoutedEventArgs e)
        {
            IsolatedStorageSettings.ApplicationSettings.Add("email", "someone@somewhere.com");
            IsolatedStorageSettings.ApplicationSettings.Add("name", "myname");
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            textBlock1.Text = (string)IsolatedStorageSettings.ApplicationSettings["email"];
            textBlock2.Text = (string)IsolatedStorageSettings.ApplicationSettings["name"];
        }
    }
}
2012-04-04 02:12
by user1222006
what do you mean by multiple data - Alaa.Ali 2012-04-04 05:27
I would like to save, say for example, 10 email id and name - user1222006 2012-04-06 00:23
aha, now i got u - Alaa.Ali 2012-04-06 04:26


3

Cleaned up your code a little for you, using a helper method to do the store:

namespace IsoStore
{
    public partial class MainPage : PhoneApplicationPage
    {
        private IsolatedStorageSettings _appSettings;

        // Constructor
        public MainPage()
        {
            InitializeComponent();
            _appSettings = IsolatedStorageSettings.ApplicationSettings;                
        }


        private void button1_Click(object sender, RoutedEventArgs e)
        {
            SaveSetting("email", "someone@somewhere.com");
            SaveSetting("name", "myname");
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            textBlock1.Text = (string)_appSettings["email"];
            textBlock2.Text = (string)_appSettings["name"];
        }

        private void SaveSetting( string setting, string value )
        {
            if (_appSettings.Contains(setting))
            {
                _appSettings[setting] = value;
            }
            else
            {
                _appSettings.Add(setting, value);
            }
        }
    }
}

Try a few other examples to get your head around using IsolatedStorageSettings.

2012-04-04 08:13
by Gone Coding
And How to retrieve this data - Mansinh 2012-09-05 04:42
@Mansinh: Create another helper to return the strings (or null if they are not present - Gone Coding 2012-09-06 07:30


1

I have in mind 2 options, you either save your data to isolatedStorageFile MSDN Library OR ,this is what i might do in such case, You save under the key email all your emails as one string separate the emails with a char that is not allowed to be in an email, Coma "," lets say, when needed split your string and retrieve it to whatever makes you comfortable.

private void SaveSetting( string setting, string value )
    {
        if (_appSettings.Contains(setting))
        {
            _appSettings[settings] = _appSettings[settings] + "," + value;
        }
        else
        {
            _appSettings.Add(setting, value);
        }
    }

please note that this code segment is copied from HiTech Magic' answer.

2012-04-06 04:33
by Alaa.Ali
Not very useful if the same email is saved twice... Also cutting and pasting from another answer (including my settings typo) without reference, just to add a slight modification, is considered plagiarism.. - Gone Coding 2012-04-06 16:36
i thought the ultimate goal is to help each other anyhow my apologies. i copied your answer because it is how i would do it, i can delete the answer if you feel like it. Duplicate entries can be handled with ease. there is another option as well - Alaa.Ali 2012-04-06 18:14
Helping is not the issue. Reference anything you copy from others or you will have problems on this site. Thanks - Gone Coding 2012-04-06 23:09
Thanks a lot, and very sorry for not referencing you as the source. will definitely keep this in mind. is it good this way - Alaa.Ali 2012-04-07 04:49
Ads