ASP.NET 2.0 DataSet DataSource

  

ASP.NET 2.0 doesn’t provide good support for binding a DataSet to later persist the changes to the database.  

The built-in ones only let you save one row, but not the whole set of DataSet changes at once, which IMHO is probably the most interesting feature of the DataSet. Also, this probably makes ASP.NET built-in DataSources useless for updating except when doing very simple tasks. 

The good news is that ASP.NET 2.0 provides good support for creating your own DataSources. Actually, it’s even better than .NET Windows Forms in that regard.  

Because we needed good support for binding DeKlarit-generated components, we built one. Below is a stripped down version that supports binding to any .NET DataSet.  

Here you can get the binaries and sources.  

To make it work, you need to install the binaries in the GAC, and then drop the Web.DataSources.dll on the toolbox.

After you drop the component on the page, you’ll see something like the following:  

I built a basic designer for the DataSource to show how it can be done. To select the DataSet you want to use, click ‘Configure Data Source.’ It will show all the classes that inherit from the DataSet in your project or the projects you are referencing:  

 

You can also set the same property in the Properties window:  

 

Then you can add a DataSource-aware ASP.NET controls and bind them:

   

 

For each control, you can select the DataTable you want to bind to by using the DataMember property:

  

State

Standard ASP.NET DataSources cannot make changes to a DataSet mainly because they don’t keep the DataSet instance around. We need to keep the DataSet instance somewhere. As usual, we can keep it in the ViewState or in the Session. If the DataSource’s EnableViewState property is set to True, it will be kept in the ViewState; otherwise, it will be kept in the Session.

  

Loading / Saving the DataSet

  

This DataSource can’t load/save data automatically to the database, as it’s not aware of the component that does it. You may add this behavior if you know how your data access components work. For example, in DeKlarit we do load/save automatically to the database because we know how the generated components work. 
 

The following code shows how you would load/save the DataSet using a VS 2005 TableAdapter:

 

  

Note that the DataSet is created by the DataSource and exposed as a DataSet property. In this case, it is wrapped by the NorthwindDataSet property that casts it to the proper typed dataset type.

  

Enjoy!

  

2 Comments

Comments have been disabled for this content.