20.8. Programming with ADO.NET: Address Book Case Study
Our
The
AddressBook
application (Fig. 20.44) provides a GUI through which users can execute SQL statements on the database. However, instead of displaying a database table in a
DataGridView
, this example
Figure 20.44. AddressBook application that allows you to manipulate entries in an address book database.
Step 1: Adding the Database to the Project
As in the
Step 2: Indicating that the IDE Should Create a Set of Labels and TextBoxes to Display Each Row of Data
In the earlier sections, you dragged a node from the
Data Sources
window to the
Form
to create a
DataGridView
bound to the data source member represented by that node. The IDE allows you to specify the type of control(s) that it creates when you drag and drop a data source member onto a
Form
. In
Design
view, click the
Addresses
node in the
Data
Sources
window (Fig. 20.45). Note that this node becomes a drop-down list when you select it. Click the down arrow to view the items in the list. The icon to the left of
DataGridView
will initially be highlighted in blue, because the default control to be bound to a table is a
DataGridView
(as you saw in the earlier examples). Select the
Details
option in the drop-down list to
Figure 20.45. Selecting the control(s) to be created when dragging and dropping a data source member onto the Form .
Figure 20.46. Displaying a table on a Form using a series of Label s and TextBoxes .(This item is displayed on page 1035 in the print version)
Step 3: Dragging the Addresses Data Source Node to the Form
Drag the
Addresses
node from the
Data Sources
window to the
Form
(Fig. 20.46). The IDE creates a series of
Label
s and
TextBox
es because you selected
Details
in the preceding step. As in the earlier examples, the IDE also creates a
BindingNavigator
and the other
Step 4: Making the AddressID TextBox ReadOnlyThe AddressID column of the Addresses table is an auto-incremented identity column, so users should not be allowed to edit the values in this column. Select the TextBox for the AddressID and set its ReadOnly property to TRue using the Properties window. Note that you may need to click in an empty part of the Form to deselect the other Label s and TextBox es before selecting the AddressID TextBox . Step 5: Running the Application
Run the application and experiment with the controls in the
BindingNavigator
at the top of the window. Like the previous examples, this example fills a
DataSet
object (
Step 6: Adding a Query to the AddressesTableAdapter
While the
BindingNavigator
allows you to browse the address book, it would be more
Figure 20.47. Dataset Designer for the AddressBookDataSet after adding a query to AddressesTableAdapter .
Step 7: Adding Controls to Allow Users to Specify a Last Name to LocateNow that you have created a query to locate rows with a specific last name, add controls to allow users to enter a last name and execute this query. Go to Design view (Fig. 20.48) and add to the Form a Label named findLabel , a TextBox named findTextBox and a Button named findButton . Place these controls in a GroupBox named findGroupBox , then set its Text property to Find an entry by last name . Set the Text properties of the Label and Button as shown in Fig. 20.48. Figure 20.48. Design view after adding controls to locate a last name in the address book.(This item is displayed on page 1037 in the print version)
Step 8: Programming an Event Handler That Locates the User-Specified Last NameDouble click findButton to add a Click event handler for this Button . In the event handler, write the following lines of code (lines 4243 of Fig. 20.44):
addressesTableAdapter.FillByLastName( addressBookDataSet.Addresses, findTextBox.Text );
The
FillByLastName
method
Step 9: Allowing the User to Return to Browsing All Rows in the DatabaseTo allow users to return to browsing all the rows after searching for specific rows, add a Button named browseAllButton below the findGroupBox . Double click browseAllButton to add a Click event handler to the code. Set the Text property of browseAllButton to Browse All Entries in the Properties window. Add a line of code that calls addressesTableAdapter.Fill( addressBookDataSet.Addresses ) to refill the Addresses DataTable with all the rows from the table in the database (line 50 of Fig. 20.44). Also, add a line of code that clears the Text property of findTextBox (line 52). Start the application. Find a specific last name as in the previous step, then click the browseAllButton button to test the new functionality. Data Binding in the AddressBook ApplicationDragging and dropping the Addresses node from the Data Sources window onto AddressBookForm in this example caused the IDE to generate several components in the component tray. These serve the same purposes as those generated for the earlier examples that use the Books database. In this case, addressBookDataSet is an object of a strongly typed DataSet , AddressBookDataSet , whose structure mimics that of the AddressBook database. addressesBindingSource is a BindingSource object that refers to the Addresses table of the AddressBookDataSet . addressesTableAdapter encapsulates a SqlDataAdapter object configured with SqlCommand objects that execute SQL statements against the AddressBook database. Finally, addressesBindingNavigator is bound to the addressesBindingSource object, thus allowing you to indirectly manipulate the Addresses table of the AddressBookDataSet . In each of the earlier examples using a DataGridView to display all the rows of a database table, the DataGridView 's BindingSource property was set to the corresponding BindingSource object. In this example, you selected Details from the drop-down list for the Addresses table in the Data Sources window, so the values from a single row of the table appear on the Form in a set of TextBox es. The IDE sets up the data binding in this example by binding each TextBox to a specific column of the Addresses DataTable in the AddressBookDataSet . To do this, the IDE sets the TextBox 's DataBindings.Text property. You can view this property by clicking the plus sign next to (DataBindings) in the Properties window (Fig. 20.49). Clicking the drop-down list for this property allows you to choose a BindingSource object and a property (i.e., column) within the associated data source to bind to the TextBox . Figure 20.49. Viewing the DataBindings.Text property of a TextBox in the Properties window.
Consider the
TextBox
that displays the
FirstName
valuenamed
firstNameTextBox
by the IDE. This control's
DataBindings.Text
property is set to the
FirstName
property of the
AddressesBindingSource
(which refers to
AddressBookDataSet
.
Addresses
). Thus,
firstNameTextBox
always displays the value of the
FirstName
column in the currently selected row of
addressBookDataSet
.
Addresses
. Each IDE-created
TextBox
on the
Form
is configured in a similar manner. Browsing the address book with the
AddressesBindingNavigator
changes the current position in
addressBookDataSet.Addresses
and thus changes the values displayed in each
TextBox
. Regardless of changes to the contents of
addressBookDataSet.Addresses
, the
TextBox
es
|