Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
Which line is throwing the exception? – Jon Skeet 20 hours ago
dr_NodeSods["typeCodee"] = dr_NondeeeR["typeCode"]; – Clement 20 hours ago
Post the full stack trace please.. Edit: Check that dr_NondeeeR["typeCode"] has value. – Oscar 20 hours ago
That isn't a misspelling is it? Is it really typeCodee vs typeCode? – DonBoitnott 20 hours ago
some don't have matching value – Clement 20 hours ago
show 2 more comments

2 Answers

up vote 1 down vote accepted

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.

share|improve this answer
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 Doerrenhaus 20 hours ago
@JanDoerrenhaus, fantastic catch. Have a look at my edit. – Michael Perrenoud 20 hours ago
Just posted that answer myself. Looks like we came to the same conclusion, then :) – Jan Doerrenhaus 20 hours ago
check on my edit I added some Image – Clement 19 hours ago
@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. – Michael Perrenoud 19 hours ago
show 2 more comments

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.