Please note: In an effort to better serve you, we are in the process of restructuring DevCenter. In the process, we have moved many items that you may be used to finding in DevCenter over to the Main Site. If you are having trouble locating something, please try looking at the following places:
With the introduction of DataSource controls in ASP.Net 2.0, a developer can connect the presentation layer to data taken from SQL Server by using a SqlDataSource control. Our controls have complete built in support for this new feature of version 2.0 of the .NET Framework, and thus you can use them to connect to data from SQL Server without having to write a single line of code.This article demonstrates how to use the SqlDataSource in Visual Studio 2005 with the WebGrid.
While it is a powerful and easy-to-use object, the SqlDataSource is not without limitations. With respect to the WebGrid, the most important limitation is that the SqlDataSource does not provide hierarchical data. It represents only a single table of data, and there is currently no way to relate multiple SqlDataSource objects to each other.This article does not include a sample, due to the likelihood that the configuration of the connection to SQL Server differing from computer to computer. Instead, it provides step-by-step instructions on how to create such a sample.
The following steps demonstrate how to configure a WebGrid to load data from a SqlDataSource.1. Drop a WebGrid, a SqlDataSource control, and a button to a page in a new ASP.NET website.2. Configure the SqlDataSource control: a. Open the DataSource configuration for the SqlDataSource control. b. Select the database that you want to connect to, and click "Next." c. Check the checkbox to save the connection string, and click "Next." d. Select the table from the drop down, check the columns that you want to show on the grid, and click "Advanced." e. Check the "Generate INSERT, UPDATE, and DELETE statements" checkbox, and click "OK." f. Click "Next." g. Click "Test Query" to make sure that the connection works. h. Click "Finish."3. Configure the WebGrid to attach to the SqlDataSource control by setting the following properties. a. UltraWebGrid1.DataSourceID = SqlDataSource1 (NOTE: If doing this in code, use SqlDataSource1.UniqueID instead) b. UltraWebGrid1.DisplayLayout.AllowUpdateDefault = Yes (this should be the value set by the default preset) c. UltraWebGrid1.DisplayLayout.AllowAddNewDefault = Yes d. UltraWebGrid1.DataKeyField = "CustomerID" e. UltraWebGrid1.Bands[0].DataKeyField = "CustomerID"Now, when you run the application, you can insert, update, and delete rows within the WebGrid. Clicking on the button will cause a postback; this causes the WebGrid to notify the SqlDataSource of the changes, which will commit all these changes the database automatically.