Using the Data Form Wizard to Create a Windows Form

In this section, you'll use the VS .NET Data Form Wizard to create a Windows application that accesses both the Customers and Orders tables. The Orders table contains rows that represent orders placed by the customers.

The rows in the Orders table are related to the rows in the Customers table through a foreign key: The Orders table contains a column named CustomerID that is a foreign key to the CustomerID column of the Customers table (CustomerID is the primary key for the Customers table). The use of the foreign key defines a parent-child relationship between the Customers and Orders tables.

The form you'll create will display a row from the Customers table, along with any related rows from the Orders table. To give you a clear idea of the final goal, Figure 6.17 shows the completed form up and running. Notice that the top part of the form shows the details for the row from the Customers table where the CustomerID is ALFKI; the bottom part of the form contains a DataGrid control that displays the rows from the Orders table for that customer. When you move to the next row in the Customers table, the rows from the Orders table for that customer are automatically displayed in the DataGrid.

click to expand
Figure 6.17: The running form

Perform these steps to begin build the form:

  1. Select File New Project.

  2. In the New Project dialog box, select Empty Project, and enter DataFormWindowsApplication in the Name field. Because you'll be adding a new form to your new application shortly, there's no need to have VS .NET generate the usual blank form for you; that's why you're creating an empty project.

  3. Click OK to continue. VS .NET will create a new empty project for you.

Next, you'll use the Data Form Wizard to create a form that accesses the Customers and Orders tables in the Northwind database:

  1. Select Project Add New Item.

  2. Select Data Form Wizard from the Templates section on the right, enter the Name of the form as MyDataForm.cs, and click Open (see Figure 6.18). You'll then see the welcome page for the Data Form Wizard.

    click to expand
    Figure 6.18: Adding a data form using the Data Form Wizard

  3. Click the Next button to proceed.

  4. Now you enter the DataSet object you want to use in your form. You can pick an existing DataSet, or you can create a new one. Because this is a new project, you'll be creating a new DataSet. Enter myDataSet as the name for your DataSet, as shown in Figure 6.19.

    click to expand
    Figure 6.19: Entering the name of the new DataSet

  5. Click the Next button to go to the next step.

  6. You must now choose a data connection to access the database. You can pick an existing connection, or you can create a new one. Select your connection, as shown in Figure 6.20-of course, your connection name will be different.

    click to expand
    Figure 6.20: Choosing the data connection

  7. Click the Next button to continue.

  8. You now log in to the database by specifying the password for the database user. You used the sa user when creating the database connection earlier, and you therefore need to reenter the password for that user, as shown in Figure 6.21.

    click to expand
    Figure 6.21: Logging in to the SQL Server Northwind database

  9. Click the OK button to proceed.

You now select the database tables or views you want to use in your form. The area on the bottom left of the dialog box shows the tables and views you can access using your form. The area on the bottom right shows the tables and views you've added. You add a table or view to your form by selecting it from the area on the left and clicking the right-arrow button.

Tip 

You can also double-click on the table or view to add them to your form.

When you do this, the table or view moves to the right, indicating that you've selected them for use in your form. If you decide you don't want to use a table or view, you can unselect them using the left-arrow button. You can also double-click the table or view to unselect them. Select the Customers and Orders tables, as shown in Figure 6.22. Click the Next button to proceed.

click to expand
Figure 6.22: Selecting the Customers and Orders tables for use in the form

Because you selected two tables-Customers and Orders-your next step is to define a relationship between those tables. This relationship is used in your form to synchronize navigation between the rows in the Customers table with the rows in the Orders table: When you move to a new row in the Customers table, the rows from the Orders table will be displayed in your form. Set the following in the dialog box (as shown in Figure 6.23):

  1. Enter myRelationship in the Name field.

  2. Select Customers as the parent table.

  3. Select Orders as the child table.

  4. Select CustomerID as the key for each table.

    Warning 

    To add the relationship to your form, click the right-arrow button. If you don't do this, your relationship won't be added to your form.

  5. Click the Next button to continue.

  6. Select the columns from the tables you want to display in your form. Because you added the Customers and Orders tables to your form, you'll be selecting the columns to display from these two tables. By default, all the columns from the tables are selected. You won't be displaying all the columns from the Customers or the Orders table. Unselect the City column for the Customers table. (Later, you'll see how to add this column to your form manually.)

  7. Deselect the following columns for the Orders table:

    RequiredDate

    ShipAddress

    ShippedDate

    ShipCity

    ShipVia

    ShipRegion

    Freight

    ShipPostalCode

    ShipName

    ShipCountry

click to expand
Figure 6.23: Creating a relationship between two tables

Note 

Remember: You're unselecting these columns, so you uncheck the columns for the Orders table.

Figure 6.24 shows the completed dialog box with the selected columns to display from each table.

click to expand
Figure 6.24: Selecting the columns to display from each table

  1. Click the Next button to proceed.

  2. Select the display style for the rows (also known as records) in the parent table that are displayed in your form. You can display the rows in a grid, or you can display each column using a separate control. You'll use a separate control for the columns, so select the Single Record in individual controls radio button. The other check boxes in the dialog box allow you pick the controls you want to add to your form. These controls affect the rows in the master table, and you can add the following controls to your form:

    Note 

    In this example, the parent table is the Customers table, and the child table is the Orders table. The rows for the child table are displayed in a DataGrid control.

    • Cancel All The Cancel All button allows you to undo any changes you've made to the current row.

    • Add The Add button allows you to add a new row.

    • Delete The Delete button allows you to delete the current row.

    • Cancel The Cancel button allows you to cancel a change made to the current row.

    • Navigation Controls The Navigation controls consist of four buttons that allow you to move to first row, the previous row, the next row, and the last row. An indicator is also displayed to show the current row.

Figure 6.25 shows the completed dialog box.

click to expand
Figure 6.25: Choosing the display style

  1. You've now completed all the steps in the Data Form Wizard. Click the Finish button to create your form. VS .NET will now display the new form, as shown in Figure 6.26.

    click to expand
    Figure 6.26: The completed form

The managed provider objects in your form use the OLE DB classes contained in the System.Data.OleDb namespace-even though a SQL Server database is used. These objects work with any OLE DB-compliant database. The code would be more efficient if the managed provider classes in the System.Data.SqlClient namespace were used instead; these classes are specifically for use with a SQL Server database. This is the price of having the VS .NET wizard generate the code for you.

In the next section, you'll learn how the text-box controls in your form access the columns in the Customers table.

Data Binding

Each text-box control in the upper part of your form is bound to a column in the Customers table using a process known as data binding. When a control is bound to a column in a DataSet object, the value for that column is displayed in the control through the Text property in the DataBindings group. The Text property in the DataBindings group sets the text displayed in a control. To examine or set the data binding for a control, you select the control in the form designer and expand the DataBindings properties in the Properties window. You'll see these properties in the Data area of the Properties window.

Next, you'll see how the text box for the Customer ID is set. Select the text box to the right of the CustomerID label in your form; this text box is named editCustomerID. Make sure the DataBindings properties are expanded in the Properties window. Finally, click the drop-down list for the Text property to view the current column to which the text box is bound. As you can see from Figure 6.27, editCustomerID is bound to the CustomerID column of the Customers table. This means that when you run the form and load data from the database, the CustomerID column value will be displayed in the editCustomerID text box.

click to expand
Figure 6.27: The editCustomerID text box is bound to the CustomerID column

In the next section, you'll add a label and a text-box control to display the City column in your form.

Adding Controls to the Form

When you ran the Data Form Wizard earlier to create your form, you'll recall that I told you to unselect the City column of the Customers table so that it didn't appear on your form. I asked you to do this so that you can now see how to manually add a control and bind it to the City column. That way, you can see how to build your own forms that access the database.

Follow these steps to add a label and a text box to your form:

  1. Add a label below the Address label in your form. Set the Name property for your new label to lblCity.

  2. Set the Text property for your label to City.

  3. Next, add a text box below the editAddress text box.

  4. Set the Name property for your new text box to editCity.

  5. Remove the current text from the Text property so that no default text is shown in the control.

Next, you need to bind editCity to the City column of the Customers table. To do this, you open the DataBindings properties and set the text property by selecting City from the Customers table, as shown in Figure 6.28.

click to expand
Figure 6.28: Binding the City column to the editCity text box

In the next section, you'll add a Main() method to the code of your form.

Adding the Main() Method

As you know, all programs must have a Main() method. The Main() method is executed when you run your program. In this section, you'll add a Main() method to your form. To do this, select View Code, and add the following Main() method inside your MyDataForm class (a good place to add Main() would be at the start of your MyDataForm class after the open curled bracket {):

 public class MyDataForm : System.Windows.Forms.Form {   public static void Main()   {     Application.Run(new MyDataForm());   }   ... 

This code creates a new object of the MyDataForm class, causing your form to be displayed on the screen.

Setting the Password

Before you can run your form, you need to set the password for the database user in the ConnectionString property of the data connection object. This object was automatically created by VS .NET when you ran the Data Form Wizard, and the object has the default name oleDbConnection1.

To modify the ConnectionString property for oleDbConnection1, select oleDbConnection1 from the drop-down list in the Properties window. Go ahead and add the text pwd=sa in the ConnectionString property, as shown in Figure 6.29.

click to expand
Figure 6.29: Setting the ConnectionString property

You're now ready to run your form.

Running the Form

To run your form, select Debug Start without Debugging. Figure 6.30 shows the running form. You click the Load button to display the rows from the Customers and Orders tables in your form.

click to expand
Figure 6.30: The running form

Note 

You'll see a Windows console appear in the background. Don't worry about it.

Notice that the top part of the form shows the details for the row from the Customers table where the CustomerID is ALFKI; the bottom part of the form contains a DataGrid control that displays the rows from the Orders table for that customer. When you move to the next row in the Customers table, the rows from the Orders table for that customer are automatically displayed in the DataGrid.

Feel free to try out the other buttons on your form to add, modify, and delete rows in the Customers table. You can also use the DataGrid control to add, modify, and delete rows from the Orders table for the current customer.




Mastering C# Database Programming
Mastering the SAP Business Information Warehouse: Leveraging the Business Intelligence Capabilities of SAP NetWeaver
ISBN: 0764596373
EAN: 2147483647
Year: 2003
Pages: 181

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