Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
| Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 19th, 2006, 01:59 PM
Registered User
 
Join Date: Feb 2004
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Null reference exception when using DataAdapter

I keep getting an exception when I call the DataAdapter Update method.
I have been trying to figure out what is causing the null reference
exception for this code for what seems like forever:

        private void DoInserts(OdbcDataAdapter odbcDataAdapter, string
tableName)
        {
            DataTable dataTableChanged =

dsTapes.Tables[tableName].GetChanges(DataRowState.Added);

            if ((dataTableChanged != null) &&
(dataTableChanged.Rows.Count > 0))
            {
                // Open the connection if its not already open.
                if (odbcConnection.State !=
System.Data.ConnectionState.Open)
                {
                    odbcConnection.Open();
                }

                //Create a new transaction.
                odbcDataAdapter.InsertCommand.Transaction =
odbcConnection.BeginTransaction();

                try
                {
                    //Submit the changes.
                    odbcDataAdapter.Update(dsTapes,
dataTableChanged.TableName.ToString());
                    //Commit the changes and close the connection.
                    odbcDataAdapter.InsertCommand.Transaction.Commit() ;
                }
                catch (Exception ex)
                {

odbcDataAdapter.InsertCommand.Transaction.Rollback ();
                    throw (ex);
                }
            }
        }

The exception information has not been helpful. All it says is: "Object
reference not set to an instance of an object."

I don't know if this will help, but the Insert command looks like this:

                        tapeLogInsert = odbcConnection.CreateCommand();
                        tapeLogInsert.CommandText =
                                "INSERT INTO " + tapeLogODBCName.Trim()
                                + " (NBR, Backup_Name, BakDate, ScrDate, Location, "
                                + "Tape_No, Tape_Set, Usage, Comment, UseDate) "
                                + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
                        tapeLogInsert.Parameters.Add(new OdbcParameter("NBR",
OdbcType.Int));
                        tapeLogInsert.Parameters["NBR"].SourceColumn = "nbr";
                        tapeLogInsert.Parameters.Add("Backup_Name", OdbcType.Char, 17,
"backup_name");
                        tapeLogInsert.Parameters.Add(new OdbcParameter("BakDate",
OdbcType.DateTime));
                        tapeLogInsert.Parameters["BakDate"].SourceColumn = "bakdate";
                        tapeLogInsert.Parameters.Add(new OdbcParameter("ScrDate",
OdbcType.DateTime));
                        tapeLogInsert.Parameters["ScrDate"].SourceColumn = "scrdate";
                        tapeLogInsert.Parameters.Add("Location", OdbcType.Char, 1,
"location");
                        tapeLogInsert.Parameters.Add(new OdbcParameter("Tape_No",
OdbcType.SmallInt));
                        tapeLogInsert.Parameters["Tape_No"].SourceColumn = "tape_no";
                        tapeLogInsert.Parameters.Add(new OdbcParameter("Tape_Set",
OdbcType.SmallInt));
                        tapeLogInsert.Parameters["Tape_Set"].SourceColumn = "tape_set";
                        tapeLogInsert.Parameters.Add(new OdbcParameter("Usage",
OdbcType.SmallInt));
                        tapeLogInsert.Parameters["Usage"].SourceColumn = "usage";
                        tapeLogInsert.Parameters.Add("Comment", OdbcType.VarChar, 50,
"comment");
                        tapeLogInsert.Parameters.Add(new OdbcParameter("UseDate",
OdbcType.DateTime));
                        tapeLogInsert.Parameters["UseDate"].SourceColumn = "usedate";
                        tapeLogAdapter.InsertCommand = tapeLogInsert;

Does any one have any idea what is going on here or how to go about
finding out? Any suggestions would be appreciated: I don't even know
how to figure out which reference is null!

 
Old July 21st, 2006, 09:12 AM
Friend of Wrox
 
Join Date: May 2006
Location: Delhi, Delhi, India.
Posts: 106
Thanks: 0
Thanked 0 Times in 0 Posts
Default

u'r code have much complexity, and not contain all code.I prefer passing datatable or dataset as parameters. Sometimes it happens that u'r object get dereferenced or u have not created the instance( a silly mis). I prefer connection dataadapter as local object rather passing it

Bijgupt




Similar Threads
Thread Thread Starter Forum Replies Last Post
Null reference exception was unhandled labby C# 2005 0 April 24th, 2007 06:14 AM
Null Reference Exception was unhandled labby C# 2005 1 February 9th, 2007 03:15 PM
NULL reference exception Dwizz VB.NET 2002/2003 Basics 3 June 21st, 2005 07:15 AM
null exception error abhit_kumar JSP Basics 4 January 5th, 2005 05:26 PM
Null Exception while reading ajindal Classic ASP XML 3 December 14th, 2004 06:20 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.