Export (0) Print
Expand All

Walkthrough: A Database Master-Detail Application 

This topic has been updated for Visual Studio 2005 SP1.

This walkthrough shows you how to use the Visual Studio 2005 environment to connect to a database, select database objects for inclusion in a project, and create data-bound controls to display the data in a smart device application.

NoteNote

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.

This walkthrough was written using Visual Basic Development Settings and Visual C# Development Settings.

Northwind database for SQL Server Mobile Edition (included in Visual Studio 2005).

Northwind database for Microsoft SQL Server 2005 Compact Edition (included when you install SQL Server Compact Edition for use with Visual Studio 2005 SP1).

NoteNote

If you are not an Administrator on your development computer, you cannot open the Northwind.sdf file in its default location (\Program Files\Microsoft Visual Studio 8\SmartDevices\SDK\SQL Server\Mobile\v3.0). Copy the file to the desktop or to My Documents and open it from there when you are prompted.

The following procedure has been updated for Visual Studio 2005 SP1.

To ensure that you are prompted to select a device when you deploy your solution, complete the following procedure.

To prompt for device choices at deployment time

  1. On the Tools menu, click Options, click Device Tools, and then click General.

  2. Select the Show device choices before deploying a device project check box.

This is a simple Windows Forms application to host the data functionality of this walkthrough.

To create a Windows Forms device project

  1. (Visual Basic) On the File menu in Visual Studio 2005, click New Project.

    —or—

    (Visual C#) On the File menu in Visual Studio 2005, point to New, and then click Project.

  2. Under Project Types in the New Project dialog box, expand Visual Basic or Visual C#, expand Smart Device, and then click Pocket PC 2003.

    If the language you want does not at first appear, expand Other Languages. This display is governed by your development settings.

  3. Under Templates, click Device Application.

    NoteNote

    Do not select Device Application (1.0), which relies on version 1 of the .NET Compact Framework and is not suitable for this walkthrough.

  4. In the Name box, type MasterDetailSample.

  5. (Visual C# only) In the Location box, verify where you want to store your project files.

  6. Click OK.

    A representation of a Pocket PC device appears in the Windows Forms Designer.

This section consists of the following tasks:

  • Selecting a data source type

  • Selecting and configuring a data connection

  • Selecting database objects

  • Adding data-bound controls to the form

To select a data source type

  1. On the Data menu, click Add New Data Source to open the Data Source Configuration Wizard.

  2. On the Choose a Data Source Type page, select Database, and then click Next.

To select and configure a data connection

  1. On the Choose Your Data Connection page, click New Connection.

  2. In the Choose Data Source dialog box, select Microsoft SQL Server Mobile Edition, and then click Continue.

    —or—

    In the Choose Data Source dialog box, select Microsoft SQL Server Compact Edition, and then click Continue.

    NoteNote

    Depending on settings and previous projects, the Add Connection dialog box might appear instead of the Choose Data Source dialog box. If this happens, click Change in the Add Connection dialog box to open the Change Data Source dialog box. Then select Microsoft SQL Server Mobile Edition or Microsoft SQL Server Compact Edition and click OK.

  3. In the Add Connection dialog box, select My Computer.

  4. Again in the Add Connection dialog box, click Browse.

  5. In the Select SQL Server Mobile Edition Database File dialog box, select Northwind.sdf, and then click Open.

    —or—

    In the Select SQL Server Compact Edition Database File dialog box, select Northwind.sdf, and then click Open.

  6. In the Add Connection dialog box, leave the Password box empty.

    This database has no password.

  7. Click Test Connection to verify the connection.

    NoteNote

    If access to the Northwind.sdf file is denied, copy the file to the desktop, and browse to that copy to open. This situation can occur if you do not have sufficient rights on the development computer to open the file in its default location, which is listed at the beginning of this walkthrough.

  8. Click OK on the message box that shows the connection succeeded, and then click OK to close the Add Connection dialog box.

  9. Close the Choose Your Data Connection page by clicking Next.

  10. In the message box that asks whether you want to copy the file to your project, click Yes.

To select database objects

  1. On the Choose Your Database Objects page, expand the Tables node, and then select the Customers and Orders tables.

  2. Click Finish.

    The NorthwindDataset is created. You can view this data source by selecting Show Data Sources on the Data menu.

To add data-bound controls to the form

  1. In the Data Sources window, select the Customers table, click the drop-down arrow, and then select the DataGrid option.

  2. Drag the Customers table from the Data Sources window onto the form in the designer.

    Locate the grid toward the top of the window.

  3. In the Data Sources window, expand the Customers table to expose the Orders table.

    NoteNote

    This is the Orders table as it appears within the Customers table, not the Orders table that is at the same tree level as the Customers table.

  4. Click the drop-down arrow for this Orders table, and select the DataGrid option.

  5. Drag this Orders table from the Data Sources window onto the form in the designer.

    Locate the grid toward the bottom of the window.

In this section you build the application, download it to the Pocket PC 2003 SE emulator, and verify that the application works correctly.

To test the application

  1. On the Debug menu, click Start or Start Debugging.

  2. In the Deploy dialog box, select Pocket PC 2000 SE Emulator, and then click Deploy.

    Deployment progress appears in the Status bar. Deployment to the emulator can take some time.

  3. When the application is running on the emulator, use the up and down arrows on your keyboard or the NAVIGATION control on the emulator to change the selected records in the Customers grid. Verify that the selected records change in the Orders grid.

If you plan to do additional walkthroughs or open other projects, you want to shut down the emulator completely and exit this solution.

To close the emulator and the solution

  1. On the File menu of the emulator, click Exit.

  2. In the Device Emulator message box, click No to the message asking if you want to save the emulator state.

  3. In the message box that advises the connection has been lost, click OK.

  4. (Visual Basic) On the File menu, click Close Project.

    If you are prompted to save the project or solution, click Save if you want to use it again later; otherwise, click Discard and your files will not be saved.

    —or—

    (Visual C#) On the File menu, click Close Solution.

Community Additions

ADD
Show:
© 2014 Microsoft