Generating a Data-Driven Web Page
Create a new Web site and page by following these steps.
To create a file system Web site
- Open Visual Web Developer.
- On the File menu, click New Web Site.The New Web Site dialog box appears.
- Under Visual Studio installed templates, click ASP.NET Web Site.
- In the right-most Location box, enter the name of the folder where you want to keep the pages of the Web site.For example, type the folder name C:\WebSites.
- In the Language list, click the programming language that you prefer to work in.
- Click OK.Visual Web Developer creates the folder and a new page named Default.aspx.
To display data on an ASP.NET Web page, you need the following:
- A connection to a data source (such as a database).In the following procedure, you will create a connection to the SQL Server Northwind database.
- A data source control on the page, which executes queries and manages the results of the queries.
- A control on the page to actually display the data.In the following procedure, you will display data in a GridView control. The GridView control will get its data from the SqlDataSource control.
You can add these elements to the Web site separately. However, it is easiest to start by visualizing the data display using GridView control, and then using wizards to create the connection and data source control. The following procedure explains how to create all three of the elements that you must have to display data on the page.
To add and configure a GridView control for displaying data
- In Visual Web Developer, switch to Design view.
- From the Data folder in the Toolbox, drag a GridView control onto the page.
- If the GridView Tasks shortcut menu does not appear, right-click the GridView control, and then click Show Smart Tag.
- On the GridView Tasks menu, in the Choose Data Source list, click <New data source>.The Data Source Configuration dialog box appears.
- Click Database.This specifies that you want to obtain data from a database that supports SQL statements. This 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 appears (SqlDataSource1). You can leave this name.
- Click OK.The Configure Data Source Wizard appears, displaying a page on which you can choose a connection.
- Click New Connection.
- In the Choose Data Source dialog box, under Data source, click Microsoft SQL Server, and then click Continue.The Add Connection dialog box appears.
- In the Server name box, enter the name of the SQL Server that you want to use.
- For the logon credentials, select the option that is appropriate for accessing the SQL Server database (integrated security or specific ID and password) and if it is required, enter a user name and password.
- Click Select or enter a database name, and then enter Northwind.
- Click Test connection, and when you are sure that it works, click OK.The Configure Data Source – <DataSourceName> Wizard appears and the connection information is filled in.
- Click Next.The wizard appears, displaying a page on which you can choose to store the connection string in the configuration file. Storing the connection string in the configuration file has two advantages:
- It is more secure than storing the connection string in the page.
- You can reuse 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 of NorthwindConnectionString.)The wizard appears, displaying a page on which you can specify the data that you want to fetch from the database.
- Under Specify columns from a table or view, in the Name list, click Customers.
- Under Columns, select the CustomerID, CompanyName, and City check boxes.The wizard appears, displaying the SQL statement that you are creating in a box at the bottom of the page.
Note: The wizard lets you specify selection criteria (a WHERE clause) and other SQL query options. For this part of the walkthrough, you will create a simple statement without selection or sort options.
- Click Next.
- Click Test Query to make sure that you are fetching the data you want.
- Click Finish.The wizard closes and you are returned to the page. Running the wizard has accomplished two tasks:
- The wizard created and configured a SqlDataSource control (named SqlDataSource1), which incorporates the connection and query information that you specified.
- The wizard bound the GridView control to the SqlDataSource. Therefore, the GridView control will display data that is returned by the SqlDataSource control.
If you view the properties for the SqlDataSource control, you can see that the wizard has created values for the ConnectionString and SelectQuery properties.
Note: You can easily change the look of the GridView control. In Design view, right-click the GridView control, and then click Show Smart Tag. On the GridView Tasks menu, click Auto Format, and then apply a scheme.