Microsoft KB Archive/326440

= BUG: AddNew method of CurrencyManager fails with bound CheckBox =

Article ID: 326440

Article Last Modified on 12/3/2007

-

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 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



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:  Follow steps 1 through 9 of the &quot;Steps to Reproduce the Behavior&quot; section of this article.  Add the following code at the end of the BindControls procedure: If dt.Columns(&quot;Discontinued&quot;).DefaultValue Is System.DBNull.Value Then dt.Columns(&quot;Discontinued&quot;).DefaultValue = False 'or True if it should be checked by default End If                    On the Debug menu, click Start. 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 &quot;Applies to&quot; section.



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.  Create a new Visual Basic .NET or Visual Basic 2005 Windows application. By default, Form1 is created.</li> Add two TextBox controls, a Button control, and a CheckBox control to the form.</li> On the View menu, click Code to view the form's Code module window.</li>  Add the following Imports statement at the top of the Code window: Imports System.Data.OleDb </li>  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 = &quot;Provider=SQLOLEDB;Data Source=localhost;&quot; & _ &quot;Initial Catalog=Northwind;Integrated Security=SSPI&quot; da = New OleDbDataAdapter(&quot;SELECT * FROM Products&quot;, strConnection) da.Fill(ds, &quot;Products&quot;) dt = ds.Tables(0) BindControls cm = CType(Me.BindingContext(dt), CurrencyManager) End Sub

Private Sub BindControls TextBox1.DataBindings.Add(&quot;Text&quot;, dt, &quot;ProductID&quot;) TextBox2.DataBindings.Add(&quot;Text&quot;, dt, &quot;ProductName&quot;) End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click cm.AddNew End Sub </li> On the Debug menu, click Start. Notice that the form populates with data from the first record of the Products table.</li> Click the button. Notice that the controls are cleared when the CurrencyManager advances to a new record.</li> Close the form, and then return to Design view.</li>  Add the following line of code to the end of the BindControls procedure: Checkbox1.DataBindings.Add(&quot;Checked&quot;, dt, &quot;Discontinued&quot;) </li> On the Debug menu, click Start.</li> Click the button. Notice that the controls retain their current values and that the CurrencyManager does not advance to a new record.</li></ol>

<div class="references_section">