Click here to Skip to main content
6,935,055 members and growing! (17,105 online)
Email Password   helpLost your password?
 
Platforms, Frameworks & Libraries » Mobile Development » Database     Beginner License: The Code Project Open License (CPOL)

How to Save and Update the Data in Windows Mobile using Emulator and Retrieve them Back

By sun1programmer

This article shows you how you can get your saved data once you closed the Windows mobile emulator.
VB, WinMobile (PocketPC-2002), SQL-Server (SQL-CE), Dev
Revision:5 (See All)
Posted:6 Jan 2009
Views:10,063
Bookmarked:14 times
printPrint Friendly   add Share
      Discuss Discuss   Broken Article?Report  
6 votes for this article.
Popularity: 2.63 Rating: 3.38 out of 5

1

2
3 votes, 50.0%
3

4
3 votes, 50.0%
5

Introduction

Many people ask me how we can insert data in SQL Server Compact Edition (CE) database. Actually they had written the code and until they close the emulator, they have those modified/inserted data, but as soon as they close the emulator the data is not reflected in the CE db. They are wondering, "How can that be possible?"

Background

Let’s take a simple example so you can understand the basic concept of what happens behind the scenes:

  1. You developed software which uses Microsoft Access db file to store data and the db is deployed in the machine where you installed the software.
  2. Now you installed the software in Machine A and B.
  3. When you run the software in Machine A, you made some changes in the db using software.
  4. Now you closed your software of Machine A, now tell me are the changes made in db of A effected in machine B? Obviously not, here you are.

As compared to the above scenario, when you run your mobile application and it's open in emulator, then both the OS and the emulator are two different machines. Your CE db of OS is totally different from your emulator CE db, so whatever you changed in your emulator db is not getting reflected in your OS.

For example, when you deploy your application in Windows mobile and when you modify the data, the modified data is displayed in mobile not in the OS where you developed that application.

Using the Code

One way I know to do it is by sharing the storage card of Mobile application and pasting the modified db of CE into that shared directory and reusing it.

  1. Create one Windows mobile application named SampleMobileApplication.

img1.PNG

  1. <formulas /></formulas />Select the “SampleMobileApplication” project and press right mouse button to open property and change the Output File folder path:

img2.PNG

  1. Create one CE db Emp with two columns ID and Name.

img3.PNG

  1. Add one form with two buttons Get Employees and Add New:

img4.PNG

  1. Add the following code in the Show Data form:
Imports System.Data
Imports System.Data.SqlServerCe
Imports System.IO

Public Class frmEmp

    Private Sub btnGetEmployees_Click(ByVal sender As System.Object, _
	ByVal e As System.EventArgs) Handles btnGetEmployees.Click
        GetTableData()
    End Sub
    Private Sub GetTableData()
        Dim conn As SqlCeConnection = Nothing
        Try
            btnGetEmployees.Enabled = False
            conn = New SqlCeConnection("Data Source = _
		\Program Files\SampleMobileApplication\mydatabase.sdf;")
            conn.Open()
            Dim cmd As SqlCeCommand = conn.CreateCommand()
            Dim dAdp As SqlCeDataAdapter = New SqlCeDataAdapter()
            Dim ds As New DataSet
            cmd.CommandText = "SELECT * FROM EMP"
            dAdp.SelectCommand = cmd
            cmd.ExecuteReader()
            dAdp.Fill(ds)
            dgEmp.DataSource = ds.Tables(0)
        Catch ex As Exception
        Finally
            conn.Close()
            btnGetEmployees.Enabled = True
        End Try
    End Sub

    Private Sub btnAddNew_Click(ByVal sender As System.Object, _
		ByVal e As System.EventArgs) Handles btnAddNew.Click
        Dim frmAddNewEmp As New frmAddEmp
        frmAddNewEmp.Show()
    End Sub
End Class
  1. Add another form for adding new employee:

img5.PNG

  1. Add the following code in the new employee:
Imports System.Data
Imports System.Data.SqlServerCe
Imports System.IO

Public Class frmAddEmp
    Private Sub btnBack_Click(ByVal sender As System.Object, _
	ByVal e As System.EventArgs) Handles btnBack.Click
        Me.Close()
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, _
	ByVal e As System.EventArgs) Handles btnSave.Click
        If txtID.Text.Trim.Equals(String.Empty) Then
            MessageBox.Show("Please enter three digit employee ID.", Me.Text)
        ElseIf txtName.Text.Trim.Equals(String.Empty) Then
            MessageBox.Show("Please enter employee name.", Me.Text)
        ElseIf txtID.Text.Length > 3 Then
            MessageBox.Show("Invalid employee ID length, _
		only 3 digits are supported.", Me.Text)
        ElseIf txtName.Text.Length > 255 Then
            MessageBox.Show("Invalid employee name length, _
		only 255 character are supported.", Me.Text)
        Else
            Dim conn As SqlCeConnection = Nothing
            Try
                conn = New SqlCeConnection("Data Source = _
		\Program Files\SampleMobileApplication\mydatabase.sdf;")
                conn.Open()
                Dim cmd As SqlCeCommand = conn.CreateCommand()
                Dim dAdp As SqlCeDataAdapter = New SqlCeDataAdapter()
                Dim ds As New DataSet
                cmd.CommandText = String.Format("INSERT INTO EMP(ID, _
		[NAME]) VALUES ('{0}','{1}')", txtID.Text, txtName.Text)
                dAdp.InsertCommand = cmd
                cmd.ExecuteNonQuery()
                MessageBox.Show("Saved Successfully.", Me.Text)
                txtID.Text = String.Empty
                txtName.Text = String.Empty
                txtID.Focus()
            Catch ex As Exception
                MessageBox.Show(ex.Message, Me.Text)
            Finally
                conn.Close()
            End Try
        End If

    End Sub

End Class
  1. Run the application and click File -> Configure.

img6.PNG

  1. Define the Shared folder in Emulator Property dialog box as shown below:

img7.PNG

  1. Now start the “SampleMobileApplication” and click the Get Employees button.

img8.PNG

  1. Now click the Add New button and enter the information and click the save button.

img9.PNG

  1. Now click the Back button then click the Get Employees button.

img10.PNG

  1. You will see that “Albert” is added and displayed in the grid. Now close the application by clicking close button of Form.

img11.PNG

  1. Now by using Up and Down arrow select myDatabase and copy the db by clicking the menu button of Pocket PC as shown below:

img12.PNG

  1. Now open the storage card and paste the db file.

img13.PNG

  1. Refresh the Storage card by going one directory up and back in storage card to see myDatabase file.
  1. Now close the emulator and open myDatabase from E:\Test or whatever directory path you specified for storage card and you see that the new entry is reflected in the Emp table.
  1. Run the application again and you found that the new entry is not reflected. Close the emulator again and now copy that DB from your E:\Test folder and paste it in under the SampleMobileApplication directory and now run the application again and you find that the entry is reflected.

History

  • 6th January, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

sun1programmer


Member
Work in Windows Forms, ASP.NET 2.0, Portal Managements using DotNetNuke 4x., Rainbow, Windows Mobile Apllication. Having good knowledge of OOPs. Leading the team and using the cutting edge technologies for designing core components of application.

Having MCPD in Web Developement, MCITP Database Developer, MCTS in .NET 2.0: Web Application and SQL Server 2005.

Having good knowledge of Crystal Report 8.5 onwards.
Occupation: Team Leader
Company: Encodex Technologies(I) Pvt. Ltd. Pune
Location: India India

Other popular Mobile Development articles:

  • Writing Your Own GPS Applications: Part 2
    In part two of the series, the author of "GPS.NET" teaches developers how to write GPS applications suitable for the real world by mastering GPS precision concepts. Source code includes a working NMEA interpreter and sample high-precision application in C# and VB.NET.
  • Writing Your Own GPS Applications: Part I
    What is it that GPS applications need to be good enough to use for in-car navigation? Also, how does the process of interpreting GPS data actually work? In this three-part series, I will cover both topics and give you the skills you need to write a commercial-grade GPS application.
  • Learn How to Find GPS Location on Any SmartPhone, and Then Make it Relevant
    A step by step tutorial for getting GPS from any SmartPhone, even without GPS built in, and then making location useful.
  • Windows Mobile, iPhone, Android - Marketplace Comparison
    Detailed comparison between Windows Mobile Marketplace, Apple's iPhone AppStore and Android Market from developer point of view.
  • iPhone UI in Windows Mobile
    It's an interface that works with transparency effects. As a sample I used an interface just like the iPhone one. In this tutorial I am explaining how simple is working with transparency on Windows Mobile.
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
GeneralNice Article. but one more Question!! PinmemberVirani19:36 20 Jan '10  
GeneralRe: Nice Article. but one more Question!! Pinmembersun1programmer2:51 21 Jan '10  
Generalproblem feeding data into database Pinmemberchristy420075:47 1 Mar '09  
although it shows a success message on the emulator... but when we look into the database theres no data.
pls help
Generalgreat article Pinmembermicheal_safian7:27 2 Feb '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+PgUp/PgDown to switch pages.

PermaLink | Privacy | Terms of Use
Last Updated: 6 Jan 2009
Editor: Deeksha Shenoy
Copyright 2009 by sun1programmer
Everything else Copyright © CodeProject, 1999-2010
Web17 | Advertise on the Code Project