NullReferenceException adding rows to datatable RRS feed

  • Question

  • I get an intermittent exception when I try to add rows to a datatable.

    Originally, I just used a Load statement to get all the rows from a reader object:

    Code Block
    reader = cmd.ExecuteReader();
    if (reader.HasRows)
       //Bulk load all rows from reader into DataTable



    Sometimes, an exception is thrown on the line

    The full stack trace looks like this:

    System.NullReferenceException: Object reference not set to an instance of an object.
          at System.Data.Index.CompareRecords(Int32 record1, Int32 record2)
          at System.Data.Index.IndexTree.CompareNode(Int32 record1, Int32 record2)
          at System.Data.RBTree`1.RBInsert(Int32 root_ID, Int32 x_id, Int32 position)
          at System.Data.Index.InsertRecord(Int32 record, Boolean fireEvent)
          at System.Data.Index.ApplyChangeAction(Int32 record, Int32 action)
          at System.Data.DataTable.RecordsStateChange(Int32 record1, DataViewRowState oldState1, DataViewRowState newState1, Int32 record2, DataViewRosState oldState2, DataViewRowState newState2)
          at System.Data.DataTable.SetNewRecordWorker(DataRow row, Int32 proposedRecord, DataRowAction action, Boolean isInMerge, Int32 position, Boolean fireEvent, Exception& deferred Exception)
          at System.Data.DataTable.InsertRow(DataRow row, Int32 proposedID, Int32 pos, Boolean fireEvent)
          at System.Data.DataRowCollection.Add(DataRow row)
          at myFunction...

    When the exception is thrown, the datatable (which was empty when I started) has two data rows in it, even though the reader has 100 valid data rows.

    I tried changing the code to this so I could explicitly monitor every data row:

    Code Block
    reader = cmd.ExecuteReader();
    if (reader.HasRows)
       DataRow dataRow = null;
       while (reader.Read())
          dataRow = myDataTable.NewRow();
          for (int colNum = 0; colNum < reader.FieldCount; colNum++)
             dataRow[colNum] = reader.GetValue(colNum);


    With this code, the error is thrown on the second time through the loop on this statement

    Again, the datatable has two data rows in it when the exception is thrown.

    I can rerun this code using the same data, and the exception occurs only sometimes, so I don't think it's bad data. The second approach separates the data connection from the data table, so I don't think it's a database connection problem (I did see a suggestion that this could be a pooling issue).

    I've seen several reports of this error around the 'net (for example, http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=184424&SiteID=1), but I haven't seen any resolution.

    Any suggestions?

    Monday, November 5, 2007 5:33 PM

All replies

  • Hi  Jonneez,

    I think your problem reason is that,

    The reader has in the third row a null value at a field its type is integer or string or any other data type that dont accept null values. and when you try to add this row to you DataTable the exception is thrown because the (for example) Integer column at DataTable will reject the null value.


    to make sure this is the problem reason , try to set all column's types at DataTable to be 'object'. if the problem disappeard (or changed). then that was the problem reason. and run out to solve it...


    best regards

    Monday, November 5, 2007 10:55 PM
  • Thanks for your suggestion, but I know that that's not the problem.


    First, my problem is intermittent with the same data. Sometimes my datatable will load successfully, and sometimes it won't.


    Also, it's not the third row that's the problem. By using the second of the code snippets I posted, I could see what was happening one row at a time. The symptom occurred at the statement:




    the second time through the loop. In other words, the second row of data was added to the datatable, then an exception was thrown...but before the loop returned to the top to start processing row 3.


    In any event, I had considered null data could be a problem, so I've already ensured that there are absolutely no null values in my data, in any row. Yet I still get this problem. And I only get it sometimes.


    Any other ideas?

    Tuesday, November 6, 2007 1:14 PM
  • Hi,

    I am not sure it can solve your problem. Try to replace reader.FieldCount  to myDataTable.Columns.Count, hope it can work.





    Wednesday, May 7, 2008 3:16 AM
  • Thanks for your suggestion. However, the code with the FieldCount was only temporary debugging code to try to find out what was wrong with my real code. I'm not using FieldCount at all.


    I've redesigned my application since my original post. I now have code to fill a dataset rather load from a reader, so I no longer have this problem.


    Wednesday, May 7, 2008 2:46 PM