(c) 2002 Visual Studio Magazine
Fawcette Technical Publications

Issue: January 2002
Section: Retrieve Data Easily
Author: Andrew J. Brust

Do It Yourself
Listing 1	This Form_Load code illustrates how you can take advantage of .NET data binding in your own applications without using design-time components and their generated code. Although the block of code that handles table mappings is a bit dense, the rest of the code is straightforward.  You create a Connection and Command, build a DataAdapter, fill a DataSet, and bind two textboxes and a DataGrid to the DataSet's DataTable.

Private Sub frmManualCode_Load(ByVal sender _
	As System.Object, ByVal e As _
	System.EventArgs) Handles MyBase.Load

scnNorthwind.ConnectionString = _
	"Data Source=localhost;Initial _
	Catalog=Northwind;User Id=sa")

With scmCustomersSelect
	.Connection = scnNorthwind
	.CommandType = CommandType.Text
	.CommandText = "Select * from Customers"
End With

sdaCustomers.TableMappings.AddRange( _
	New System.Data.Common.DataTableMapping() _
	{New System.Data.Common. DataTableMapping( _
	"Table", "Customers", New _
	System.Data.Common. DataColumnMapping() _
	{New System.Data.Common. DataColumnMapping( _
	"CustomerID", "CustomerID"), New _
	System.Data.Common. DataColumnMapping( _
	"CompanyName", "CompanyName"), New _
	System.Data.Common. DataColumnMapping( _
	"ContactName", "ContactName"), New _
	System.Data.Common. DataColumnMapping( _
	"ContactTitle", "ContactTitle"), New _
	System.Data.Common. DataColumnMapping( _
	"Address", "Address"), New _
	System.Data.Common. DataColumnMapping( _
	"City", "City"), New System.Data.Common. _
	DataColumnMapping("Region", "Region"), _
	New System.Data.Common. DataColumnMapping( _
	"PostalCode", "PostalCode"), New _
	System.Data.Common. DataColumnMapping( _
	"Country", "Country"), New _
	System.Data.Common. DataColumnMapping( _
	"Phone", "Phone"), New System.Data.Common. _
	DataColumnMapping("Fax", "Fax") })} )

With sdaCustomers
	.SelectCommand = scmCustomersSelect
	.Fill(dsNorthwind)
End With

With scbCustomers
	.DataAdapter() = sdaCustomers
	.RefreshSchema()
End With

txtContactName.DataBindings.Add("Text", _
	dsNorthwind.Tables("Customers"), _
	"ContactName")
txtCompanyName.DataBindings.Add("Text", _
	dsNorthwind.Tables("Customers"), _
	"CompanyName")
bmbCustomers = txtContactName. _
	DataBindings("Text").BindingManagerBase
dgrCustomers.DataSource = _
	dsNorthwind.Tables("Customers")

End Sub
