Adding Rows to a Typed DataSet from a Custom Query

July 12th, 2006

Yesterday I was looking to feed rows from a query generated DataSet into a strongly typed DataSet. I had not attempted this before. Generally with .NET 2.0 I use a DataObject and stored procedures which conform to the Properties of the object so that I can use a clever routine using reflection to automatically map values. But yesterday I was dealing with existing code which was making use of typed DataSets.

I created a new stored procedure which generated a DataSet with a DataTable which had all of the columns for the typed DataSet along with a few extras and I wanted to add those rows to my empty typed DataTable. At first I attempted to map each value manually but that proved to be cumbersome and almost impossible because at times the Guid values from the database could be NULL so I would have to address each instance. Finally I discovered the method DataTable.ImportRow. It works great!

private void FillCustomerTable(CustomerDataSet.CustomerDataTable
customerDataTable, DataRow[] rows)         {
List customerKeys = new List();
foreach (DataRow row in rows)
{
if (row["ItemUID"] != null &&
!customerKeys.Contains((Guid)row["CustomerUID"]))
{
customerDataTable.ImportRow(row);
customerKeys.Add((Guid)row["CustomerUID"]);
}
}
}

One Response to “Adding Rows to a Typed DataSet from a Custom Query”

  1. Dercsár Péter Says:

    I use similar code, except that I create a new row manually and import it to an empty datatable (the first item of a dropdown have to be 'please, select...'),
    and it does not work. There is no Exception thrown, but nothing happens. The datatable is still empty after ImportRow().
    Anybody knows why?