Binding the DetailsView to a SqlDataSource
Here, our aim is to replicate the functionality the DetailsView gave us in
Chapter 11, and to add functionality that will allow users to add and delete
employees records.
Lets start by adding another SqlDataSource control, either next to or below the
existing one, in AddressBook.aspx. Give the new SqlDataSource the name em-
ployeeDataSource. Click its smart tag, and select Configure Data Source. The
Configure Data Source wizard will appear again.
In the first screen, choose the Dorknozzle connection string. Click Next, and youll
be taken to the second screen, where theres a bit more work to do. Start by
specifying the Employees table and checking all of its columns, as shown in Fig-
ure 12.9.
Figure 12.9. Choosing fields
479
Binding the DetailsView to a SqlDataSource
Figure 12.10. Creating a new condition
Next, click the WHERE button. In the dialog that opens, select the EmployeeID
column, specify the = operator, and select Control in the Source field. For the
Control ID select grid, and leave the default value empty, as Figure 12.10 shows.
Finally, click Add, and the expression will be added to the WHERE clause list. The
SQL expression thats generated will filter the results on the basis of the value
selected in the GridView control. Click OK to close the dialog, then click the Ad-
vanced button. Check the Generate INSERT, UPDATE, and DELETE statements
checkbox, as shown in Figure 12.11.
Click OK to exit the Advanced SQL Generation Options dialog, then click Next.
In the next screen, feel free to click on Test Query to ensure everythings working
as expected. If you click Test Query, youll be asked for the Employee IDs type
and value. Enter 1 for the value, leave the type as Int32, then click OK. The row
should display as shown in Figure 12.12.
Click Finish.
Congratulations! Your new SqlDataSource is ready to fill your DetailsView.
Next, we need to tie this SqlDataSource to the DetailsView and specify how
we want the DetailsView to behave. Open AddressBooks.aspx, locate the
DetailsView control and set the properties as outlined in Table 12.2.
480
Chapter 12: Advanced Data Access
Figure 12.11. Generating INSERT, UPDATE, and DELETE statements
Figure 12.12. Testing the query generated for our data source
481
Binding the DetailsView to a SqlDataSource
Table 12.2. Properties to set for the DetailsView control
ValueProperty
TrueAutoGenerateDeleteButton
TrueAutoGenerateEditButton
TrueAutoGenerateInsertButton
FalseAllowPaging
employeeDataSourceDataSourceID
EmployeeIDDataKeyNames
Recreating the Columns
If youre using Design View, make sure you choose Yes when youre asked
about recreating the DetailsView rows and data keys. If youre not using
Design View, set the columns as shown here:
File: AddressBook.aspx (excerpt)
<Fields>
<asp:BoundField DataField="EmployeeID"
HeaderText="EmployeeID" InsertVisible="False"
ReadOnly="True" SortExpression="EmployeeID" />
<asp:BoundField DataField="DepartmentID"
HeaderText="DepartmentID"
SortExpression="DepartmentID" />
<asp:BoundField DataField="Name" HeaderText="Name"
SortExpression="Name" />
<asp:BoundField DataField="Username"
HeaderText="Username"
SortExpression="Username" />
<asp:BoundField DataField="Password"
HeaderText="Password"
SortExpression="Password" />
<asp:BoundField DataField="MobilePhone"
HeaderText="MobilePhone"
SortExpression="MobilePhone" />
</Fields>
Youre ready! Execute the project, and enjoy the new functionality that you
implemented without writing a single line of code! Take it for a quick spin to
ensure that the features for editing and deleting users are perfectly functional!
482
Chapter 12: Advanced Data Access
Right now, when we add a new employee, the form looks like the one shown in
Figure 12.13.
Figure 12.13. Adding a new employee
Adding Users the Easy Way
If you want to be able to add new employees through this form, the easiest
way to do so is to leave in all the required columns; otherwise, youll get an
error when you try to add a new employee without specifying values for the
NOT NULL columns. If you dont need inserting features, and you want to
keep the list of details short, you can simply remove the unwanted columns
from the list.
483
Binding the DetailsView to a SqlDataSource

Get Build Your Own ASP.NET 2.0 Web Site Using C# & VB, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.