Microsoft Knowledge Base Email Alertz

KBAlertz.com: (310376) - 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...

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com

ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks




Article ID: 310376 - Last Review: May 16, 2007 - Revision: 2.2

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

This article was previously published under Q310376
This article refers to the following Microsoft .NET Framework Class Library namespace:
  • System.Data.SqlClient

On This Page

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:
Data Walkthroughs
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vboriDataWalkthroughs.asp (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vboriDataWalkthroughs.asp)

OleDbDataAdapter Class
http://msdn2.microsoft.com/en-us/library/system.data.oledb.oledbdataadapter(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/system.data.oledb.oledbdataadapter(vs.71).aspx)

SqlDataAdapter Class
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDataSqlClientSqlDataAdapterClassTopic.asp (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDataSqlClientSqlDataAdapterClassTopic.asp)

OleDbCommandBuilder Class
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDataOleDbOleDbCommandBuilderClassTopic.asp (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDataOleDbOleDbCommandBuilderClassTopic.asp)

SqlCommandBuilder Class
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDataSqlClientSqlCommandBuilderClassTopic.asp (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDataSqlClientSqlCommandBuilderClassTopic.asp)
For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:
308055  (http://kbalertz.com/Feedback.aspx?kbNumber=308055/EN-US/ ) HOW TO: Update a SQL Server Database by Using the SqlDataAdapter Object in Visual Basic .NET
313483  (http://kbalertz.com/Feedback.aspx?kbNumber=313483/EN-US/ ) INFO: Roadmap for ADO.NET DataAdapter Objects

APPLIES TO
  • Microsoft ADO.NET (included with the .NET Framework)
  • Microsoft ADO.NET 1.1
Keywords: 
kberrmsg kbinfo kbsqlclient kbsystemdata KB310376
Retired KB ArticleRetired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Be the first to leave feedback, to help others about this knowledge base article.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please