Redink
Redink

Reputation: 344

windows phone 8 edit/update local database

WP8, VS12, & C#

I've started creating an app which allows me to store relational data in a local database using LINQ to SQL.

What I'd like to do next is be able to update the existing data...I would click an icon in the appbar and be taken to the previously saved data so I could update it.

I've looked on msdn... Local database for Windows Phone, and I would like to know if the following code I see at section Using the database > Updating data is valid given my LINQ to SQL set-up. If so, how do I go about adding this code to allow updating?

    protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
    { 

    //Call base method
    base.OnNavigatedFrom(e);

    //Save changes to the database
    toDoDB.SubmitChanges();
    }

If anyone could point to a working example or help me hook up the ability to update data, I would be grateful.

Much Thanks,

k

Upvotes: 0

Views: 2028

Answers (3)

Kn0rK3
Kn0rK3

Reputation: 73

The important thing in updating the data in the database is, that you handle with the same object which results the query from database and don't copy it. You can pass the object from the query results to other objects or functions but make sure that it is still the same object on the heap. Just in this case the SubmitChanges works. I did it the first time wrong and copied it for the page object and then passed it back and the SubmitChanges didn't work.

Upvotes: 1

KoJitsu
KoJitsu

Reputation: 38

Yhello, dunno if you still working on your project but I've got a solution for you.

From your site (msdn), here what I found :

First, query the database for the object that is to be updated. Then, modify the object as desired. Finally, call the SubmitChanges method to save the changes to the local database.

So, you need to query your db (example from my own VB.net code)

Dim monContact = From contact As Authentification In bddGLI.TableAuth Select contact

Execute the query and get the result in a collection

Dim resultCollection = New ObservableCollection(Of Authentification)(monContact)

Run through this collection with a ForEach loop and modify your object

For Each elem As Authentification In resultCollection
                elem.Mail = txtEmail.Text
                elem.Nom = txtNom.Text
                elem.Prenom = txtPrenom.Text
            Next

And don't forget to save your db

bddGLI.SubmitChanges()

Now, how to check if you really updated your data ? Where I create my db, i inserted some data test in my table

Using db As New GeoLiveInfoDataContext(GeoLiveInfoDataContext.DBConnectionString)
            If db.DatabaseExists() = False Then
                db.CreateDatabase()
                Dim contact As New Authentification
                With contact
                    .Nom = ""
                    .Prenom = ""
                    .Mail = ""
                End With
                db.TableAuth.InsertOnSubmit(contact)                   
                db.SubmitChanges()
            End If
        End Using

http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh286408(v=vs.105).aspx
Go to C:\Program Files (x86)\Microsoft SDKs\Windows
Phone\v8.0\Tools\IsolatedStorageExplorerTool
SHIFT + right clic => open prompt here
ISETool.exe ts xd {ID APP HERE FROM MANIFEST } {PATH}
Don't forget to install your app on the emulator or device (not debugging)

Now at your path, you have a .sdf which can be open with SQL server Compact Edition.

Do this command before and after updating and check the difference.

Upvotes: 1

Daniel W
Daniel W

Reputation: 73

Use Sqlight -- Here is a great example http://code.msdn.microsoft.com/wpapps/Using-Sqlite-with-WP8-52c3c671

Upvotes: 1

Related Questions