Microsoft KB Archive/319387

= Incorrect code sample in &quot;SqlDataAdapter.UpdateCommand Property&quot; documentation =

Article ID: 319387

Article Last Modified on 2/21/2007

-

APPLIES TO


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

-



This article was previously published under Q319387



SUMMARY
The SqlDataAdapter.UpdateCommand Property MSDN documentation and the &quot;SqlDataAdapter.UpdateCommand Property&quot; topic in the Microsoft Visual Studio .NET Help documentation describe how to get or how to set a Transact-SQL statement to update records in a data source by using the UpdateCommand property of a SqlDataAdapter object. This documentation includes the following code, which creates a SqlDataAdapter and sets some of the properties of the SqlDataAdapter: Public Sub CreateSqlDataAdapter

Dim mySelectText As String = &quot;SELECT * FROM Categories ORDER BY CategoryID&quot; Dim mySelectConn As String = &quot;Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind&quot; Dim myDataAdapter As New SqlDataAdapter(mySelectText, mySelectConn) myDataAdapter.UpdateCommand.CommandText = &quot;UPDATE Categories SET Description='Cheeses, Milk, Ice Cream' WHERE CategoryName='Dairy Products'&quot; myDataAdapter.UpdateCommand.Connection = CType(myDataAdapter.SelectCommand.Connection, SqlConnection)

End Sub 'CreateSqlDataAdapter When you run the code, you receive a System.NullReferenceException exception with the following error message:

Object reference not set to an instance of an object.



MORE INFORMATION
The SqlDataAdapter.UpdateCommand property is a SqlCommand object. Because this example does not create an instance of the UpdateCommand property, you cannot access its properties.

To resolve this problem, replace the last two lines of code in the Sub CreateSqlDataAdapter procedure with the following code: Dim updateCmd As New SqlCommand updateCmd.CommandText = &quot;UPDATE Categories SET Description='Cheeses, Milk, Ice Cream' WHERE CategoryName='Dairy Products'&quot; updateCmd.Connection = CType(myDataAdapter.SelectCommand.Connection, SqlConnection) myDataAdapter.UpdateCommand = updateCmd

Keywords: kbvs2002sp1sweep kbbug kbdataadapter kbdocerr kbpending KB319387

-

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

© Microsoft Corporation. All rights reserved.