Microsoft Knowledge Base Email Alertz

KBAlertz.com: (310367) - If you use the CommandBuilder object to explicitly get commands for the DataAdapter object as follows: da.InsertCommand = cb.GetInsertCommand and then run the following Visual Basic .NET code cb.DataAdapter = Nothing or the following Visual C# .NET...

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: 310367 - Last Review: December 11, 2006 - Revision: 2.11

Error message when you use CommandBuilder: "An unhandled exception of type 'System.NullReferenceException' occurred"

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

On This Page

SYMPTOMS

If you use the CommandBuilder object to explicitly get commands for the DataAdapter object as follows:
da.InsertCommand = cb.GetInsertCommand
				
and then run the following Visual Basic .NET code
cb.DataAdapter = Nothing
				
or the following Visual C# .NET code
cb.DataAdapter = null;
				
the commands that you add to the DataAdapter are deleted, and you receive the following error message:
An unhandled exception of type 'System.NullReferenceException' occurred in app_name.exe

Additional information: Object reference not set to an instance of an object.

CAUSE

CommandBuilder deletes the commands that it generates when it is disassociated from a DataAdapter. CommandBuilder and DataAdapter are linked; when they are unlinked or disassociated, the commands are nulled. This problem does not affect commands that you build from the beginning (from scratch).

RESOLUTION

Use one of the following methods to resolve this problem:
  • Do not disassociate the CommandBuilder from the DataSet.
  • Build the commands yourself, either in code or through Visual Data Tools.

STATUS

This behavior is by design.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Create a new Visual Basic .NET Windows Application project. Form1 is added to the project by default.
  2. Add a Button control to Form1.
  3. Switch to Code view, and add the following code to the top of the Code window:
    Imports System.Data.OleDb
    Imports System.Data.SqlClient
    					
  4. Add the following code to the Click event of the Button:
            Dim con As New SqlConnection("server=myserver;uid=sa;pwd=mypassword;" & _
                                         "database=northwind")
            Dim da As New SqlDataAdapter("Select * From Customers", con)
            Dim cb As New SqlCommandBuilder(da)
            Dim cmdInsert As New SqlCommand( _
                   "Insert Into Customer (CustomerID) Value ('AAAAA')", con)
            da.InsertCommand = cmdInsert
            da.UpdateCommand = cb.GetUpdateCommand
            da.DeleteCommand = cb.GetDeleteCommand
            Debug.WriteLine(da.InsertCommand.CommandText)
            Debug.WriteLine(da.DeleteCommand.CommandText)
            cb.DataAdapter = Nothing ' Comment out this line to avoid the error.
            cb = Nothing
            Debug.WriteLine(da.InsertCommand.CommandText)     
            Debug.WriteLine(da.DeleteCommand.CommandText)  'Error occurs here.
    					
  5. Modify your connection string as appropriate for your environment.
  6. Press the F5 key to compile and to run the application.

APPLIES TO
  • Microsoft ADO.NET 2.0
  • Microsoft ADO.NET (included with the .NET Framework)
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Visual J# .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic 2005
  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft Visual C# 2005
Keywords: 
kbtshoot kberrmsg kbprb kbsqlclient kbsystemdata KB310367
       

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