FIX: Cannot close form when the Tag property is bound to a read-only field (317254)



The information in this article applies to:

  • Microsoft ADO.NET (included with the .NET Framework)
  • Microsoft Visual Studio .NET (2002), Professional Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Developer Edition
  • Microsoft Visual Studio .NET (2002), Academic Edition

This article was previously published under Q317254

SYMPTOMS

If the Tag property of a Windows Forms control is bound to a read-only field in a DataSet object, you cannot move the focus away from the control, and you cannot close the form.

CAUSE

Because the Tag property does not cause a change notification, an attempt is always made to update the underlying DataTable class. This does not occur with the Text property because the TextChanged event is fired, and a check is made to see if the property was updated.

RESOLUTION

To work around this problem, handle the Parse event for the data-bound property, and then throw an exception. This exception is caught at a lower level and is not seen. The exception does not interfere with other data binding on the form.

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article. This bug was corrected in Microsoft ADO.NET (included with the .NET Framework 1.1), and Microsoft Visual Studio .NET (2003).

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Start Visual Studio .NET, and then create a new Microsoft Visual Basic .NET Windows application.
  2. Drag a SqlDataAdapter object from the Data tab of the toolbox to the form.
  3. Use the wizard to set up a connection to the Northwind database on the computer that is running Microsoft SQL Server. Use the following SQL statement to fetch the data:
    SELECT EmployeeID, LastName, FirstName FROM Employees
    					
  4. Right-click SqlDataAdapter1, and then click Generate Dataset. Click OK to add DataSet11 to the designer.
  5. Drag a TextBox control from the toolbox to the form.
  6. In the Properties window, expand (DataBindings), and then bind the Tag property to EmployeeID.
  7. To fill the DataSet, add the following code to the Form1_Load event:
    SqlDataAdapter1.Fill(DataSet11)
    					
  8. Drag a second TextBox control from the toolbox to the form.
  9. Press F5 to run the application. Notice that you cannot close the form and that you cannot tab out of TextBox1. However, notice that the Minimize and the Maximize buttons work.
  10. To work around this problem, return to the Visual Studio IDE, stop debugging, and then modify your code as follows:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As  System.EventArgs) Handles MyBase.Load
        AddHandler TextBox1.DataBindings("tag").Parse, AddressOf parseit
        SqlDataAdapter1.Fill(DataSet11)
    End Sub
    
    Sub parseit(ByVal o As Object, ByVal e As ConvertEventArgs)
        Throw New Exception()
    End Sub
    					
  11. Run the application again. Notice that you can tab out of TextBox1 and that you can close the form.

Modification Type:MinorLast Reviewed:9/15/2005
Keywords:kbvs2002sp1sweep kbfix kbbug kbDataBinding kbnofix KB317254