How to store data in Isolated Storage in Windows Phone 7

I am building an app for windows phone 7. I am using data which is coming from the web service which i want to save in isolated storage so that next time when the data is viewed it shows the data even when offline.

My cs file is:

public about()
{
    InitializeComponent();

    KejriwalService.aapSoapClient myclient = new KejriwalService.aapSoapClient();
    myclient.getarvindAboutCompleted += new EventHandler<KejriwalService.getarvindAboutCompletedEventArgs>(myclient_getarvindAboutCompleted);
    myclient.getarvindAboutAsync();        
}

void myclient_getarvindAboutCompleted(object sender, KejriwalService.getarvindAboutCompletedEventArgs e)
{
    var data = e.Result;

    XElement xml = XElement.Parse(data);

    aboutview.Text = xml.Elements("UserDetails").Elements("about_details").First().Value;
}

Answers


One of the easiest way to store data is to store them in the IsolatedStorageSettings. You can make a property for this purpose:

        string PropertyName
        {
            get
            {
                var settings = System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings;
                if (settings.Contains("valueKeyName"))
                    return (string)settings["valueKeyName"];
                else
                    return null;
            }
            set
            {
                var settings = System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings;
                if (settings.Contains("valueKeyName"))
                    settings["valueKeyName"] = value;
                else
                    settings.Add("valueKeyName", value);
            }
        }

IsolatedStorageSettings store pairs key-value in the dictionary. You should just choose key name of your storing value and name of the property.


with the help ofIsolatedStorageSettings you could save your data in the form of key value paire like this:

 var Iso_settings = System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings;   
if (!Iso_settings.Contains("yourDataKey"))
    {
      Iso_settings.Add("yourDataKey", yourDataValue);
      Iso_settings.Save()//This will save your data in isolated storage.
    }

Don't go for Sqlite if you don't want offline data and to store Data two best method is to use Isolated Storage and using linq so if you have only 1-2 data members and you have to change there values then you can use Isolated Storage otherwise go for linq if you have 2-3 columns and rows to store data in form of Tables. Isolated storage is used like this:

try
{
    if (IsolatedStorageSettings.ApplicationSettings.Contains("email_id"))
    {
        IsolatedStorageSettings.ApplicationSettings["email_id"] = emailid;
        IsolatedStorageSettings.ApplicationSettings["password"] = password;
        IsolatedStorageSettings.ApplicationSettings.Save();
    }
    else
    {
        IsolatedStorageSettings.ApplicationSettings.Add("email_id", emailid);
        IsolatedStorageSettings.ApplicationSettings.Add("password", password);
        IsolatedStorageSettings.ApplicationSettings.Save();
    }
}
catch (Exception ex)
{
    Console.WriteLine(ex.InnerException);
}

For linq http://www.codeproject.com/Articles/43025/A-LINQ-Tutorial-Mapping-Tables-to-Objects go for the link.


Need Your Help

Autoplay attribute for HTML 5 in android devieces

android html5-video

I am trying to integrate an HTML 5 video player with It's auto-play attribute on my site. It is working fine on Web and other devices but it is not working with any android device which has 4.0 and...

Sending nested FormData on AJAX

ajax string nested form-data

I need to send some data using ajax and FormData, because I want to send a file and some other parameters. The way I usually send data is this: