Search code examples
c#datatable

DataTable returning empty


For my application there are a few separate dataTables and I need to create a new dataTable based on matching ids. I have to do the process a few times so I created a function so I'm not duplicating code, I've done this like so:

    private static DataTable CloneTable(DataTable originalTable, DataTable newTable, DataTable targetTable,
        string addedColumn, string columnToExtract, bool multipleConditions = false, string secondColumnName = null, string secondColumnConditon= null)
    {

        newTable = originalTable.Clone();
        newTable.Columns.Add(addedColumn);

        foreach (DataRow row in originalTable.Rows)
        {

            DataRow[] rowsTarget;

            if (multipleConditions == false)
            {
                rowsTarget = targetTable.Select(string.Format("ItemId='{0}'", Convert.ToString(row["ItemId"])));
            } else
            {
               rowsTarget = targetTable.Select(string.Format("ItemId='{0}' AND {1} ='{2}'", Convert.ToString(row["ItemId"]), secondColumnName, secondColumnConditon));
            }


            if (rowsTarget != null && rowsTarget.Length > 0)
            {
                string data = rowsTarget[0][columnToExtract].ToString();
                var lst = row.ItemArray.ToList();
                lst.Add(data);
                newTable.Rows.Add(lst.ToArray());
            }

            else
            {
                string data = "";
                var lst = row.ItemArray.ToList();
                lst.Add(data);
                newTable.Rows.Add(lst.ToArray());
            }

        }

            return newTable;

    }

I then call this in a separate function like so:

    private DataTable GetExtractData()
    {

.........................

            DataTable includeLastModified = new DataTable();
            DataTable includeFunction = new DataTable();
            DataTable includeDiscipline = new DataTable();


            CloneTable(itemsTable, includeLastModified, lastModifiedTable, "LastModifiedDate", "LastModifiedDate");
            CloneTable(includeLastModified, includeFunction, customPropertiesTable, "Function", "ItemTitle", true, "Title", "Function");
            CloneTable(includeFunction, includeDiscipline, customPropertiesTable, "Discipline", "ItemTable", true, "Title", "Discipline");

return includeDiscipline;


}

The issue I am having is that the dataTable here is returning empty and I am not sure why. In my CloneTable function I did the following to make sure that the new table is not empty:

        foreach (DataRow row in newTable.Rows)
        {
            foreach (var item in row.ItemArray)
            {
                Console.WriteLine(item);
            }
        }

It is not empty so I am not sure why when I'm returning it in a separate function it is now empty?

I call the same thing but for the includeDiscipline table in the GetData function but it comes back empty.

There are no errors but there is a message that comes and goes that says that the parameter "newTable" can be removed as the initial value isn't used. I'm not sure how that could be the case though as it is clearly being used?

I'm assuming that it is probably the way I am calling the function but I'm really not sure what it is that I have done wrong here


Solution

  • Okay face palm moment, just realised I forgot to assign it to something.

    So if I do something like:

    var test =  CloneTable(itemsTable, includeLastModified, lastModifiedTable, "LastModifiedDate", "LastModifiedDate");
    
    
    return test;
    

    It works fine and no longer returns empty