INFO: Exceptions That Are Raised by the Update Method of the DataAdapter with Null Objects (310376)



The information in this article applies to:

  • Microsoft ADO.NET (included with the .NET Framework)
  • Microsoft ADO.NET (included with the .NET Framework 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.

Steps to Reproduce the Behavior

Build the Visual Basic .NET Sample

This sample uses the Northwind database that comes with Microsoft SQL Server.
  1. Start Microsoft Visual Studio .NET. Create a new Windows Application project in Visual Basic .NET. Form1 is added to the project by default.
  2. Add three Button controls to Form1. Button1, Button2, and Button3 are added by default.
  3. Double-click Form1 to open the Code window for Form1. Add the following code to the top of the Form1 Code window, above the "Public Class Form1" declaration:
    Imports System.Data
    Imports System.Data.SqlClient
    					
  4. Add the following code to the Form1 Code window, after the "Windows Form Designer generated code" section:
    Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
        MsgBox("Attempting to update a DataSet that is Nothing...")
        Dim cn As New SqlConnection("Server=SQLServerName;" & _
                                    "integrated security=true;database=northwind")
        Dim da As New SqlDataAdapter("Select * From Customers", cn)
        Dim cb As New SqlCommandBuilder(da)
        Dim ds As New DataSet()
    
        da.Fill(ds, "Cust")
        'Uncomment the following line of code to resolve this problem.
        'ds.Tables(0).Rows(0)!Region = "WA"
        '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, "Cust")
    
        cn.Close()
        cn = Nothing
    End Sub
    
    Private Sub Button2_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button2.Click
        MsgBox("Attempting to update a DataTable that is Nothing...")
        Dim cn As New SqlConnection("Server=SQLServerName;" & _
                                    "integrated security=true;database=northwind")
        Dim da As New SqlDataAdapter("Select * From Customers", cn)
        Dim cb As New SqlCommandBuilder(da)
        Dim ds As New DataSet()
    
        da.Fill(ds, "Cust")
        ds.Tables(0).Rows(0)!Region = "WA"
        da.Update(ds, "NonExistentDataTable")
        'Comment the above line of code, and uncomment the following line
        'of code to resolve this problem.
        'da.Update(ds, "Cust")
    
        cn.Close()
        cn = Nothing
    End Sub
    
    Private Sub Button3_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button3.Click
        MsgBox("Attempting to update without UpdateCommand...")
    
        Dim cn As New SqlConnection("Server=SQLServerName;" & _
                                    "integrated security=true;database=northwind")
        Dim da As New SqlDataAdapter("Select * From Customers", 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, "Cust")
        ds.Tables(0).Rows(0)!Region = "WA"
        da.Update(ds, "Cust")
    
        cn.Close()
        cn = Nothing
    End Sub
    					
  5. 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("Server=SQLServerName;" & _
                                    "integrated security=true;database=northwind")
    					

First Exception: DataSet Is Nothing or Null

  1. Press the F5 key to build and to run the project.
  2. 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.
  3. Click Break to return to the source code, and then stop running the code.
  4. In the Button1_Click event code, uncomment the following code:
        'ds.Tables(0).Rows(0)!Region = "WA"
    					
  5. Press F5 to build and to run the project, and then click Button1. Notice that you do not receive the exception.

Second Exception: DataTable Is Nothing or Null

  1. Press F5 to build and to run the project.
  2. 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'.
  3. Click Break to return to the source code, and then stop running the code.
  4. In the Button2_Click event code, comment the following code:
        da.Update(ds, "NonExistentDataTable")
    					
    Uncomment the following code:
        'da.Update(ds, "Cust")
    					
  5. Press F5 to build and to run the project, and then click Button2. Notice that you do not receive the exception.

Third Exception: UpdateCommand of the DataAdapter Is Nothing or Null

  1. Press F5 to build and to run the project.
  2. 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.
  3. Click Break to return to the source code, and then stop running the code.
  4. In the Button3_Click event code, uncomment the following code:
        'Dim cb As New SqlCommandBuilder(da)
    					
  5. Press F5 to build and to run the project, and then click Button3. Notice that you do not receive the exception.

REFERENCES

For more information, refer to the following topics in the Visual Studio .NET Help documentation: For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:

308055 HOW TO: Update a SQL Server Database by Using the SqlDataAdapter Object in Visual Basic .NET

313483 INFO: Roadmap for ADO.NET DataAdapter Objects


Modification Type:MajorLast Reviewed:9/4/2003
Keywords:kberrmsg kbinfo kbSqlClient kbSystemData KB310376