Main Menu
Knowledge Base
Product Registration
Log an Incident
Request a Feature
Search Incidents/Bug Reports


Search KB

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:

Knowledge Base Article: KB09994

HOWTO:Bind data from SQL Server to a WebGrid using declarative programming with the SqlDataSource


The information in this article applies to:
UltraWebGrid (v5.3.20053, 6.1.20061, 6.2.20062)
  Article Created: 
8/10/2006

Last Updated:
8/11/2006

Article Type
How To
  
Page Options
Average Rating:
6 out of 10

Rate this page
Print this page
E-mail this page
Add to Favorites

Summary

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.

Additional Information

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.

Step-By-Step Example

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."

Choose Data Connection Screenshot

    c. Check the checkbox to save the connection string, and click "Next."

Save Connection String to Config File Screenshot

    d. Select the table from the drop down, check the columns that you want to show on the grid, and click "Advanced."

Configure Select Statement Screenshot

    e. Check the "Generate INSERT, UPDATE, and DELETE statements" checkbox, and click "OK."

Advanced SQL Generation Options Screenshot

    f. Click "Next."
    g. Click "Test Query" to make sure that the connection works.

Test Query Screenshot

    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.

Related Articles

HOWTO: Bind a custom object to a WebGrid using declarative programming with the ObjectDataSource (KB09968)

HOWTO: Bind data from an Access database to a WebGrid using using declarative programming with the AccessDataSource (KB09995)


How would you rate the quality of this content?
Poor -----------------------------------------> Outstanding

Tell us why you rated the content this way. (optional)