Note that there are some explanatory texts on larger screens.

plurals
  1. POis it possible to add DataRelation to DataSet if child table contains rows that have no parent in parent table in C# / ADO.NET 2.0
    text
    copied!<p>If I fill the DataSet with DataAdapters that select all rows from Orders and Customers and call:</p> <pre> 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); } </pre> <p>(from <a href="http://msdn.microsoft.com/en-us/library/system.data.datarelation.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/system.data.datarelation.aspx</a>)</p> <p>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.</p> <p>What can I do except put Orders select string a additional where-condition:</p> <pre> CUSTID IN (SELECT DISTINCT CUSTID FROM CUSTOMERS) </pre> <p>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:</p> <p>An unhandled exception of type 'System.ArgumentException' occurred in System.Data.dll</p> <p>Additional information: This constraint cannot be enabled as not all values have corresponding parent values.</p> <p>Thanks &amp; Best Regards - Matti</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload