Error: “The row doesn’t belong to the same DataSet as this relation”

This .NET error message is misleading (although in the long run, it is still correct) and brings up nothing at all in Google, so I am going to put it here again: “The row doesn’t belong to the same DataSet as this relation”. Miseleading because I was focused on the row.

This is the System.ArgumentException error that will be thrown when you attempt to either XMLWrite a DataSet with tables that are related through a DataRelation, or when you attempt to GetChildRows on one of the rows of the parent table.

The error will happen if you have forgotten a key line of code in setting up a DataRelation  — that is to attach the DataRelation that you have just created to the DataSet.

Dim ds As DataSet = GetSomeData()
Dim rel As DataRelation = New DataRelation(“sitestodocs”, ds.Tables(0).Columns(“ID”), ds.Tables(1).Columns(“ParentID”))
rel.Nested =
True
ds.Relations.Add(rel)   <– this is the likely missing code!
Dim dr as DataRow=ds.Tables(0).Rows(0)
dim tbl as DataTable=dr.GetChildRows(rel)

This is for VS2003. The documentation says that the exception’s message is “”The relation and row do not belong to the same table”. I wonder if that would have shortened the time that it took me to figure out my really stoooopid mistake. So hopefully, this little bit of google text will help the next person who is having a similar brainf*rt.

www.acehaid.org

  Sign up for my newsletter so you don't miss my conference & Pluralsight course announcements!  

6 thoughts on “Error: “The row doesn’t belong to the same DataSet as this relation”

Leave a Reply to Engle Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.