ADO Data Binding

ADO Data Binding

With ADO data binding, you can bind controls on forms to fields in a database. In Visual Basic 6, you can bind a control to one of three ADO objects:

  • ADO data environment

  • ADO Data control

  • ADO data source classes

The Upgrade Wizard can upgrade the first two data binding technologies, but it cannot upgrade data binding to ADO data source classes. Before we look at how data binding is upgraded, let s take a minute to review how it works in Visual Basic 6.

Each Visual Basic 6 control supports either complex or simple binding. Complex binding occurs when a control binds to an entire Recordset. The control manages its own data binding. The Microsoft DataGrid ActiveX control is a good example of complex binding. Complex binding is the simplest to upgrade, since it s only a matter of setting the control s DataSource to a Recordset and then letting the control manage its own binding. The Upgrade Wizard manages this without problems.

Simple binding is more involved. Simple binding occurs when a control binds its value to the current record in a Recordset. Binding a TextBox Text property to an ADO Data control is a good example of simple binding. Simple binding in Visual Basic 6 is controlled by four properties of the control. Table 14-1 lists these properties. In Visual Basic 6, these four properties are magic properties. When you set them to valid entries, Visual Basic 6 adds an entry to an MSBind binding collection. If you change the properties at run time, Visual Basic 6 removes the old entry from the binding collection and adds a new entry.

Table 14-1 Properties Controlling Simple Binding in Visual Basic 6

Property

Description

DataSource

The name of the ADO Data control or ADO data environment to bind to

DataMember

For ADO data environments only, the name of the command to bind to

DataField

The field of the Recordset to bind to

DataFormat

The format in which the data is to be displayed

Windows Forms controls do not have these properties, so the Upgrade Wizard adds code to the form to manage the binding collection. For example, suppose that a form has a TextBox that is bound to a field of an ADO Data control. When it upgrades the form, the Upgrade Wizard does the following:

  • Inserts a form variable that manages the control binding. The variable is declared in the form s declarations section. If the ADO Data control is called Adodc1, the binding variable is named ADOBind_Adodc1.

  • Inserts a method that sets up the binding collection. This method is named VB6_AddADODataBinding and is called from the Form.New event.

  • Inserts a method that removes the bindings and cleans up the objects when the form closes. This method is named VB6_RemoveADODataBinding and is called from the Form.Dispose event.

There are some data binding elements that the upgrade can t handle automatically. Let s look at them now.

Control Arrays of ADO Data Controls

Control arrays of ADO Data controls cannot automatically be upgraded from Visual Basic 6 to Visual Basic .NET. If you have a form with a control array of ADO Data controls, the control arrays will lose their settings during the upgrade, and the controls will not be bound. The best solution is to remove the controls from the control array in Visual Basic 6 before upgrading.

Setting Data Binding Properties at Run Time

Many projects that use data binding have code that adjusts data binding properties at run time. There are several reasons for this: you may need to change the database location, depending on whether the application is being run against a development database or a production database; or you may need to dynamically change the field a control is bound to. Code that changes data binding properties at run time may need some modifications after upgrading. The reason for requiring these modifications is that the underlying data binding mechanism has changed you no longer use the DataSource and DataField properties to set up data binding. Instead, you add and remove binding entries of a data binding variable.

Let s walk through an example to see how to upgrade code that adjusts binding properties at run time. On the companion CD, you will find a project called prjADORuntime. This Visual Basic 6 project has a TextBox bound to an ADO Data control. The binding is changed at run time to point to the Northwind database. In the Form_Load event, the code changes the connection string for the ADO Data control and then changes the data binding for the TextBox. When it is run, the form looks like Figure 14-6.

Figure 14-6

Setting up data binding dynamically at run time.

Here is the relevant code in Form_Load that changes the data binding:

'Set up connection string and recordsource for the ADODC Me.dcEmployees.ConnectionString = _    "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _     m_NorthwindDatabase Me.dcEmployees.CommandType = adCmdTable Me.dcEmployees.RecordSource = "Employees" 'Set up binding for the textbox Set Me.txtLastname.DataSource = Me.dcEmployees Me.txtLastname.DataField = "Lastname"

After upgrading, this section of code becomes the following:

'Set up connection string and recordsource for the ADODC Me.dcEmployees.ConnectionString = _    "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _     m_NorthwindDatabase Me.dcEmployees.CommandType = ADODB.CommandTypeEnum.adCmdTable Me.dcEmployees.RecordSource = "Employees" 'Set up binding for the textbox 'UPGRADE_ISSUE: TextBox property txtLastname.DataSource was not 'upgraded. Me.txtLastname.DataSource = Me.dcEmployees 'UPGRADE_ISSUE: TextBox property txtLastname.DataField was not 'upgraded. Me.txtLastname.DataField = "Lastname"

Changing the properties of the Data control is fine, but the code that sets up the binding for a particular control also needs to be modified. This code should be changed from

'UPGRADE_ISSUE: TextBox property txtLastname.DataSource was not 'upgraded. Me.txtLastname.DataSource = Me.dcEmployees 'UPGRADE_ISSUE: TextBox property txtLastname.DataField was not 'upgraded. Me.txtLastname.DataField = "Lastname"

to the following:

ADOBind_dcEmployees.Remove("txtLastname") ADOBind_dcEmployees.Add(txtLastname, "Text", "Lastname", _    Nothing, "txtLastname")

The ADOBind_dcEmployees variable is the global binding variable. This code removes the entry for the TextBox txtLastname and then adds a new entry to bind txtLastname to the Lastname field. Once this modification has been made, the code works in Visual Basic .NET.

If you want, you can go one step further and adjust the architecture of the form so that binding is set up only after the ADO Data control properties have been set at run time. If you look inside the Form_New event, you will find a line that reads

VB6_AddADODataBinding()

Remove this line of code and add it to the frmMain_Load event, so that the Load event looks like the following:

Private Sub frmMain_Load(ByVal eventSender As System.Object, _ ByVal eventArgs As System.EventArgs) Handles MyBase.Load    If DoesDatabaseExist(m_NorthwindDatabase) Then       'Set up connection string and recordsource for the ADODC       Me.dcEmployees.ConnectionString = _       "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _       m_NorthwindDatabase       Me.dcEmployees.CommandType = ADODB.CommandTypeEnum.adCmdTable       Me.dcEmployees.RecordSource = "Employees"       VB6_AddADODataBinding()    Else       MsgBox("Northwind database can't be found at " & _       m_NorthwindDatabase, MsgBoxStyle.Critical)       End    End If End Sub

What we have done here is set up the correct connection string for the ADO Data control before adding the binding. The Visual Basic .NET data binding code may be more verbose than that in Visual Basic 6, but it also offers more control over how and when the data binding is set up.

If you are adjusting the data binding at run time, it is important to set some binding at design time first. The Upgrade Wizard adds the binding variables and procedures only if it detects that a form has data binding. If the binding is set only at run time, the Upgrade Wizard will not create the binding variable and procedures.



Upgrading Microsoft Visual Basic 6.0to Microsoft Visual Basic  .NET
Upgrading Microsoft Visual Basic 6.0 to Microsoft Visual Basic .NET w/accompanying CD-ROM
ISBN: 073561587X
EAN: 2147483647
Year: 2001
Pages: 179

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net