Book Read Free

Pocket PC Magazine, November '03

Page 15

by MS Reader version $4. 95


  Hey, I don't have a Pocket PC! Can I still develop and run my apps?

  Visual Studio.NET 2003 comes with a Pocket PC emulator. So even if you don't own a Pocket PC-based device, you can start coding right on your PC! I'm afraid it doesn't get easier than that—I've done major development on other PDA platforms and it was tough!

  Creating our application

  From within Visual Studio.NET we select the File Menu > New > Project and select our Smart Device Application (Fig. 3).

  Fig. 3. VS.NET New Project window allows you to select a Smart Device Application to build.

  We have called our application "SoccerManager."

  I have chosen a VB.NET Smart Device application here, where as we could have as easily chosen a C#.NET flavor as well. These are currently the only two .NET languages supported by the Compact Framework.

  From the Smart Device Application Wizard Screen select:

  "Pocket PC" as our target platform

  A Windows Application to target.

  We also have to ability to create other types of Pocket PC (or Windows CE) based applications.

  Our starting point is now created and we've set our form's background to light blue (Fig. 4).

  Fig. 4. Our Smart Device Application Starting Point, with a blue form.

  Using the visual form designer is very similar to designing layouts for other GUI applications. It enables you to lay out forms quickly. This is sensational for prototyping Pocket PC applications.

  One main consideration is the screen size. From my travels I've found that as developers we try to put all the controls we can on our screens. Why? Because we can. But from a usability point of view, since we may be looking at our Pocket PC quickly we want the information to stick out like a sore thumb. So don't be afraid of going big and using a few more screens than you usually would for a desktop application.

  Setting a reference to SQLCE

  We need to tell Visual Studio.NET that we want to use the SQLCE system libraries. We do this by creating a reference:

  In the Solution Explorer window select References.

  Right-click and select Add Reference...

  From the available .NET available references, select System.Data. SQLServerCE.

  Press OK.

  The References window can be seen in Figure 5.

  Fig. 5. Selecting a SQLCE reference for our Pocket PC application.

  When we've created this reference, VS.NET is smart enough to know to deploy the SQLCE system libraries' CAB file along with our application.

  A Note on Compiling Our Application

  When you're ready for the big test and all the debugging has been done (or at least so you think), then don't forget to select Release Build from the Configuration Manager under the Build Menu. This will remove the debug code in your application; hence it will run more quickly.

  Also—a note to VB.NET developers—something I notice from my students quite a bit. To speed up your code, make sure that you Enable Optimizations from your project properties (Select your project in Solution Explorer, right mouse click and select Enable Optimizations at Properties > Configuration Properties > Optimizations). This also applies C# projects.

  Building a simple UI

  After adding a few controls from the toolbox and an image to the main form, we now have a basis for starting our data access routines. Our first and fantastic form can be seen in Figure 6.

  Fig. 6 (above). Our fantastic Soccer Manager starting page, as seen in the Pocket PC emulator.

  I've added most of the code needed in our application at this point, and the code I have written is designed so that you can easily take it away and use it in your own applications, particularly around the data accessing area. Basically, all database requests go through the module dbSys.vb.

  This module keeps track of the SQLCE connection and it knows how to create commands, data readers, and so on specifically for SQLCE. All the other modules call this module at some point in time.

  Onward and forward to discuss the code!

  SQLCE database security

  The first thought I had about SQLCE databases on the Pocket PC was—security. SQLCE supports 128-bit encryption on the device, which is great, but security also depends on what type of information you are storing here.

  How sensitive is the information you are storing? You may determine that the information that the application deals with is far too important and therefore should not leave the secure LAN environment (e.g., some countries treat medical information this way), whereas other information is totally open (e.g. Mom's famous recipes...actually, I wonder about that one, too).

  SQLCE database access

  Time to make our Soccer Manager come to life by giving it the data it needs. Before we go there, spare a thought for the device. It has limited CPU power and limited RAM (we can add extra, I agree, but in essence it's limited). As do all good developers, we usually get data from databases by using "SELECT * FROM ...". Why do we use '*'? Because it's the only field name we can remember!

  A quick pointer: On a constrained device, ask only for the data you need. If you use only 10 records from a table that holds 1000, ask for the 10. If you use only 20 bytes of a 2000 byte record, ask only for those field names that you need, not '*' (as tempting as it may be).

  So how do we get to our SQLCE database? How does ADO.NET sound? We have the SqlCeEngine, SqlCeConnection, SqlCeCommand, SqlCeDataReader, SqlCeDataAdapter, and more. It's starting to look all too easy and very similar to the SQL Server world.

  Let's see how....

  Creating the database in the first place

  Soccer Manager will check to see whether our database exists; if it doesn't exist, Soccer Manager will create it by using SQL Script. I've decided to do it this way for ease of deployment. Another way to create the database is to use isql.exe on the Pocket PC.

  Our database has 3 tables as follows:

  PrivateFunction CreateTables() As Boolean

  Dim sql As String

  Dim cmd As SqlCeCommand

  'players table

  sql = "CREATE TABLE Players(PlayerID int not null identity(1,1)," _

  + "Name nvarchar(100) not null, TeamID int not null)"

  cmd = ME.CreateCommand(sql) 'CreateMethod lives in dbSys.db

  cmd.ExecuteNonQuery()

  'teams table

  sql = "CREATE TABLE Teams(TeamID int not null identity(1,1)," _

  + "Name nvarchar(100) not null)"

  'we will re-use our command.

  cmd.CommandText = sql

  cmd.ExecuteNonQuery()

  'games table

  sql = "CREATE TABLE Games(GameID int not null identity(1,1), " _

  + "Team1ID int not null, Team2ID int not null, " _

  + "GameDate datetime not null, " _

  + "Result nvarchar(50) null)"

  cmd.CommandText = sql

  cmd.ExecuteNonQuery()

  Return (True)

  End Function

  The SQL script that creates the database tables can be seen above. Note that this script does not include creation of indexes.

  Accessing data within Soccer Manager

  We could add data by using SQL scripts if we want our database pre-populated with match/team/player data, or we can use ADO.NET to add data to our database.

  We create a connection to the database as follows:

  Const DB_CONN_STRING = "Data Source=My Documentssoccer.sdf"

  Private Shared objConn As SqlCeConnection

  Private Function CreateConnection() As SqlCeConnection

  objConn = Nothing

  objConn = New SqlCeConnection(DB_CONN_STRING)

  objConn.Open()

  Return (objConn)

  End Function

  (objConn is declared as Shared or Static in C#, since our application will need only one connection to our database.)

  As mentioned previously, we can use SqlCeCommand, SqlCeDataAdapter, SqlCeDataReaders to send SQL statements back and forth to SQLCE. Bear in mind
, ask only for the data you need. We create these as you would normally in ADO.NET as follows:

  Private Function CreateCommand(ByVal sql As String) As SqlCeCommand

  Dim cmd As New SqlCeCommand(sql, Me.Connection)

  cmd.CommandType = CommandType.Text

  Return (cmd)

  End Function

  Private Function CreateDataAdapter(ByVal sql As String) As SqlCeDataAdapter

  Dim da As New SqlCeDataAdapter(sql, Connection)

  Return (da)

  End Function

  Remember that we are able to use the objects such as SqlCeDataAdapter because of our reference to the SQLCE data library.

  To read the data we can use a SqlCeDataReader as in the following snippet of code:

  Dim dr As SqlServerCe.SqlCeDataReader

  Dim objTeams As New dbTeam

  Dim lstItem As uiHelper

  dr = objTeams.GetList()

  While dr.Read()

  ' we created a small uiHelper object to allow us to store the TeamID with the name in the combo box.

  lstItem = New uiHelper(dr("Name"), dr("TeamID"))

  cmbTeams.Items.Add(lstItem)

  End While

  Public Class uiHelper

  Public Name As String

  Public ID As String

  Public Sub New(ByVal sName As String, ByVal sID As String)

  Name = sName

  ID = sID

  End Sub

  Public Overrides Function ToString() As String

  Return (Name)

  End Function

  End Class

  This code populates a combo box with a list of teams. You may think, "Why didn't I just add the team name to the combo box and be done with it?" The reason is that when the user selects a team from the list, we will usually want the TeamID as well for later use. Rather than going back to the database, we store it alongside the name.

  All in all, the database work is not hard. The only thing to watch out for is that SQLCE currently does not support stored procedures or triggers. Bear this in mind when heading down the design path for Pocket PC applications.

  Soccer Manager in action

  At this point we have our full-fledged Soccer Manager application (minus exception handling), with the entering of results seen in Fig. 7 and the viewing of matches/results in Fig. 8. Also we can see in Fig. 9 the creation of a match schedule.

  Fig. 7 (above). Being able to save the results in Soccer Manager.

  As you've probably guessed the user interface in our application is basic and obviously is not the main focus, but remember...a picture tells a thousand words.

  The Soccer Manager sampleis available at Breeze Training Pty Ltd (www.breezetraining.com.au/ppc/downloads).

  Sharing the data with others

  Our application works great so far for doing its own thing, but what about when it comes time to share? How do we connect, and how do we replicate our matches?

  First, we need a connection to the network/Internet and this can be done in a variety of ways; e.g., docking the Pocket PC in its cradle, wireless access, infrared, or direct modem/GPRS connections.

  Second, we need some sort of synchronization. SQLCE comes with replication and synchronization technology, allowing it to synchronize with SQL Server. To enable this you must have control over the SQL Server and you must expose a virtual directory. In an environment in which your site is hosted by a third party, the required configuration isn't always possible.

  Web services spring to mind! We have a web service for posting our updates and another for retrieving data. This will come soon....

  Other uses

  Soccer Manager is a simple application for maintaining a sports league. To give you an idea of taking this further, I have developed a system that not only keeps track of the teams and games, but also evaluates the players' performance and graphs their results. This allows coaches to stand by the soccer field with Pocket PC in hand and evaluate the performance of their team and players.

  Fig. 8 (above). Viewing the upcoming games, or the ones played—all from the palm of your hand.

  These sorts of applications are very useful for a variety of sports. For example, baseball leagues could be scored this way and then the data sent centrally after the games, allowing it to be posted within moments to a Web site.

  Fig. 9 (above). Scheduling one of the Matches of the Century.

  Ciao!

  Hope you've enjoyed it, gained some new ideas, and have even more questions.

  Happy coding!

  * * *

  Mick Badran is an MCSE, MCSD and MCT. He has been performing Microsoft technical classroom-based training for over 8 years, with over 12 years' commercial development experience. Mick consults to Microsoft in areas such as .NET Mobility solutions, BizTalk Server, Content Management Server, and SharePoint Portal Server Solutions. he also creates and delivers custom courseware for Microsoft in the above areas. He can be reached at mickb@breezetraining.com.au and would love to hear your suggestions.

  Industrial Pocket PCs for Remote Data Entry

  by David Shier

  I have worked with computers for more than 20 years, starting out as a software developer for automated test equipment used in factories. The computers we used were designed for industrial use, yet were still extremely fragile by today's standards. Looking back, it's incredible to me how much more computing power a Pocket PC provides than those refrigerator-size "mini-computers" of the late 1970s and early 1980s. What is even more impressive is how rugged the "industrialized" Pocket PCs have become. The thought of a computer being dropped on a concrete floor makes one shudder at the tens of thousands of dollars lost. But industrial Pocket PCs can take abuse that was unimaginable for a computer just a few years ago.

  The one compromise we've had to make was in data input. While the Pocket PC's touch screen is great for selecting items from menus, it is less than ideal for inputting more random information such as inventory quantities, new customer names, and other alpha numeric data. Good handheld computing application design attempts to minimize such input by providing the user with a list of current customers and inventory items, but there are certain situations where numeric or text input can't be avoided.

  Fortunately for the developers of applications that require such user input in the field, there are a few new industrial Pocket PCs now available that address the problem. Here's a look at two of them.

  Symbol PDT 8000 series for numeric data entry

  Symbol has been a leader in industrial handheld computers and barcode readers for many years. With the PDT 8000 Series they've combined the two in an industrial Pocket PC that includes a 23-key keypad for numeric entry in addition to the normal touch screen.

  Fig. 1. Symbol PDT 8000

  In addition to the ten number keys, the keypad includes cursor navigation keys and three keys that can be used for custom applications.

  Other features of the Symbol PDT 8000 include a 400 MHz Intel XScale processor, 128 MB of RAM, and 64 MB of flash ROM. This large amount of memory allows complex enterprise applications to be stored in non-volatile flash memory and large databases in the built-in RAM without the need for an additional storage card. But storage should not be a problem since there is a CF slot available as well as built-in wireless capability. The PDT 8000 can be configured with a Bluetooth, Wi-Fi, or GSM/GPRS wireless adapter—all this in a handheld designed to withstand multiple drops from five feet onto concrete.

  Industrial Pocket PC with a full keyboard

  If your application requires the user to enter more than just numbers and menu selections, then you may find that the pop-up software keyboard (known as SIP, for Soft Input Panel) is a limiting factor. In this case, you will be pleased to see the new CF-P1 from Panasonic.

  Fig. 2. Panasonic CF-P1

  While you might be familiar with Panasonic as a maker of consumer electronics, the company has been producing ruggedized laptop and handheld computers under the Tough book brand for quite some time. The CF-
P1 is unique in that it includes a "thumb keyboard" with a joystick button and three application function buttons.

  The keyboard makes the CF-P1 an ideal choice for applications such as law enforcement in which random text, such as names or notes, must be entered.

  Another unique feature of the CF-P1 is the battery. While the user documentation admonishes you to use only the specified battery pack, the battery is the same as that used by many portable video camcorders. This not only allows for inexpensive replacement batteries but also external chargers. The battery is large enough to provide 8 to 24 hours of operating time, depending on the accessories used and backlight usage. Along with this lithium-ion main battery, there is an internal rechargeable lithium backup battery.

  The CF-P1 uses the older-generation 206 MHz Intel StrongARM processor and 64 MB of RAM and 32 MB of flash ROM. This means that if you want to use non-volatile storage, you will need to insert a flash card in the SD memory card slot.

 

‹ Prev