Upgrading ADO Data Binding

Data binding is one of the most powerful uses of data access in Visual Basic 6.0. Essentially, using data binding allows a property of a control to bind to a specific field in a database. When the bound control property (such as the text box's Text property) is modified by the user of the control, the control notifies the database that the value has changed. It then requests that the relevant field of the current record be updated. This process takes place, and in return the database notifies the control of the success or failure of the update. The main properties for data binding in a control are: DataChanged, DataField, DataFormat, DataMember, and DataSource.

This upgrade scenario includes any application in which a reference to Microsoft ActiveX Data Objects has been added to the project, and at least one form contains an ADO Data Control, and one or more controls use data binding, using some of the following properties: DataSource, DataMember, and DataField.

Normally, most ADO code is upgraded automatically by the upgrade wizard. With minimal manual changes, your application and Data Access technology will work the same way that it works in Visual Basic 6.0. For example, it may be necessary to add a reference to the ADODB.NET assembly (Adodb.dll) instead of the Interop.ADODB.dll that is automatically referenced by the upgrade wizard.

Visual Basic .NET only supports ADO data binding. DAO or RDO data binding are not supported. ADO data binding is supported because it is built into the COM library that manages ADO data binding in Visual Basic 6.0. Its equivalent is available in the compatibility library in Visual Basic .NET,

Microsoft.VisualBasic.Compatibility.Data. This library manages data binding in Visual Basic .NET. As a result, the properties, methods, and events are compatible between the two versions of the language, which allows ADO Control and Data Environment to be automatically upgraded.

The upgrade wizard adds some support functions to maintain the same functionality of Visual Basic 6.0. This is because of the need in Visual Basic .NET for all objects referenced in the code to be instantiated before they are used. The code added by the upgrade wizard ensures this happens. Also, when your code has data binding created at design time, the upgrade wizard adds the binding variables and procedures that are used to do the data binding with ADO.

Consider a form example that contains a TextBox with an ADO Data Control set as the DataSource property of the TextBox control, and where the DataField property of the TextControl is set to Title. When you use the upgrade wizard to upgrade such a form, variables and procedures are added to the upgraded code to enable the data binding to continue to work in Visual Basic .NET, as shown in the following code example.

Private ADOBind_Adodc1 As VB6.MBindingCollection

Public Sub VB6_AddADODataBinding()

ADOBind_Adodc1 = New VB6.MBindingCollection()

ADOBind_Adodc1.DataSource = CType(Adodc1, msdatasrc.DataSource) ADOBind_Adodc1.Add(Text1, "Text", "Title", Nothing, "Text1") ADOBind_Adodc1.UpdateMode = VB6.UpdateMode.vbUpdateWhenPropertyChanges ADOBind_Adodc1.UpdateControls() End Sub

Public Sub VB6_RemoveADODataBinding() ADOBind_Adodc1.Clear() ADOBind_Adodc1.Dispose() ADOBind_Adodc1 = Nothing End Sub

However, there are problems with the upgrade of data binding in intrinsic controls set at run time. These kinds of controls are upgraded to native Visual Basic .NET, and the DataSource, DataMember, and DataField are not upgraded by the upgrade wizard. Data binding at design time is also not supported, and the upgrade wizard does not add any binding variable or procedures, requiring you to make changes to the upgraded code.

To illustrate this situation, assume that you have a project with a form that contains a Label control named labAuthor and an ADO Data Control named Adodc1. Adodc1 is connected to the Microsoft Access database Northwind. At run time, you set the ADO Data Control to the DataSource property and set EmployeeID as the DataField, as shown in the following code example.

Private Sub Form_Load()

Set Me.labAuthor.DataSource = Adodc1 Me.labAuthor.DataField = "EmployeeID" End Sub

When this code is upgraded, the resultant code has issues that you must fix. The upgraded code is shown here.

Private Sub frmADO_Load(ByVal eventSender As System.Object, _

ByVal eventArgs As System.EventArgs) Handles MyBase.Load ' UPGRADE_ISSUE: Label property labAuthor.DataSource was not upgraded. Me.labAuthor.DataSource = Adodc1

' UPGRADE_ISSUE: Label property labAuthor.DataField was not upgraded. Me.labAuthor.DataField = "EmployeeID" End Sub

The upgrade wizard does not automatically create the variables and procedures needed for the data binding do function. Instead, you must add this code manually.

Later, you will have to add the Text property of the label control to binding, using the ADO Data Control, and you will have to eliminate the instructions that have issues. The modified code for the current example is shown here.

Private ADOBind_Adodc1 As VB6.MBindingCollection

Public Sub VB6_AddADODataBinding()

ADOBind_Adodc1 = New VB6.MBindingCollection

ADOBind_Adodc1.DataSource = CType(Adodc1, msdatasrc.DataSource) ADOBind_Adodc1.Add(labAuthor, "Text", "EmployeeID", Nothing, "labAuthor") ADOBind_Adodc1.UpdateMode = VB6.UpdateMode.vbUpdateWhenPropertyChanges ADOBind_Adodc1.UpdateControls() End Sub

Public Sub VB6_RemoveADODataBinding() ADOBind_Adodc1.Clear() ADOBind_Adodc1.Dispose() ADOBind_Adodc1 = Nothing End Sub

After these subroutines and variables are added, you have to invoke the appropriate subroutines in the same place where the DataSource and DataField properties were assigned in the original code. In the current example, these were assigned in the Load Event code. The code following example shows the appropriate adjustments.

Private Sub frmADO_Load(ByVal eventSender As System.Object, _

ByVal eventArgs As System.EventArgs) Handles MyBase.Load VB6_AddADODataBinding() End Sub

After these changes are made, the upgraded code has the same behavior in Visual Basic .NET as it did in Visual Basic 6.0.

0 0

Post a comment

  • Receive news updates via email from this site