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.
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.
The Northwind database for SQL Server Compact 3.5, which is included in Visual Studio.
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.
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
On the Tools menu, click Options, expand Device Tools, and then click General.
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
(Visual Basic) On the File menu in Visual Studio, click New Project.
(Visual C#) On the File menu in Visual Studio, point to New, and then click Project.
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.
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.
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
On the Data menu, click Add New Data Source to open the Data Source Configuration Wizard.
On the Choose a Data Source Type page, click Database, and then click Next.
To select and configure a data connection
On the Choose Your Data Connection page, click New Connection.
In the Choose Data Source dialog box, select Microsoft SQL Server Compact 3.5, and then click Continue.
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.
In the Add Connection dialog box, select My Computer.
In the Add Connection dialog box, click Browse.
In the Select SQL Server Compact 3.5 Database File dialog box, click Northwind.sdf, and then click Open.
In the Add Connection dialog box, leave the Password box empty.
This database has no password.
Use a strong password for databases that are used in real-world applications.
Click Test Connection to verify the connection.
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.
Click OK on the message box that shows the connection succeeded, and then click OK to close the Add Connection dialog box.
On the Choose Your Data Connection page, click Next.
In the message box that asks whether you want to copy the file to your project, click Yes.
To select database objects
On the Choose Your Database Objects page, expand Tables, and then select the Customers and Orders tables.
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
In the Data Sources window, click the Customers table, click the drop-down arrow, and then select the DataGrid option.
Drag the Customers table from the Data Sources window onto the form in the designer.
Locate the grid toward the top of the window.
In the Data Sources window, expand the Customers table to expose the Orders table.
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.
Click the drop-down arrow for this Orders table, and select the DataGrid option.
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
On the Debug menu, click Start or Start Debugging.
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.
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.
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
On the File menu of the emulator, click Exit.
In the Device Emulator message box, click No to the message asking if you want to save the emulator state.
In the message box that advises the connection has been lost, click OK.
(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.
(Visual C#) On the File menu, click Close Solution.