Getting Data from a Database

To see how to interact with a database, you’ll begin by creating a web application that can be used to display information about the NorthWind database (that comes with SQLExpress and SQL Server).

Start by creating a new web site called WebNorthWind.

You’ll be working with the Customers table in the NorthWind database, so rename your .aspx file from default.aspx to Customers.aspx.

Warning

Remember to change the class name in the code file and in the page directive.

You need a connection to the database. You can explicitly create one, or you can use a control that depends on having a connection and one will be created for you. Let’s start by explicitly creating one.

Drag a SqlDataSource control onto the form (see Figure 9-1). You can drag and drop this control either into Design view or into Source view. Switch to Design view, and click on the smart tag to open its menu. Clicking on Configure Data Source... opens the Configure Data Source Wizard. Your first option is to choose an existing connection or to press the button to create a new connection.

SqlDataSource control on form

Figure 9-1. SqlDataSource control on form

Tip

If you do not see the SqlDataSource control, choose View Non Visual Controls.

Click on New Connection. When you create a new connection, you’ll be asked to fill in the Server name. Decide if you want to use a trusted connection (Windows Authentication) or use a specific ...

Get Programming ASP.NET, 3rd 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.