Problem with adding new record to DB

Dec 31, 2010 at 3:42 AM
Edited Dec 31, 2010 at 3:49 AM

I have a strange problem while using Sterling  for Wp7 in my app.

Each time I exit the app after saving a new record into the Database, my 1st old record is overwritten by new record value, this is in addition to new record inserted into the Database.

///this my table def 

namespace MyDBApp1.Sterling
{
    public class RolesDatabase : BaseDatabaseInstance
    {

        public override string Name
        {
            get { return "RolesDatabase"; }
        }

        protected override System.Collections.Generic.List<ITableDefinition> _RegisterTables()
        {
            return new System.Collections.Generic.List<ITableDefinition>
            {
                CreateTableDefinition<RoleViewModel,int>(k=>k.RoleID)
            };
        }
    }
}

  /// this is my class


namespace MyDBApp1
{
    public class RoleViewModel : INotifyPropertyChanged
    {
        private int _RoleID;

        public int RoleID
        {
            get { return _RoleID; }
            set
            {
                if (value != _RoleID)
                {
                    _RoleID = value;
                    NotifyPropertyChanged("RoleID");
                }
            }
        }


        private string _RoleName;
        /// <summary>
        /// Sample ViewModel property; this property is used in the view to display its value using a Binding.
        /// </summary>
        /// <returns></returns>
        public string RoleName
        {
            get
            {
                return _RoleName;
            }
            set
            {
                if (value != _RoleName)
                {
                    _RoleName = value;
                    NotifyPropertyChanged("RoleName");
                }
            }
        }

        private int _BillingRate;
        /// <summary>
        /// Sample ViewModel property; this property is used in the view to display its value using a Binding.
        /// </summary>
        /// <returns></returns>
        public int BillingRate
        {
            get
            {
                return _BillingRate;
            }
            set
            {
                if (value != _BillingRate)
                {
                    _BillingRate = value;
                    NotifyPropertyChanged("BillingRate");
                }
            }
        }


        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

 /// Code snippet to save ...

App.Database.Save(new RoleViewModel() { RoleID=i , RoleName = sRoleName, BillingRate = iBillingRate });
/// Where i, sRoleName, iBillingRate are input from a page in WP7 passed to the method as parameters
Jan 4, 2011 at 2:21 PM

i am also having a similar problem. there is a record inserted then i insert another record and press start button on device and then i resume my app again, but this time my existing record and the what i inserted last time becomes same. same values.

Coordinator
Jan 4, 2011 at 2:50 PM

Guys can you help me out - are you flushing after you insert to ensure the keys are serialized?

I want to fix this as quickly as possible. Can you take a look at this link:

http://sterling.codeplex.com/SourceControl/changeset/view/71611#1253241

One of the test methods (TestSaveShutdownReInitialize) basically inserts two records, shuts it all (emulating a stop/tombstone) and restarts it then loads the values back. Are you flushing/disposing as per the WP7 guidance? How is your case different than the test case so I can update it to reproduce the error?

Coordinator
Jan 4, 2011 at 2:59 PM

PS - updated it. I save two values, recycle the whole thing, then add a third value ... passes my test, is your case different?

Jan 4, 2011 at 3:30 PM
Edited Jan 4, 2011 at 3:31 PM

When I got that error, the problem was that I was not incrementing the key correctly.

Make sure you are using the latest source.

Also post the code you have in your 'app.xaml' and 'app.xaml.cs'.

I am using these directions and it works perfectly:

http://csharperimage.jeremylikness.com/2010/12/using-sterling-in-windows-phone-7.html

Jan 5, 2011 at 4:14 AM

let me see if I can simplify the repro steps using your sample app on the site.

1) I downloaded Sterling OODB v0.9.

2) Modified the sample project "WindowsPhoneSterlingSln" to have an app bar with a button (see below) & a 'Click' event handler

In MainPage.xaml
<shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1" Click="Button1_Click"/>

 In MainPage.xaml.cs

        private void Button1_Click(object sender, System.EventArgs e)
        {
        	// TODO: Add event handler implementation here.
			
            App.Database.Save(new ItemViewModel() {Id = 18, LineOne = "Test1 line 1", LineTwo = "Test 1 line 2", LineThree = "Test line 3" });
            App.Database.Flush();
        }

 3) Build and deploy the App in WP7 Emulator. Let the App start successfully, and then click the WP7 'back' button

4) Now on Emulator, navigate to the menu and click on the app to start again. Let the App come up then click on the button on the App bar to invoke the "Button1_Click".

5) Click the WP7 'back' button to nagivate out of the App.

6) Start the App again on the emulator. Notice the list. You should have a repro.

Note: I don't have an unlocked device. so far with this I get a consistent repro of the behavior on my WP7 emulator. I am new to sterling on WP7 and I did read the below article before I started writing my app, yes I am doing Flush(). Now I was able to repro this on the sample app posted on the sterling site. What am I doing wrong?

http://csharperimage.jeremylikness.com/2010/12/using-sterling-in-windows-phone-7.html

Jan 5, 2011 at 4:22 AM

You have to use the source version (from the "source code" tab). The release version has a known bug.

Jan 5, 2011 at 4:36 AM

hi Jeremy,

I reviewed (TestSaveShutdownReInitialize) from your last post.

In my case, I am inserting the new row during the second time I start the app in the WP7 emulator (ie) (step 1) start the app, then stop the app, (step 2) start app again and insert new row. 

Please correct me if I am wrong, this is indeed different from the test case (TestSaveShutdownReInitialize), correct?

Thanks

-G

Jan 5, 2011 at 5:09 AM

Thanks guys.

Just confirming - I used sterling-71636. I can no longer repro the issue.

-G

Jan 5, 2011 at 7:53 AM

i checked you testcase and i found i am missing

 _databaseInstance.Flush(); 

after Save Method call. Is this necessary to call Flush after Each Save and Delete?
Coordinator
Jan 5, 2011 at 10:07 AM

It is recommended. If you follow the guidance in the post, the flush will happen when the engine is disposed so technically you'll be fine. Flush simply serializes keys and indexes to disk. If you are doing a bulk of inserts/updates/deletes, it is suggested you wait until after the bulk operation is completed, but then flush as soon as possible so if the application crashes or goes away, the indexes are there when it restarts.

Sounds like the issue is no longer occurring, however, with the latest source download per king_ganesh post.