is it possible to add DataRelation to DataSet if child table contains rows that have no parent in pa

Posted by matti on Stack Overflow See other posts from Stack Overflow or by matti
Published on 2010-03-25T16:07:45Z Indexed on 2010/03/25 16:13 UTC
Read the original article Hit count: 563

Filed under:
|

If I fill the DataSet with DataAdapters that select all rows from Orders and Customers and call:

private void CreateRelation() 
{
    // Get the DataColumn objects from two DataTable objects 
    // in a DataSet. Code to get the DataSet not shown here.
    DataColumn parentColumn = 
        DataSet1.Tables["Customers"].Columns["CustID"];
    DataColumn childColumn = 
        DataSet1.Tables["Orders"].Columns["CustID"];
    // Create DataRelation.
    DataRelation relCustOrder;
    relCustOrder = new DataRelation("CustomersOrders", 
        parentColumn, childColumn);
    // Add the relation to the DataSet.
    DataSet1.Relations.Add(relCustOrder);
}

(from http://msdn.microsoft.com/en-us/library/system.data.datarelation.aspx)

there will be a runtime error if there is orders that do not have customers. This might happen when a buggy program has not deleted customer's orders when customer was deleted.

What can I do except put Orders select string a additional where-condition:

CUSTID IN (SELECT DISTINCT CUSTID FROM CUSTOMERS)

OR: is it really that way (that all children have to have parents)? My code might have a bug also. The exception occurs when IN MY CODE I add the relation to filled DataSet. The exception is:

An unhandled exception of type 'System.ArgumentException' occurred in System.Data.dll

Additional information: This constraint cannot be enabled as not all values have corresponding parent values.

Thanks & Best Regards - Matti

© Stack Overflow or respective owner

Related posts about c#

Related posts about ADO.NET