GetChildRows returns zero datarows RRS feed

  • Question

  • Hi

    I'm developing a project where I want to check if I have any childrows in my database before deleting a record. When I use the GetChildRows method, it return no records. I've discovered that it will not return any records when i haven't done a fill of the datatable containing the possible childrows. It seems a bit strange to me to fill all my tables in my dataset before i can use the GetCHildRows-method.

    I even considered filling the datatables based on the datarelation. I can get a ChildTable from it, but i'm not able to get the associated dataadapter/tableadapter. So it's a dead end. I'd like to use the datarelations to get all possible ChildRows from all possible datatables.

    Now i'm wondering what's the use of the GetChildRows-method if i have to fill my datatable first with the possible childrows. Do i really have to fill all my datatables with possible childrows?

    I'm looking of an answer on
    • how the GetChildRows-method really works.
    • how i can get the dataadapter/tableadapter from a datatable
    • If i better use LINQ or create queries to find related records
    Below my function in my class   

    Public Function HasRelations(ByVal LeveranciersRow As tblLeveranciersRow) As Boolean
            Dim blnHasRelations As Boolean = False

            For Each myRelation In LeveranciersRow.Table.ChildRelations
                 If LeveranciersRow.GetChildRows(CType(myRelation, DataRelation)).Count > 0 Then
                    blnHasRelations = True
                End If

            Return blnHasRelations

    End Function

    Tuesday, February 16, 2010 12:26 AM


  • Hi Frank

    I think we have to understand that what you have in the database is different from what you have in the dataset.
    The dataset is a disconnected (and potentially different) representation of the data you have in the database.

    If you for example have a table in the dataset on the client, then if you add a row in the database it will not
    automatically end up on the client unless you refill/update the dataset on the client.
    Or if you fill your parent table (on client) and then you fill the child table (on client) with a WHERE filter
    it may be that in the database you have x number of childrows, but on client you will have y number of child rows (thanks to the where clause).

    So it makes sense that you need to fill the both the parent and the child tables on the client.

    In short, GetChildRows will return the number of childrows in the disconnected dataset, not what is in the database.
    That is why you need to fill both parent and child tables.

    If you use LINQ, then the it will get the child rows for you, but this is because it will query for the child rows for each parent.

    create table Parent(pid int primary key, ctxt nvarchar(10))
    create table Child(cid int primary key, pid int, ctxt nvarchar(10))
    alter table Child add constraint FK_Child_Parent foreign key(pid) references Parent(pid)
    insert into Parent values (1, 'PRow 1')
    insert into Parent values (2, 'PRow 2')
    insert into Child values (1, 1, 'CRow 1')
    insert into Child values (2, 1, 'CRow 2')
    insert into Child values (3, 2, 'CRow 3')
    insert into Child values (4, 2, 'CRow 4')
    insert into Child values (5, 1, 'CRow 5')


                // Dataset (create xsd)
                MyDs ds = new MyDs();
                MyDsTableAdapters.ParentTableAdapter pa = new ConsoleApplication1.MyDsTableAdapters.ParentTableAdapter();
                // Must fill the child table as well.
                MyDsTableAdapters.ChildTableAdapter ca = new ConsoleApplication1.MyDsTableAdapters.ChildTableAdapter();
                foreach (DataRow r in ds.Parent)
                    DataRow[] crows = r.GetChildRows("FK_Child_Parent");
                    Console.WriteLine("RowText: {0}, Count {1}", r["ctxt"], crows.Count<DataRow>());
                //LINQ (create dbml)
                MyLinqDataContext dc = new MyLinqDataContext();
                var parent = from p in dc.Parents select p;
                foreach (var p in parent)
                    Console.WriteLine("RowText: {0}, Count {1}", p.ctxt, p.Childs.Count);

    Hope this makes sense


    This posting is provided "AS IS" with no warranties.
    Wednesday, February 17, 2010 1:11 PM