0

Trying to get parent id directly in child table , Is it possible to get parent id in child table by just using dsTest.Relations.Add() also tried by adding cascade.for cascade How to update Dataset Parent & Child tables with Autogenerated Identity Key?

DataSet dsTest = new DataSet("DataSet");

       DataTable parentTable = new DataTable("Parents");

        parentTable.Columns.Add("ParentId", typeof(int));

        parentTable.Columns.Add("ParentName", typeof(string));

        //Create some parents.

        parentTable.Rows.Add(new object[] { 1, "Parent # 1" });

        parentTable.Rows.Add(new object[] { 2, "Parent # 2" });

        parentTable.Rows.Add(new object[] { 3, "Parent # 3" });

        dsTest.Tables.Add(parentTable);


        // Table for childrend

        DataTable childTable = new DataTable("Childs");

        childTable.Columns.Add("ChildId", typeof(int));

        childTable.Columns.Add("ChildName", typeof(string));

        childTable.Columns.Add("ParentId", typeof(int));

        //Create some childs.

        childTable.Rows.Add(new object[] { 1, "Child # 1", });

        childTable.Rows.Add(new object[] { 2, "Child # 2", });

        childTable.Rows.Add(new object[] { 3, "Child # 3", });

        childTable.Rows.Add(new object[] { 4, "Child # 4", });

        childTable.Rows.Add(new object[] { 5, "Child # 5", });

        dsTest.Tables.Add(childTable);

        //// Create their relation.

        DataRelation parentChildRelation = new DataRelation("ParentChild", parentTable.Columns["ParentId"], childTable.Columns["ParentId"]);

        dsTest.Relations.Add(parentChildRelation);

        childTable.AcceptChanges();
Community
  • 1
  • 1
yash fale
  • 235
  • 1
  • 4
  • 19
  • No. There isn't enough information in either table to link the two tables together. When you create a child of any type you must add a link (or double link) between the parent and child or have a common field in both objects. – jdweng Dec 20 '16 at 08:55
  • ParentId is common in both table – yash fale Dec 20 '16 at 09:01
  • Yes, but in the sample code you did not fill in the column. My assumption was that you wanted to fill in the column, but with the data posted there was not enough info to fill in the column. – jdweng Dec 20 '16 at 10:09
  • do you mean i have to add new row of child table after data relation – yash fale Dec 20 '16 at 13:21
  • No. Just add the 3rd column from : new object[] { 1, "Child # 1", } To : new object[] { 1, "Child # 1", 1} – jdweng Dec 20 '16 at 14:27
  • actually i kept it blank bcz i want that ParentId to be filled in childTable where i kept blank , after data Relation , above example states that i have two data table which having data , in which parent table has parent id , and child is missing in child table , after data relation i want child id automatically filled with parent id – yash fale Dec 21 '16 at 09:12
  • You do not have enough info to fill in the child automatically with the code posted. – jdweng Dec 21 '16 at 09:25
  • i wan to achieve like this ( see sql server code) in last step author is adding 1 row to table and then oDetailsRow.SetParentRow(oOrderRow); oDS.Tables["OrderDetails"].Rows.Add(oDetailsRow); https://www.codeproject.com/Articles/3805/Inserting-relational-data-using-DataSet-and-DataAd, finaly id will be inserted into sql db , in this link author is addign one row at a time and sets SetParentRow , but in my case i have 2 tables which already hav data but id is not present in child so how can i get these ids in second child table – yash fale Dec 21 '16 at 09:38
  • You need to define a relationship as shown in the article. Right now there isn't a relationship between the 2 tables. – jdweng Dec 21 '16 at 10:27

0 Answers0