Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top