Microsoft KB Archive/310366

= The CommandBuilder object may rebuild a command that you try to modify during the next call to the DataAdapter.Update method and your changes to the command may be lost =

Article ID: 310366

Article Last Modified on 12/6/2006

-

APPLIES TO


 * Microsoft ADO.NET 2.0
 * Microsoft .NET Framework 1.1 Service Pack 1
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic 2005

-



This article was previously published under Q310366



This article refers to the following Microsoft .NET Framework Class Library namespace:
 * System.Data.SqlClient



SYMPTOMS
The CommandBuilder object may rebuild a command that you try to modify during the next call to the DataAdapter.Update method, and your changes to the command may be lost. This problem occurs under the following circumstances:
 * If you associate a CommandBuilder object with a DataAdapter object.
 * If you use the GetInsertCommand, GetUpdateCommand, or GetDeleteCommand method of the CommandBuilder object to explicitly assign commands to the DataAdapter.
 * If you modify one of the commands that CommandBuilder generates.

When you try to call the Update method of the DataAdapter, you may receive the following error message:

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in system.data.dll



CAUSE
This problem occurs because CommandBuilder dynamically alters the commands that it generates back to the original command.



RESOLUTION
Use one of the following methods to resolve this problem:
 * Do not modify commands that CommandBuilder generates. CommandBuilder does not alter Command objects that you build yourself.
 * Copy the InsertCommand, DeleteCommand, and UpdateCommand objects to a new DataAdapter (see the &quot;More Information&quot; section for an example). The new DataAdapter variable must have the same scope or narrower as the old DataAdapter variable.
 * Do not use CommandBuilder at all. Write your own Command objects, or use Visual Data Tools to write them.



STATUS
This behavior is by design.



Steps to Reproduce the Behavior
 Create a new Visual Basic Windows Application project. Form1 is added to the project by default.  Double-click the form, and add the following code at the top of the Code window: Imports System.Data.SqlClient  Add a Button control to Form1.  Double-click the button, and add the following code to the Click event: Dim cn As New SqlConnection Dim custDS As New DataSet Dim da1 As New SqlDataAdapter Dim da2 As New SqlDataAdapter Dim dr As DataRow Dim cb As SqlCommandBuilder

cn.ConnectionString = &quot;server=servername;database=northwind;uid=sa;pwd=password;&quot; cn.Open da1 = New SqlDataAdapter(&quot;select * from Customers&quot;, cn) cb = New SqlCommandBuilder(da1)

da1.Fill(custDS, &quot;Customers&quot;)

'Get the original commands.

da1.InsertCommand = cb.GetInsertCommand da1.DeleteCommand = cb.GetDeleteCommand da1.UpdateCommand = cb.GetUpdateCommand

Debug.WriteLine(&quot;Original command length: &quot; & da1.InsertCommand.CommandText.Length)

'Modify the Insert command.

da1.InsertCommand.CommandText = &quot;select * from customers where customerid=@@identity&quot;

da1.InsertCommand.UpdatedRowSource = UpdateRowSource.FirstReturnedRecord

da2.InsertCommand = da1.InsertCommand da2.DeleteCommand = da1.DeleteCommand da2.UpdateCommand = da1.UpdateCommand

'Add a record to the table.

Dim tblCust As DataTable tblCust = custDS.Tables(&quot;Customers&quot;)

Dim drCust As DataRow

drCust = tblCust.NewRow drCust(&quot;CustomerID&quot;) = &quot;ZYYYY&quot; drCust(&quot;CompanyName&quot;) = &quot;Zora's Yummies&quot; drCust(&quot;ContactName&quot;) = &quot;Christophe Namby&quot; drCust(&quot;ContactTitle&quot;) = &quot;Assistant Manager&quot;

tblCust.Rows.Add(drCust)

'Update the backend.

Debug.WriteLine(&quot;Length da1 before insert: &quot; & da1.InsertCommand.CommandText.Length)

da1.Update(custDS, &quot;Customers&quot;)

Debug.WriteLine(&quot;Length da1 after insert: &quot; & da1.InsertCommand.CommandText.Length) Debug.WriteLine(&quot;length da2 before insert: &quot; & da2.InsertCommand.CommandText.Length)

da2.Update(custDS, &quot;Customers&quot;)

Debug.WriteLine(&quot;Length da2 after insert: &quot; & da2.InsertCommand.CommandText.Length)  Modify the connection string to connect to your Microsoft SQL Server computer.</li> Press the F5 key to run the code.</li>  Comment out the following line to resolve this problem: da1.Update(custDS, &quot;Customers&quot;) </li></ol>

This resolution works because CommandBuilder hooks the RowUpdating event on the DataAdapter (&quot;da1&quot;) so that it knows when to generate the commands. When you copy the commands to a different DataAdapter (&quot;da2&quot;), CommandBuilder is no longer hooked into its events and does not alter them.

Variable scope is important because if &quot;da1&quot; and SqlCommandBuilder (&quot;cb&quot;) go out of scope, and if garbage collection occurs, CommandBuilder sets its commands to Nothing (or null in Visual C#). Thus, you cannot use these commands in &quot;da2.&quot; Because garbage collection may occur at random times, the da2.Update method call may raise intermittent NullReferenceException errors.

Additional query words: not saved take effect

Keywords: kberrmsg kbtshoot kbsqlclient kbsystemdata kbprb KB310366

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.