sql - Local database changes not saved after SubmitChanges() -
i'm using local database store data. have file *.sdf load isolated storage, because want application work in offline mode, if user doesn't want, doesn't have update data.
this how datacontext like:
public class tablesdatacontext : datacontext { // specify connection string static, used in main page , app.xaml. public static string dbconnectionstring = "data source=isostore:/mydatabase.sdf"; // pass connection string base class. public tablesdatacontext(string connectionstring) : base(connectionstring) { } // specify single table items. public table<customeritem> customerstable; public table<productitem> productstable; } here in app.xaml.cs if needed create database:
// create database if not exist. using (tablesdatacontext db = new tablesdatacontext(tablesdatacontext.dbconnectionstring)) { if (db.databaseexists() == false) { //create database db.createdatabase(); } } datacontext = new userdatacontext(); and settingspage in update needed information:
public partial class settingspage : phoneapplicationpage { // data context local database private tablesdatacontext tablesdb; // define observable collection property controls can bind to. private observablecollection<customeritem> _customerstable; public observablecollection<customeritem> customerstable { { return _customerstable; } set { if (_customerstable != value) { _customerstable = value; notifypropertychanged("customerstable"); } } } // define observable collection property controls can bind to. private observablecollection<productitem> _producttable; public observablecollection<productitem> producttable { { return _producttable; } set { if (_producttable != value) { _producttable = value; notifypropertychanged("producttable"); } } } public void addingcustomerstodatabase(list<customerjson> customerslist) { // define query gather of to-do items. var customerstablesindb = customeritem todo in tablesdb.customerstable select todo; // execute query , place results collection. customerstable = new observablecollection<customeritem>(customerstablesindb); tablesdb.customerstable.deleteallonsubmit(customerstable); tablesdb.submitchanges(); foreach (customerjson customer in customerslist) { // create new to-do item based on text box. customeritem newcustomer = new customeritem { // filling customer data json object }; // add to-do item observable collection. customerstable.add(newcustomer); // add to-do item local database. tablesdb.customerstable.insertonsubmit(newcustomer); tablesdb.submitchanges(); } } public void addingproductstodatabase(list<productjson> productslist) { // same things in previouse method products objects } private void load_click(object sender, routedeventargs e) { addingcustomerstodatabase(customerslist); addingproductstodatabase(productslist); } } after updating information i'm sure data up-to-date, until close application. if reopen application, still has old data. if understand correctly shouldn't saved in *.sdf file? btw, i'm using linq.
edit:
i managed check , found out right after json parsing, database updated (*.sdf file filled new data), when close , open application again (not build, reopen on emulator), again has old data.
please try following
- verify if json data updated
try using "addobject" instead of "insertonsubmit" below
tablesdb.customerstable.addobject(newcustomer);
Comments
Post a Comment