Everyone Loves a Demo

Using the Data Form Wizard to Build a Data-Bound Form

You can add a number of different types of objects to a Visual Studio .NET project, such as forms, class modules, and code modules. With Visual Basic .NET and C#, you can add a data-bound form. When you add a data-bound form to your project, Visual Studio automatically launches the Data Form Wizard.

To help you build your data-bound form, the wizard prompts you for information about the database, tables in the database, columns in the table, and so forth, in a series of pages. We'll step through the wizard to build a sample data-bound form.

First in Visual Studio .NET, choose New, Project from the File menu or click the New Project toolbar button to open the New Project dialog box. Select your language of choice in the Project Types pane—Visual Basic or Visual C#—and then select the Windows Application icon, as shown in Figure 2-1. In the text box below the icons, change the name of the project you're about to create to Chapter2, and then click OK.

note

Building a data-bound Web form involves more complex concepts, which we'll cover in Chapter 14. For simplicity, we'll focus on building a data-bound Windows form in this chapter.

Figure 2-1

Creating a new Windows application

Now that you have your new project set up, you can use the Data Form Wizard to create a new data-bound form. Because using the wizard results in a new form in your project, the wizard is available through the Add New Item dialog box, which you open by choosing Add New Item from the File menu or clicking the Add New Item toolbar button in Visual Studio .NET. Select the Data Form Wizard icon, shown in Figure 2-2, and then click OK.

Figure 2-2

Launching the Data Form Wizard from the Add New Item dialog box

The wizard will launch and display a welcome page, shown in Figure 2-3.

Figure 2-3

The Data Form Wizard's welcome page

Click Next to go to the next page, which offers you a choice of using an existing DataSet object or a new one. You haven't created a DataSet yet, so you'll create a new one, as shown in Figure 2-4.

Figure 2-4

Creating a new DataSet object for your new form

Your DataSet object's name should indicate the type of data contained in your DataSet. In this example, the DataSet will store customer information and orders tables in the Northwind database. To name the new DataSet, type CustomersAndOrders in the text box and then click Next.

note

The Data Form Wizard builds strongly typed DataSet objects, which are new class files in your project. Always be sure that the name you type for your new DataSet is a valid class name—that is, one that begins with a letter and includes only letters, numbers, and underscore characters.

Choosing a Connection

Now it's time to connect the Data Form Wizard to your database. The Choose A Data Connection page, shown in Figure 2-5, allows you to add new connections or select a connection that's available in the Server Explorer window. If you haven't already created a connection to your database, click the New Connection button and the OLE DB Data Link Properties dialog box will appear.

Figure 2-5

The wizard's Choose A Data Connection page

On the Connection tab of the Data Link Properties dialog box, you can specify your connection. By default, it is set to connect to Microsoft SQL Server. To connect to your database, specify a server name (or type (local) to communicate with an instance of SQL Server running on your machine), a username, password, and a database name, as shown in Figure 2-6.

Figure 2-6

The Connection tab of the Data Link Properties dialog box

If multiple instances of SQL Server 2000 are installed on a machine, you can indicate which instance you want to use by specifying the server name, a back slash, and then the instance name—for example, MyServerName\MyInstance.

note

The default password for the SQL Server administrator account is blank. Having a blank password for the master account is, generally speaking, a bad idea. For security reasons, you should change this password. Also, when you develop database applications for SQL Server, don't code your application to log users in with the administrator account. Create accounts for your users, or maybe one generic user account, with the appropriate permissions. You don't want your users to accidentally or intentionally make catastrophic changes to your database.

Notice that the Allow Saving Password check box is selected in the figure. By default, this check box is deselected. If you enter a password using the default settings, Visual Studio .NET will receive the entire connection string except for the password. As a result, you'll be prompted for the password at various times while you access the database at design time. I prefer to select this check box to remove the password from the connection string in my code. Select the check box for now. You'll learn more about this feature in Chapter 3.

If you want to connect to a database other than SQL Server, click on the Provider tab to select a different OLE DB provider. As shown in Figure 2-7, this tab lists all the OLE DB providers installed on your machine. I'll discuss using other providers with the Data Link Properties dialog box in more depth in the next chapter.

Figure 2-7

The Provider tab of the Data Link Properties dialog box

If you don't have access to a SQL Server or MSDE database but do have an Access version of the Northwind database, select the Microsoft Jet 4.0 OLE DB provider.

Click Next to move to the Connection tab of the dialog box, as shown in Figure 2-8.

Figure 2-8

Specifying an Access database on the Connection tab of the Data Link Properties dialog box

Type the path to your Access database in the first text box, or click the ellipsis button to the right of the text box to open a dialog box in which you can select the database from your hard drive or network. Type a username and a password in the appropriate text boxes, and select the password check boxes (if desired) for the connection. Click OK.

Selecting Tables from Your Database

The next Data Form Wizard page, shown in Figure 2-9, lists the tables, views, and stored procedures that are available in your database's schema. Although tables and views are treated as different structures in different database systems, the output from either type of object is mapped to an ADO.NET DataTable. Thus, the wizard refers to all the objects as tables.

Select the tables you want to include in your DataSet in the Available Items list and move them to the Selected Items list by clicking the right arrow button. If you make a mistake and need to remove a table from the Selected Items list, select the table to remove and click the left arrow button. You can also select and deselect tables by double-clicking them.

Figure 2-9

Selecting tables in the Data Form Wizard

For the purposes of our sample application, select the Customers and Orders tables from the Northwind database. Then click Next.

Creating Relationships in Your DataSet

If you've selected more than one table, the Data Form Wizard will display a page in which you can create relationships between the tables you selected. As you learned in Chapter 1, you can use relationships to easily locate data in related tables. Relationships also help enforce referential integrity rules by cascading changes from one table to another.

Adding a relationship to your DataSet using the wizard is simple. The most challenging part of the process might be naming the relationship. As a general guideline, combine the name of the parent and child tables (in that order) to create the name.

Here we'll relate the Customers and Orders tables you selected on the previous page. Name the relationship CustomersOrders.

The data in the two tables are related. Each entry in the Orders table relates to an entry in the Customers table. In other words, each order belongs to a particular customer. Because each customer row has order rows associated with it, the Customers table is the parent table in the relationship.

Select the Customers table as the Parent Table in the relationship and the Orders table as the Child Table in the relationship. The CustomerID defines the relationship between the two tables. Select CustomerID as the key field for each table, as shown in Figure 2-10. Click the right arrow button to add the relationship to the Relations list, and then click Next.

Figure 2-10

Creating relationships between tables

Selecting Columns to Display

On the next wizard page (shown in Figure 2-11), you can select the columns to display on the data-bound form. When you selected the tables to include in your DataSet, you didn't have the option to specify which columns to include. The Data Form Wizard retrieved all rows and all columns in the selected tables.

Figure 2-11

Choosing the tables and columns to display on your data-bound form

You can display a single table or two tables in a master/detail relationship. Once you select the table or tables to display, you'll see all the available columns for the selected tables. By default, all the columns are selected, but you can deselect any columns you don't want to have appear on the data-bound form.

When this page of the wizard appears, you'll see that the Customers table is already selected as the parent table and the Orders table is selected as the child table. This behavior is a pleasant result of the wizard's defaults. The wizard automatically selects the first table by alphabetical order as the parent table. Because "Customers" comes before "Orders," the Customers table is selected by default. The Customers table has only one related table, Orders, so Orders is selected by default as the child table.

To simplify the display of your form, select just the CustomerID, CompanyName, ContactTitle, and Phone fields from the Customers table and the OrderID, CustomerID, EmployeeID, and OrderDate fields from the Orders table. Then click Next.

Choosing a Display Style

The next wizard page provides a couple of options for showing the contents of the main table on data-bound Windows forms. You can display contents of the main table in a grid, which will allow the user to see multiple rows at the same time, or you can display the rows one at a time in a series of bound controls such as text boxes.

You'll have more options if you choose to display the contents of the main table one row at a time. The wizard lets you decide whether you want to include buttons that allow the user to navigate back and forth through the contents of the table, cancel pending changes on a row, or add and remove rows. For the purpose of this exercise, select the Single Record In Individual Controls option and all the check boxes that follow as shown in Figure 2-12.

Figure 2-12

Selecting the Single Record In Individual Controls display style

note

If you were building a Web application, the wizard would create DataGrid objects, which would convert the data in your tables to HTML tables to display in the browser on your Web form.

That's it. Click Finish to build your new data-bound form.



Microsoft ADO. NET Core Reference
Microsoft ADO.NET (Core Reference) (PRO-Developer)
ISBN: 0735614237
EAN: 2147483647
Year: 2002
Pages: 104
Authors: David Sceppa

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net