0

I'm having this strange issue while adding a new row to a DataTable. It does not occur every time, but while running the application, I get null reference exception while there are no objects found null.

Here is the code:

Firstly Clear the Row and Columns of the DataTable:

dtLibraries.Rows.Clear();
dtLibraries.Columns.Clear();
dtLibraries.Columns.Add("Col1");
dtLibraries.Columns.Add("Col2");
dtLibraries.Columns.Add("Col3");
dtLibraries.Columns.Add("Col4");
dtLibraries.Columns.Add("Col5");
dtLibraries.Columns.Add("Col6");
dtLibraries.Columns.Add("Col7");
dtLibraries.Columns.Add("Col8");
dtLibraries.Columns.Add("Col9");

Then reading another DataTable and Writing Data to this table:

for (int s = 0; s <= dttemp.Rows.Count - 1; s++)
{
    dttemp1.Clear();
    dttemp1 = business.GetOutputLibraries("SimObjectOutputRequestSet",
    dttemp.Rows[s].ItemArray[2].ToString(),
    dttemp.Rows[s].ItemArray[1].ToString(), dttemp.Rows[s].ItemArray[0].ToString());

    for (int j = 0; j <= dttemp1.Rows.Count - 1; j++)
    {
        DataRow dr = dtLibraries.NewRow();
        dr["Col1"] = dttemp1.Rows[j].ItemArray[0].ToString();
        dr["Col2"] = dttemp1.Rows[j].ItemArray[1].ToString();
        dr["Col3"] = dttemp1.Rows[j].ItemArray[2].ToString();
        dr["Col4"] = dttemp.Rows[s].ItemArray[0].ToString();
        dr["Col5"] = dttemp.Rows[s].ItemArray[2].ToString();
        dr["Col6"] = dttemp.Rows[s].ItemArray[1].ToString();
        dr["Col7"] = cmbObject.Text.ToString();
        dr["Col8"] = cmbLibraryType.Text;
        dr["Col9"] = cmbLibrarySubType.Text;
        dtLibraries.Rows.Add(dr);  /// Exception occurs on this line
    }
}

StackTrace:

at System.Data.NameNode.Eval(DataRow row, DataRowVersion version)
   at System.Data.BinaryNode.EvalBinaryOp(Int32 op, ExpressionNode left, ExpressionNode right, DataRow row, DataRowVersion version, Int32[] recordNos)
   at System.Data.BinaryNode.Eval(DataRow row, DataRowVersion version)
   at System.Data.DataExpression.Invoke(DataRow row, DataRowVersion version)
   at System.Data.Index.AcceptRecord(Int32 record, IFilter filter)
   at System.Data.Index.ApplyChangeAction(Int32 record, Int32 action, Int32 changeRecord)
   at System.Data.Index.RecordStateChanged(Int32 record, DataViewRowState oldState, DataViewRowState newState)
   at System.Data.DataTable.RecordStateChanged(Int32 record1, DataViewRowState oldState1, DataViewRowState newState1, Int32 record2, DataViewRowState oldState2, DataViewRowState newState2)
   at System.Data.DataTable.SetNewRecordWorker(DataRow row, Int32 proposedRecord, DataRowAction action, Boolean isInMerge, Boolean suppressEnsurePropertyChanged, Int32 position, Boolean fireEvent, Exception& deferredException)
   at System.Data.DataTable.InsertRow(DataRow row, Int64 proposedID, Int32 pos, Boolean fireEvent)
   at System.Data.DataRowCollection.Add(DataRow row)
13
  • what is dttemp1?which line exactly rise the error execption? Commented Nov 2, 2012 at 7:30
  • 1
    your StackTrace doesn't has details like which method call raised exception, in which method exception occurred etc.. Commented Nov 2, 2012 at 7:33
  • @Selalu_Ingin_Belajar: it's another DataTable. I am converting that dataTable values to string, so i do not think that will cause error. Commented Nov 2, 2012 at 7:34
  • @dotNETbeginner: it's Exception strackTrace, i have remove the last line and that is not relevant to problem. Commented Nov 2, 2012 at 7:36
  • Why don't you step through the code and see on what line the exception occurs? It would help us help you (and at best, you'll find the answer by then) =) Commented Nov 2, 2012 at 7:36

2 Answers 2

1

it's from your schema :

dtLibraries.Columns.Add("Col1");
dtLibraries.Columns.Add("Col2");

you using Capital "C". meanwhile you call column with lowercase "c".

dr["col1"] = dttemp1.Rows[j].ItemArray[0].ToString();
dr["col2"] = dttemp1.Rows[j].ItemArray[1].ToString();

maybe that's the problem

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry for the typo, passed column names are already match with name of the column. this is not an issue. These lines will cause error before the line where i am specifying the error.
1

As suggested by @AndreasSabroeJydebjerg, I have checked for the filters. May be that was causing problem with binding controls.

To make it work, i have loaded another table done all work around for this..

Thanks to all for suggestion.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.