DataSet Question (cascading inserts)

D

dm_dal

I have a typed dataset with two tables ( Table1 and Table2 ). I'm not
trying to update the actual db tables at this point, just wanting to see the
results in the DataGrid.

Column are:
Table1.ID (int)
Table1.cell1 (string)
Table1.cell2 (string)
Table1.cell3 (string)

Table2.ID (int)
Table2.cell1 (string)
Table2.data1 (string)
Table2.data2 (string)
Table2.data3 (string)

The keys for both tables are the ID column and the cell1 column. and a
parent/child relationship is defined, with Table1 as the parent and Table2
as the child. The UpdateRule is set to "Cascade".

When I insert a new row into Table1, I would expect that a new row is
inserted into Table2, with the parent column values from the ID and cell 1
columns propogated to Table2, but, when I check Table2, there are no rows.

Here's the code:
public class WebForm1 : System.Web.UI.Page

{

protected DAL.TestDataSet ds;

protected System.Web.UI.WebControls.DataGrid DataGrid1;


private void Page_Load(object sender, System.EventArgs e)

{

// Put user code to initialize the page here

InitDataSet();

BindGrid();

}

private void InitDataSet()

{

ds = new DAL.TestDataSet();

DAL.TestDataSet.TABLE1Row row = ds.TABLE1.AddTABLE1Row(30,"data 1","data
2","data 3");

ds.AcceptChanges();

}

private void BindGrid()

{

DataGrid1.DataSource = ds.TABLE2;

DataGrid1.DataBind();

}
 
D

David Browne

dm_dal said:
I have a typed dataset with two tables ( Table1 and Table2 ). I'm not
trying to update the actual db tables at this point, just wanting to see the
results in the DataGrid.

Column are:
Table1.ID (int)
Table1.cell1 (string)
Table1.cell2 (string)
Table1.cell3 (string)

Table2.ID (int)
Table2.cell1 (string)
Table2.data1 (string)
Table2.data2 (string)
Table2.data3 (string)

The keys for both tables are the ID column and the cell1 column. and a
parent/child relationship is defined, with Table1 as the parent and Table2
as the child. The UpdateRule is set to "Cascade".

When I insert a new row into Table1, I would expect that a new row is
inserted into Table2, with the parent column values from the ID and cell 1
columns propogated to Table2, but, when I check Table2, there are no rows.

Why would it do that. If Table1 was, say, customers and Table2 was, say,
orders, why should adding a customer automatically create an order? What
would the values of Table2.data1, .data2 and .data3 be?

If you want a new row in Table2, you must add one.

David
 
D

dm_dal

We'll when you think about it in terms of Customers/Orders, your right, it
doesn't make sense. However, when you think about it in terms of
Invoice/InvoiceItem it does. As each Invoice must have an InvoiceItem (or
detail line).

The scenario I'm working in, the "Table1" is the parent table of "Table2".
Each Table2 entry must have a corresponding Table1 entry, and each Table1
entry must have a corresponding Table2 entry. The PK columns from Table1
must be cascaded down to Table2.

Anytime a new Table1 row is created, a new Table2 row must be created. If
this can't be achieved through the relationship, then I know of other ways,
but I thought it would work this way

David
 
D

dm_dal

FYI - I've figured it out.

After creating/populating a new Table1 row, I need to create a new empty
Table2 row and then set the Table1 row as it's parent.

DAL.TestDataSet.TABLE1Row parentRow = ds.TABLE1.AddTABLE1Row(value, value,
value, value);
DAL.TestDataSet.TABLE2Row childRow = ds.TABLE2.NewTABLE2Row();
childRow.SetParentRow(parentRow);
ds.TABLE2.Rows.Add(childRow);

That will create a new Table2 row, and automatically populate it's FK
columns with the values for the parent tables PK columns.

Thanks anyway.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top