|Figure 2. The SqlDataSource Control: Click on the Configure Data Source... link to configure the SqlDataSource control.|
Configuring the SqlDataSource Control
Create a Web application using Visual Studio 2005 Beta 1. In the default page, drag-and-drop the SqlDataSource control (also found under the Data tab in Toolbox). Using this control declaratively establishes a connection to a SQL data sourcewithout writing any code. In the days of ASP.NET 1.x, you needed to write elaborate code to access data sources using ADO.NET. Now, you have controls that encapsulate all the logic needed to access a data source.
To configure the SqlDataSource control to connect to a SQL Server 2000 database, click on the Configure Data Source... link in the Smart Tag (see Figure 2).
Click on the New... button to establish a new data connection.
In my case, the database service is installed locally. So I specify "(local)" for the server name and use Windows NT Integrated Security to access the database (dependent on the mode you set on your SQL Server). Select the Northwind database (see Figure 3). Click on Test Connection to verify that the connection is working. Click OK when done.
Figure 3. Specify the Database Properties: Select the Northwind database to specify the database properties.
Figure 4. View the Connection Details: After specifying the database properties, you should see the data connection details.
You should see the data connection details (see Figure 4). Click Next to proceed.
You have the option to save the connection string to the web.config file. If you do so, every time you need the connection string you can simply reference the connection string stored in web.config. In this example case, name the connection as NorthWindConn.
If you examine your web.config now, you will see the added <connectionStrings> element:
|Figure 5. Construct the SQL Statements: Specify the column that you want to retrieve construct the SQL statements.|
Database=Northwind;Persist Security Info=True"
Click on Next.
In the next step, specify the column that you want to retrieve (see Figure 5). Click on Next when you're done.
You can now test the SQL query that you have created in the previous step. Click on the Test Query button to execute the SQL query (see Figure 6). When done, click Finish.
Your SqlDataSource control is now configured. Switch to Source view to see the source of the SqlDataSource control. Note how the control references the connection string using the "<%$ %>" directive:
|Figure 6. Test the Query: Click on the Test Query button to execute the SQL query.|
SelectCommand="SELECT [ProductID], [ProductName], [SupplierID],
[CategoryID], [QuantityPerUnit], [UnitPrice] FROM
[Alphabetical list of products]"
ConnectionString="<%$ ConnectionStrings: NorthWindConn %>">