Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
We've got two last things to do before we fire this up and see it running against the database. First we're going to update the DetailsPage, then we're going to update the icon, and we're going to give it a try. So let's go over to the Solution Explorer. I'll open up the CS code-behind file for the DetailsPage. XAML doesn't change any between the flat file version and the database version of sonnets plus. So we'll put in the new version and take a look and see what we changed.
First we add a reference to System.Linq because we need to be able to do it LinkQuery and then OnNavigatedTo method it's still the same as it was before. But our toggleFavorite_Click handler is going to be different, because here's where we use things differently between the XML and the database. So first, when the user taps on the Toggle button, we need to be able to go find the sonnet in the database because that's what we want to update. So we do that with a little bit more sophisticated query than we did before.
Because now we don't want to get all the sonnets in the database, we only want to get the sonnet that matches the one we're actually looking at. So again, pretty standard link to SQL, from Sonnets matchingSonnet in db.Items, now we have a where clause, where matchingSonnet.SonnetId is equal to ourItem.SonnetId, and we select it. Now because we're doing an exact query here, and we know that SonnetId is a system-generated identity key, we know that there is only one possible answer, so we call ourRow.First to get access to the sonnet.
And just to prove to ourselves that we retrieved the right item from the database, we'll put in this Debug here to make sure that the SonnetId we retrieved is the one we're expecting. Same logic as we had before about the toggling except that we now want to toggle not only our item, which is ItemViewModel instance which is related to what's on the screen. We also want to toggle IsFavorite property of the Sonnet object which is what's in the database. So all we have to do is toggle that properly and call db.SubmitChanges, and that changes the database. We didn't have to do anything other than an initial query, change the property and submit changes, and that writes it back.
And of course the rest of this is the same as before, meaning we update the AddItemToFavorites or RemoveItemFromFavorites in order to get our favorites list in the second part of our pivot to update. And because we did this db.SubmitChanges here, we no longer have to go and call the MainViewModel and tell it to update the database, a.k.a. write the XML file back out. That's the end of the code changes. Now let's do the really hard work. Open up that icon and set this one to a little lighter green, so I have three different versions of this icon, one for each version of this app that we built.
That's the ApplicatioIcon, here's the Background, and pick the same light green, our favorite paint bucket, boom, boom and save that, don't forget now to do a Rebuild Solution. Since we changed the icons we need to make sure that you've done, and let's give it a try. Before we do that, let's make sure that we don't already have an instance of that app, which we do. Let's get rid of that, because we want to actually see the process of creating the database, and now let's run it.
And we can see here in our Output window we didn't find the database, so we initialized it from the XML, which means that we're going to have all of our Sonnets, but we won't have any favorites, and let's come down here and pick a Sonnet. Once again, the beautiful summers day here, and when we tap it we've now updated the database. So what happens is we printed out this line, Toggle Favorite for :XVIII, and it shows that we actually did retrieve item 18 from the database and updated it.
And we can tell that because we come back here, and it's in our favorites list, and it has our gold star on it, meaning we like this Sonnet, and that's it. So it was just a quick review, we cloned our application, we went through all the steps of doing that. We built a database object, our Sonnets.cs in which we added all the attributes here, we decorated this object in order to make it a database table. And we build our SonnetsDBDataContext object which has a constructor for connecting us to the database and an items instance variable telling the database what kind of information the database is going to store.
We also updated our MainViewModel, to go and retrieve the data from the database here, to get our items to display in our list boxes, and then finally we updated our DetailsPage.xaml in our toggle method down here to query the individual item that we wanted the individual Sonnet from the database and updated and called db.SubmitChanges to write it back. Well, this was a non-trivial process to convert from flat file to database.
You can see that the process of using the database on Windows phone devices is very easy and very quick to implement using Linq to SQL and the SQLCE features that are there. And finally, let's go back to our Emulator, and let's pin our application to the Start menu, and now we can see how far we've come since we started the course. We built our Units Converter and three different versions our Sonnets application, Illustrating Pivots Isolated Storage or Flat Files and Isolated Storage for the database.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101391 Viewers
61 Video lessons · 88160 Viewers
71 Video lessons · 71989 Viewers
56 Video lessons · 103809 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Your file was successfully uploaded.