Walkthrough: A Master-Detail Application

This walkthrough shows you how to use the Visual Studio integrated development environment (IDE) 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.

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Visual Studio Settings.

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

Prerequisites

The Northwind database for SQL Server Compact 3.5, which is included in Visual Studio.

Note

If you are not an Administrator on your development computer, you cannot open the Northwind.sdf file in its default location (drive:\Program Files\Microsoft SQL Server Compact Edition\v3.5\Samples). Copy the file to the desktop or to My Documents and open it from there when you are prompted.

Choosing a Target Device

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, expand Device Tools, and then click General.

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

Creating the Application

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, click New Project.

    —or—

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

  2. In the Project Types pane, expand Visual Basic or Visual C#, click Smart Device, click Smart Device Project in the Templates pane, type MasterDetailSample in the Name box, and then click OK.

  3. In the Add New Smart Device Project dialog box, select Pocket PC 2003 from the Target platform drop-down, click Device Application, and then click OK.

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

Adding Data Functionality

This section consists of the following tasks:

  • Selecting a type of data source

  • Selecting and configuring a data connection

  • Selecting database objects

  • Adding data-bound controls to the form

To select a type of data source

  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, click 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 Compact 3.5, and then click Continue.

    Note

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

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

  4. In the Add Connection dialog box, click Browse.

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

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

    This database has no password.

    Security noteSecurity Note:

    Use a strong password for databases that are used in real-world applications.

  7. Click Test Connection to verify the connection.

    Note

    If access to the Northwind.sdf file is denied, copy the file to the desktop, and browse to that copy to open it. 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 start 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. On the Choose Your Data Connection page, click 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 Tables, 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, click 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.

    Note

    This is the Orders table as it appears in 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.

Testing the Application

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 the 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.

Preparing for Additional Walkthroughs

If you plan to do additional walkthroughs or open other projects, you want to shut down the emulator 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.

See Also

Reference

Data Source Configuration Wizard

Other Resources

Data Walkthroughs

Accessing Data (Visual Studio)