Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Querying and updating the database

From: Windows Phone SDK Essential Training

Video: Querying and updating the database

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.

Querying and updating the database

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.

Show transcript

This video is part of

Image for Windows Phone SDK Essential Training
Windows Phone SDK Essential Training

65 video lessons · 3309 viewers

Michael Lehman
Author

 
Expand all | Collapse all
  1. 2m 34s
    1. Welcome
      55s
    2. What you need to know to take this course
      51s
    3. Using the exercise files
      48s
  2. 15m 44s
    1. Downloading and installing the Windows Phone tools
      2m 44s
    2. Creating a Hello World application
      9m 31s
    3. Debugging on a Windows Phone device
      3m 29s
  3. 6m 29s
    1. Exploring device capabilities
      2m 58s
    2. Understanding the Emulator
      3m 31s
  4. 8m 51s
    1. Understanding the SDK
      8m 51s
  5. 41m 32s
    1. Introducing the converter
      3m 36s
    2. Creating the user experience (UX)
      13m 35s
    3. Responding to the Application Bar
      2m 40s
    4. Implementing click handlers
      3m 52s
    5. Saving and loading settings
      8m 18s
    6. Preparing your app for shipment
      9m 31s
  6. 20m 48s
    1. Introducing sonnets
      1m 38s
    2. Data binding with Silverlight
      4m 4s
    3. Exploring the Model-View-ViewModel pattern (MVVM)
      4m 2s
    4. Implementing the Master-Detail pattern using pages
      3m 53s
    5. Loading external data
      7m 11s
  7. 50m 26s
    1. Choosing a multipage controller
      3m 13s
    2. Preparing for persistence
      7m 30s
    3. Updating the data model
      7m 2s
    4. Creating the details page
      4m 30s
    5. Bringing it all together
      3m 58s
    6. Cloning a Windows Phone app
      6m 49s
    7. Using SQL CE
      8m 49s
    8. Updating the data model
      2m 24s
    9. Querying and updating the database
      6m 11s
  8. 14m 19s
    1. Working with the camera
      6m 11s
    2. Exploring GPS
      5m 21s
    3. Exploring the accelerometer
      2m 47s
  9. 29m 34s
    1. Introducing recording and playback with XNA
      45s
    2. Capturing sound
      5m 12s
    3. Providing feedback while recording
      5m 50s
    4. Playing a recording
      4m 34s
    5. Persisting a recording
      2m 58s
    6. Listing recordings
      5m 15s
    7. Managing the recording list
      3m 26s
    8. Recording under the lock screen
      1m 34s
  10. 28m 32s
    1. Building the UX
      3m 54s
    2. Using WebClient
      4m 1s
    3. Determining network connectivity
      2m 9s
    4. Leveraging Internet Explorer
      7m 56s
    5. Adding a live tile
      2m 42s
    6. Building a background agent
      7m 50s
  11. 14m 46s
    1. Hello World in C++
      4m 35s
    2. Libraries
      4m 17s
    3. Managed plus managed
      5m 54s
  12. 26m 35s
    1. Exploring built-in controls
      4m 28s
    2. Windows Phone Toolkit
      2m 42s
    3. Introducing Coding4Fun
      3m 13s
    4. Reminders and notifications
      6m 10s
    5. Wallet and In-App Purchase
      1m 34s
    6. File and protocol associations
      1m 59s
    7. Bluetooth
      44s
    8. Integrating speech
      1m 28s
    9. Near field communication (NFC)
      1m 23s
    10. WinRT and legacy Windows Phone APIs
      2m 54s
  13. 12m 14s
    1. Publishing your apps
      2m 49s
    2. Learning from apps in the Windows Phone Marketplace
      1m 22s
    3. Staying in touch with updates and new information from Microsoft
      2m 34s
    4. Looking at Windows Phone 7 app reviews
      44s
    5. Exploring web sites books and other useful links
      4m 45s
  14. 1m 34s
    1. Farewell
      1m 34s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Windows Phone SDK Essential Training.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.