VS.net 2005 Data Binding summary

Visual Studio 2005.net introduced code-less databinding, entirely using drag and drop.

Here’s what happens in the GUI, and what occurs behind the scenes:

GUI - Add a new data source to the project
Code -
Database (mdf) added to project [optional]
Connection strings added to app.config.
TypedDataSet (xsd) created and added to project.
In the TypedDataSet, each database table is represented by property of the same name. (For example, the Document table can be accessed by MyDataSet.Document property). The property returns a DataTable (as seen in ADO.Net 1.0). A TableAdapter is also defined for each table. A TableAdapter is just a struct of SqlDataAdapter, SqlConnection and SqlCommands. The setting-name used for looking up ConnectionString is stored against each TableAdapter.
Here’s a snippet of the XSD file


   <DbSource
     ConnectionRef="MyConnectionString (Settings)"
     DbObjectName="dbo.Document"
     DbObjectType="Table"
     FillMethodModifier="Public"
     FillMethodName="Fill"
     GenerateMethods="Both"
     GenerateShortCommands="True"
     GeneratorGetMethodName="GetData"
     GeneratorSourceName="Fill"
     GetMethodModifier="Public"
     GetMethodName="GetData"
     QueryType="Rowset"
     UseOptimisticConcurrency="True"
     UserGetMethodName="GetData"
     UserSourceName="Fill">

GUI - Add a new control from data source to a form
Code - Adds TypedDataSet to form if not already there.
Adds TableAdapter for the DataTable to form if the TableAdapter is not already there.
Adds BindingSource for the DataTable to form if the BindingSource is not already there.
Adds BindingNavigator for the DataTable to form if the BindingNavigator is not already there.
Adds DataBinding to the control, associating property name with BindingSource.


this.precisTextBox.DataBindings.Add(
  new System.Windows.Forms.Binding(
    "Text", this.documentBindingSource, "Precis", true));

In the Form_load event, the datatable is populated

this.documentTableAdapter.Fill(this.myDataSet.Document);

It’s creepy when Visual Studio adds all this code for you. Although designer-added code is protected from user edits, it still opens code to be brittle. What happens when you rename a control, for instance? I suppose the refactoring browser will have to kick in and fix up all the changes. In particular, observe that the column name is not strongly typed. I don’t know how the refactoring module will cope with that.

2 Responses to “VS.net 2005 Data Binding summary”

  1. Robs writes:

    I changed an oject ( added a member) and refreshed the Data source in my UI but the change is not getting reflected. Tried removing the obj and adding again…but still change is not reflected. On stepping thru the code, the datasource after binding reflects the change…but my Data source does not contain that added member in the Datasource window for me to drag and drop onto form as a control. Please help.

  2. Robs writes:

    It worked…just closed VS and reopened…
    Thanks

Leave a Reply