Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Using the New GridView Control in ASP.NET 2.0 : Page 2

Of all the improvements made in the release of ASP.NET 2.0, the most important by far is the ability to let the controls perform most of the work for you. Learn how declaratively configuring the new GridView control allows you to get more work done with less code!


advertisement
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 source—without 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.

<configuration xmlns= "http://schemas.microsoft.com/.NetConfiguration/v2.0"> <appSettings/> <connectionStrings> <add name="NorthWindConn" connectionString="Server=(local);Integrated Security=True; Database=Northwind;Persist Security Info=True" providerName="System.Data.SqlClient" /> </connectionStrings> ... ...

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.

<asp:SqlDataSource ID="SqlDataSource1" Runat="server" SelectCommand="SELECT [ProductID], [ProductName], [SupplierID], [CategoryID], [QuantityPerUnit], [UnitPrice] FROM [Alphabetical list of products]" ConnectionString="<%$ ConnectionStrings: NorthWindConn %>"> </asp:SqlDataSource>



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap