BUG: AddNew method of CurrencyManager fails with bound CheckBox (326440)



The information in this article applies to:

  • Microsoft .NET Framework Class Libraries 1.1
  • Microsoft .NET Framework Class Libraries 1.0
  • Microsoft .NET Framework 2.0
  • Microsoft Visual Basic 2005 Express Edition
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)

This article was previously published under Q326440

SYMPTOMS

When you call the AddNew method to add a new record to a DataTable object that the CurrencyManager object manages, the method silently fails and does not advance to a new record.

CAUSE

This problem occurs under the following circumstances:
  • The form contains a CheckBox control whose Checked property is bound to a DataColumn object. -and-

  • The DefaultValue property of the DataColumn object is set to System.DBNull.
This problem may occur even if the column in the underlying database has a default value of True or False.

The Checked property of a CheckBox control can only accept boolean values (True or False) and does not understand Null. Therefore, you cannot position CurrencyManager on a new row when the DefaultValue of the DataColumn is set to System.DBNull.

RESOLUTION

To resolve this problem, programmatically modify the DataColumn object in the DataTable, and then set its DefaultValue property to True or to False.

NOTE: This does not modify the property in the underlying database. This only applies to the instance of the DataTable class that the CurrencyManager is using.

For example, to set the DefaultValue of the Discontinued column in the DataTable object, follow these steps:
  1. Follow steps 1 through 9 of the "Steps to Reproduce the Behavior" section of this article.
  2. Add the following code at the end of the BindControls procedure:
    If dt.Columns("Discontinued").DefaultValue Is System.DBNull.Value Then
       dt.Columns("Discontinued").DefaultValue = False 'or True if it should be checked by default
    End If
    					
  3. On the Debug menu, click Start.
  4. Click the button. Notice that the controls are cleared when the CurrencyManager advances to a new record.

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the "Applies to" section.

MORE INFORMATION

Steps to Reproduce the Behavior

NOTE: The following code sample assumes that Microsoft SQL Server is installed locally and that SQL Server contains a copy of the Northwind database.
  1. Create a new Visual Basic .NET or Visual Basic 2005 Windows application. By default, Form1 is created.
  2. Add two TextBox controls, a Button control, and a CheckBox control to the form.
  3. On the View menu, click Code to view the form's Code module window.
  4. Add the following Imports statement at the top of the Code window:
    Imports System.Data.OleDb
    					
  5. Add the following code to the Form module:
    Private ds As New DataSet()
    Private dt As DataTable
    Private da As OleDbDataAdapter
    Private cm As CurrencyManager
    
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       Dim strConnection As String
       strConnection = "Provider=SQLOLEDB;Data Source=localhost;" & _
                       "Initial Catalog=Northwind;Integrated Security=SSPI"
       da = New OleDbDataAdapter("SELECT * FROM Products", strConnection)
       da.Fill(ds, "Products")
       dt = ds.Tables(0)
       BindControls()
       cm = CType(Me.BindingContext(dt), CurrencyManager)
    End Sub
    
    Private Sub BindControls()
       TextBox1.DataBindings.Add("Text", dt, "ProductID")
       TextBox2.DataBindings.Add("Text", dt, "ProductName")
    End Sub
    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       cm.AddNew()
    End Sub
    					
  6. On the Debug menu, click Start. Notice that the form populates with data from the first record of the Products table.
  7. Click the button. Notice that the controls are cleared when the CurrencyManager advances to a new record.
  8. Close the form, and then return to Design view.
  9. Add the following line of code to the end of the BindControls procedure:
    Checkbox1.DataBindings.Add("Checked", dt, "Discontinued")
    					
  10. On the Debug menu, click Start.
  11. Click the button. Notice that the controls retain their current values and that the CurrencyManager does not advance to a new record.

REFERENCES

For more information, click the following article number to view the article in the Microsoft Knowledge Base:

321504 BUG: A check box is not cleared when you call the AddNew method on a DataSet


Modification Type:MajorLast Reviewed:2/11/2006
Keywords:kbvs2005applies kbvs2005swept kbvs2002sp1sweep kbbug kbCtrl kbnofix KB326440