|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.|
Walkthrough: Modifying Data Using the ListView Web Server Control
The ASP.NET ListView control has built-in functionality that enables users to insert, edit, or delete records without requiring programming.
This walkthrough shows you how to display and update data by using the ListView control. This walkthrough uses a SqlDataSource control to retrieve results from the data source and to manage updates. The SqlDataSource control acts as the data source for the ListView control.
Tasks illustrated in this walkthrough include:
In order to complete this walkthrough, you will need:
Visual Studio 2008 or Visual Web Developer 2008 Express Edition.
Access to the SQL Server AdventureWorks database. For information about how to download and install the SQL Server sample AdventureWorks database, see Installing Sample Databases for Express Editions on the Microsoft SQL Server Web site.
If you need information about how to log on to the computer that is running SQL Server, contact the server administrator.
A user name and password for a SQL Server account that has access to the AdventureWorks database.
If you have already created a Web site (for example, by completing Walkthrough: Creating a Basic Web Page in Visual Web Developer), you can use that Web site and go to the next section. Otherwise, create a new Web site and page by following these steps.
To create a file system Web site
Open Visual Studio 2008 or Visual Web Developer 2008 Express Edition.
On the File menu, click New, and then click Web Site. If you are using Visual Web Developer 2008 Express Edition, in the File menu, click New Web Site.
The New Web Site dialog box is displayed.
Under Visual Studio installed templates, click ASP.NET Web Site.
In the first Location box, select File System, and in the second box, enter the name of the folder where you want to keep the pages of the Web site.
For example, enter the folder name C:\WebSites\ModifyData.
In the Language list, click the programming language that you prefer to work in.
Visual Studio creates the folder and a new page named Default.aspx.
In this section, you will add a ListView control to the page and configure it to display and modify data from the Department table of the AdventureWorks database.
To display and modify data in the ListView control
If the Web site does not have an App_Data folder, in Solution Explorer, right-click the project, click Add ASP.NET Folder, and then click App_Data.
In Solution Explorer, right-click the App_Data folder, and then click Add Existing Item.
The Add Existing Item dialog box is displayed.
Enter the location of the AdventureWorks database file (AdventureWorks_Data.mdf) was installed.
By default, the .mdf file is installed in the path C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\AdventureWorks_Data.mdf.
This procedure will create a copy of the database file in the project. The database file is large. If it is impractical to make a copy of the database, you can connect to it by using an alternative method, such as attaching the database file directly. However, the procedure for doing this is not covered in this walkthrough.
Switch to or open the Default.aspx file.
Switch to Design view.
From the Data tab of the Toolbox, drag a ListView control onto the page.
In the Common ListView Tasks menu, in the Choose Data Source drop-down list, click <New data source…>. The following illustration shows the Common ListView Tasks menu.
The Data Source Configuration wizard is displayed. The following illustration shows the Data Source Configuration wizard.
This specifies that you want to get data from a database that supports SQL statements, which includes SQL Server and other OLE-DB–compatible databases.
In the Specify an ID for the data source box, a default data source control name, SqlDataSource1, is displayed. You can leave this name.
The Configure Data Source wizard is displayed.
Under Which data connection should your application use to connect to a database?, select AdventureWorks_Data.mdf from the list.
The wizard displays a page where you can choose to store the connection string in the configuration file. Storing the connection string in the configuration file has two advantages:
It can be more secure than storing it in the page.
You can use the same connection string in multiple pages.
Make sure that the Yes, save this connection as check box is selected, and then click Next. You can leave the default connection string name.
The Configure Data Source wizard is displayed, where you can specify the data that you want to retrieve from the database.
Select the Specify a custom SQL statement or stored procedure option. The following illustration shows the Configure Data Source wizard.
Typically, you would use the option Specify columns from a table or view. However, because the AdventureWorks database has schema names, in this walkthrough you will create a custom SQL statement.
In the Define Custom Statements or Stored Procedures page, enter the following SQL query to retrieve department data from the AdventureWorks database.
SELECT DepartmentID, Name, GroupName FROM HumanResources.Department
You can also click the Query Builder and use Query Builder to create a query and validate it by using the Execute Query button.
Click the UPDATE tab and then enter the following SQL query to update department data in the AdventureWorks database.
UPDATE HumanResources.Department SET Name = @Name, GroupName = @GroupName WHERE (DepartmentID = @DepartmentID)
Click the INSERT tab and then enter the following SQL query to insert department data in the AdventureWorks database.
INSERT INTO HumanResources.Department(Name, GroupName) VALUES (@Name, @GroupName)
Click the DELETE tab and then enter the following SQL query to delete department data from the AdventureWorks database.
DELETE FROM HumanResources.Department WHERE (DepartmentID = @DepartmentID)
Click Test Query to make sure that you are retrieving the data that you want.
Right-click the ListView control, click Show Smart Tag.
In the ListView Tasks menu, click Configure ListView.
The Configure ListView dialog box is displayed.
Under Options, select the Enable Editing, Enable Inserting, and Enable Deleting check boxes. The following illustration shows the Configure ListView dialog box.
You might also want to select a different style, which can make it easier to view the data. To do this, under Select a Style, select a style such as Colorful.
Click OK. The configured ListView control might look like the following illustration.
You can now test the ListView control.
To test the page
Press CTRL+F5 to run the page.
The ListView control is displayed with DepartmentID, Name, and GroupName columns.
Click the Edit button to modify a record in the table.
Change the values and then click Update, or click Cancel to cancel the edit operation.
Enter values for the Name and GroupName fields in the bottom of the page and click Insert to insert a new record.
Click the Delete button of the record you just inserted to delete the record from the database.
This walkthrough has shown you the basic steps for using a ListView control to display and edit data records by using a custom layout. The ListView control enables you to perform more sophisticated formatting than what you have done in this walkthrough. To explore other possible scenarios with the ListView control, see ListView Web Server Control Overview.