Microsoft KB Archive/310376

= INFO: Exceptions That Are Raised by the Update Method of the DataAdapter with Null Objects =

Article ID: 310376

Article Last Modified on 5/16/2007

-

APPLIES TO


 * Microsoft .NET Framework 1.1 Service Pack 1
 * Microsoft ADO.NET 1.1

-



This article was previously published under Q310376



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



SUMMARY
This article lists the exceptions that can be raised when you invoke the Update method of a DataAdapter object to update a DataSet object or a DataTable object. These exceptions occur if the DataSet, the DataTable, or the UpdateCommand property is Nothing or Null.



MORE INFORMATION
 If the DataSet object that you want to update is Nothing or Null, you receive the following exception (or similar):

An unhandled exception of type 'System.ArgumentNullException' occurred in system.data.dll

Additional information: Value cannot be null.

To resolve this problem, make sure that the DataSet that you want to update is not Nothing or Null before you pass it into the Update method of the DataAdapter. If the DataTable object that you want to update is Nothing or Null, you receive the following exception (or similar):

An unhandled exception of type 'System.InvalidOperationException' occurred in system.data.dll

Additional information: Update unable to find TableMapping['Cust'] or DataTable 'Cust'.

where 'Cust' represents a DataTable name.

Alternately, you may receive the following exception (or similar):

An unhandled exception of type 'System.InvalidOperationException' occurred in system.data.dll

Additional information: Update unable to find TableMapping['Table'] or DataTable 'Table'.

To resolve this problem, make sure that the DataTable that you want to update is not Nothing or Null before you pass it into the Update method of the DataAdapter. If the DataAdapter does not have an InsertCommand, an UpdateCommand, or a DeleteCommand property, you receive the following exception or similar:

An unhandled exception of type 'System.InvalidOperationException' occurred in system.data.dll

Additional information: Update requires a valid UpdateCommand when passed DataRow collection with modified rows

To resolve this problem, make sure that the DataAdapter has a valid UpdateCommand. To create an UpdateCommand, an InsertCommand, and a DeleteCommand for a DataAdapter, use one of the following methods:  Write the commands manually. Use the CommandBuilder object to build the commands dynamically at run time. Use the Visual Data tools to write the commands at design time.</li></ul> </li></ul>

Build the Visual Basic .NET Sample
This sample uses the Northwind database that comes with Microsoft SQL Server. <ol> Start Microsoft Visual Studio .NET. Create a new Windows Application project in Visual Basic .NET. Form1 is added to the project by default.</li> Add three Button controls to Form1. Button1, Button2, and Button3 are added by default.</li>  Double-click Form1 to open the Code window for Form1. Add the following code to the top of the Form1 Code window, above the &quot;Public Class Form1&quot; declaration: Imports System.Data Imports System.Data.SqlClient </li>  Add the following code to the Form1 Code window, after the &quot;Windows Form Designer generated code&quot; section: Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click MsgBox(&quot;Attempting to update a DataSet that is Nothing...&quot;) Dim cn As New SqlConnection(&quot;Server=SQLServerName;&quot; & _                               &quot;integrated security=true;database=northwind&quot;) Dim da As New SqlDataAdapter(&quot;Select * From Customers&quot;, cn) Dim cb As New SqlCommandBuilder(da) Dim ds As New DataSet

da.Fill(ds, &quot;Cust&quot;) 'Uncomment the following line of code to resolve this problem. 'ds.Tables(0).Rows(0)!Region = &quot;WA&quot; 'Without the above line, ds.GetChanges does not return a DataSet 'because no Rows are modified. Dim ds2 As DataSet = ds.GetChanges da.Update(ds2, &quot;Cust&quot;)

cn.Close cn = Nothing End Sub

Private Sub Button2_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button2.Click MsgBox(&quot;Attempting to update a DataTable that is Nothing...&quot;) Dim cn As New SqlConnection(&quot;Server=SQLServerName;&quot; & _                               &quot;integrated security=true;database=northwind&quot;) Dim da As New SqlDataAdapter(&quot;Select * From Customers&quot;, cn) Dim cb As New SqlCommandBuilder(da) Dim ds As New DataSet

da.Fill(ds, &quot;Cust&quot;) ds.Tables(0).Rows(0)!Region = &quot;WA&quot; da.Update(ds, &quot;NonExistentDataTable&quot;) 'Comment the above line of code, and uncomment the following line 'of code to resolve this problem. 'da.Update(ds, &quot;Cust&quot;)

cn.Close cn = Nothing End Sub

Private Sub Button3_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button3.Click MsgBox(&quot;Attempting to update without UpdateCommand...&quot;)

Dim cn As New SqlConnection(&quot;Server=SQLServerName;&quot; & _                               &quot;integrated security=true;database=northwind&quot;) Dim da As New SqlDataAdapter(&quot;Select * From Customers&quot;, cn) 'Uncomment the following line of code to resolve this problem. 'Dim cb As New SqlCommandBuilder(da)

Dim ds As New DataSet da.Fill(ds, &quot;Cust&quot;) ds.Tables(0).Rows(0)!Region = &quot;WA&quot; da.Update(ds, &quot;Cust&quot;)

cn.Close cn = Nothing End Sub </li>  In each Button_Click event procedure, modify the following code as appropriate to connect to the computer that is running SQL Server: Dim cn As New SqlConnection(&quot;Server=SQLServerName;&quot; & _                               &quot;integrated security=true;database=northwind&quot;) </li></ol>

First Exception: DataSet Is Nothing or Null
<ol> Press the F5 key to build and to run the project.</li> Click Button1. You receive the following exception:

An unhandled exception of type 'System.ArgumentNullException' occurred in system.data.dll

Additional information: Value cannot be null.

This exception occurs because no rows are modified in the Cust table of the dsDataSet. Therefore, ds.GetChanges does not return a DataSet, and ds2 is Nothing.</li> Click Break to return to the source code, and then stop running the code.</li>  In the Button1_Click event code, uncomment the following code: 'ds.Tables(0).Rows(0)!Region = &quot;WA&quot; </li> Press F5 to build and to run the project, and then click Button1. Notice that you do not receive the exception.</li></ol>

Second Exception: DataTable Is Nothing or Null
<ol> Press F5 to build and to run the project.</li> Click Button2. You receive the following exception:

An unhandled exception of type 'System.InvalidOperationException' occurred in system.data.dll

Additional information: Update unable to find TableMapping['NonExistentDataTable'] or DataTable 'NonExistentDataTable'

This exception occurs because 'NonExistentDataTable' is not a DataTable within ds. In this example, you intended to update 'Cust'.</li> Click Break to return to the source code, and then stop running the code.</li>  In the Button2_Click event code, comment the following code: da.Update(ds, &quot;NonExistentDataTable&quot;) Uncomment the following code: 'da.Update(ds, &quot;Cust&quot;) </li> Press F5 to build and to run the project, and then click Button2. Notice that you do not receive the exception.</li></ol>

Third Exception: UpdateCommand of the DataAdapter Is Nothing or Null
<ol> Press F5 to build and to run the project.</li> <li>Click Button3. You receive the following exception:

An unhandled exception of type 'System.InvalidOperationException' occurred in system.data.dll

Additional information: Update requires a valid UpdateCommand when passed DataRow collection with modified rows.

</li> <li>Click Break to return to the source code, and then stop running the code.</li> <li> In the Button3_Click event code, uncomment the following code: 'Dim cb As New SqlCommandBuilder(da) </li> <li>Press F5 to build and to run the project, and then click Button3. Notice that you do not receive the exception.</li></ol>

<div class="references_section">