Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Null reference exception when trying to use DataAdapter Update

Status
Not open for further replies.

webbsk

MIS
Jul 19, 2006
1
0
0
CA
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:
Code:
private void DoUpdates (OdbcDataAdapter odbcDataAdapter, string tableName)
{
	DataTable dataTableChanged =
		dsTapes.Tables[tableName].GetChanges(DataRowState.Modified);

	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.UpdateCommand.Transaction = odbcConnection.BeginTransaction();

		try
		{
			//Submit the changes.
			odbcDataAdapter.Update(dataTableChanged);
			//Commit the changes and close the connection.
			odbcDataAdapter.UpdateCommand.Transaction.Commit();
		}
		catch (Exception ex)
		{
			odbcDataAdapter.UpdateCommand.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:
Code:
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!
 
The object that is null, I think, is the UpdateCommand in
Code:
odbcDataAdapter.UpdateCommand.Transaction = odbcConnection.BeginTransaction();

The last line of code in the code that sets the Insert command is
Code:
tapeLogAdapter.InsertCommand = tapeLogInsert;

This sets the InsertCommand to an instance of the command object but the UpdateCommand still null. Set a break point at this line and see the properties of the odbcDataAdapter, I think you will find odbcDataAdapter.UpdateCommand is null.

Please avoid using reserved type names as identifiers (odbcDataAdapter is a type in the .NET Framework) like odbcConnection and odbcDataAdapter. these are types, why you use them as your objects names?

Also, when you have an unhandled exception window pops up and you don't know where it came from, hit the Debug button on the exception window (to the far bottom left), and choose to debug with VS, this will take you to the line that throw the exception. Good luck

Walid Magd (MCP)

The primary challenge of every software development team is to engineer the illusion of simplicity in the face of essential complexity.
-Grady Booch
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top