3

I'm trying to join 2 DataTables but I' m getting this error:

Object reference not set to an instance of an object 

this is what I'm doing:

DataTable NodeDataTable = new DataTable();
DataTable sdosDataTable = new DataTable();
private DataTable NodedataTable()
    {
        XmlTextReader xmlreader = new XmlTextReader(Server.MapPath("stuff.xml"));
        DataSet ds = new DataSet();
        ds.ReadXml(xmlreader);
        xmlreader.Close();
        if (ds.Tables.Count != 0)
        {
            NodeDataTable = ds.Tables[22];
        }
        return NodeDataTable;
    }
    private DataTable SdosDataTable()
    {
        XmlTextReader xmlreader = new XmlTextReader(Server.MapPath("stuff.xml"));
        DataSet ds = new DataSet();
        ds.ReadXml(xmlreader);
        xmlreader.Close();
        if (ds.Tables.Count != 0)
        {
            sdosDataTable = ds.Tables[10];
        }
        return sdosDataTable;
    }

and to join both DataTables:

private void JoinNodeSdosDT()
    {
        DataColumn obj_NodeID, obj_SdosID;
        DataSet ds1 = new DataSet();    
        NodeDataTable = NodeDataTable.Copy();
        sdosDataTable = sdosDataTable.Copy();
        ds1.Tables.Add(NodeDataTable);
        ds1.Tables.Add(sdosDataTable);
        obj_NodeID = ds1.Tables["node"].Columns["node_Id"];
        obj_SdosID = ds1.Tables["sdos"].Columns["node_Id"];    
        sdosDataTable.Columns.Add("typeCodee");           
        DataRelation obj_NodeandSdosRelation = new DataRelation("dept_reln", obj_NodeID, obj_SdosID);
        ds1.Relations.Add(obj_NodeandSdosRelation); 
        foreach (DataRow dr_NodeSods in ds1.Tables["sdos"].Rows)
        {
            DataRow dr_NondeeeR = dr_NodeSods.GetParentRow("dept_reln");
            dr_NodeSods["typeCodee"] = dr_NondeeeR["typeCode"];
        }
        DataTable dtResult = ds1.Tables["sdos"].DefaultView.ToTable(false, "node_Id", "typeCode", "sdos_Id");
        GridView1.DataSource = dtResult;           
    }

there is some any matching ID what can I do here to resolve my problem.

I removed The datatable Images there is no use of them.

Clement
  • 91
  • 1
  • 8

2 Answers2

1

Looks like dr_NondeeeR is null:

DataRow dr_NondeeeR = dr_NodeSods.GetParentRow("dept_reln");
dr_NodeSods["typeCodee"] = dr_NondeeeR["typeCode"];

because for whatever reason GetParentRow is returning null.

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
  • 2
    Are you sure? The documentation of the `DataRow.Item` property states that this should throw an `ArgumentException` when accessing a non existing column, not a `NullReferenceException` as he gets. – Jan Dörrenhaus Jun 13 '13 at 12:05
  • Just posted that answer myself. Looks like we came to the same conclusion, then :) – Jan Dörrenhaus Jun 13 '13 at 12:07
  • @Clement, that would kind of be the issue though. The `GetParentRow` method is returning `null` because there is no related row. Just check for `null` and move on if no related row exists. There's nothing to merge. – Mike Perrenoud Jun 13 '13 at 12:32
  • Thanks man I think I' m getting confused a lot on this things. it real worked after checking for null. – Clement Jun 13 '13 at 12:44
  • @Clement, I'm glad I could be of assistance! – Mike Perrenoud Jun 13 '13 at 12:47
1

As the documentation of DataRow.Item (String) states, accessing a non-existing column should give an ArgumentException. What you are getting is a NullReferenceException. If that happens actually in the line you gave, then I can only assume that

DataRow dr_NondeeeR = dr_NodeSods.GetParentRow("dept_reln");

gives a null reference.

Jan Dörrenhaus
  • 6,581
  • 2
  • 34
  • 45