By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,702 Members | 1,432 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,702 IT Pros & Developers. It's quick & easy.

Q: Relationships and exceptions

P: n/a
Hi

Can anybody explain to me what I'm doing wrong or misunderstood in the
following:

I have two tables, for example, customers and orders. I have loaded these
into a dataset. I have also created a relation between these two tables
using the Relation keyword e.g.

ds.Relationships.Add("Customers_Orders", _
ds.Tables("Customers").Columns("CustomerID"), _
ds.Tables("Orders").Columns("CustomerID"), True)

I populate both tables with data.

Everything works ok. However, when I choose a row from the Customers tables
and try to delete it as follows:

Try
row.Delete() ' where row has child rows in the Orders table
Catch ex Exception
MessageBox(ex.Message)
End Try

The thing is that no exception is thrown!!! I was expecting one to be
thrown. Have I missed something obvious?

Thanks for any help in advance

Geoff
Feb 11 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi,

"Geoff" <no********@email.com> wrote in message
news:95******************************@pipex.net...
Hi

Can anybody explain to me what I'm doing wrong or misunderstood in the
following:

I have two tables, for example, customers and orders. I have loaded these
into a dataset. I have also created a relation between these two tables
using the Relation keyword e.g.

ds.Relationships.Add("Customers_Orders", _
ds.Tables("Customers").Columns("CustomerID"), _
ds.Tables("Orders").Columns("CustomerID"), True)

I populate both tables with data.

Everything works ok. However, when I choose a row from the Customers
tables and try to delete it as follows:

Try
row.Delete() ' where row has child rows in the Orders table
Catch ex Exception
MessageBox(ex.Message)
End Try

The thing is that no exception is thrown!!! I was expecting one to be
thrown. Have I missed something obvious?
When you add a DataRelation, then there's also a ForeignKeyConstraint added
to the child table. The ForeignKeyConstraint has a property 'DeleteRule'.
By default that property is set to Rule.Cascade. So when you delete a
parent row, child rows are deleted too and therefore there is no reason for
an Exception.

You can change this behaviour, eg. :

ds.Relations("Customers_Orders").ChildKeyConstrain t.DeleteRule = Rule.None

If you delete a parent row now and the parent has child rows, it will throw
an exception, because the child rows would become orphaned.
HTH,
Greetings


Thanks for any help in advance

Geoff

Feb 11 '06 #2

P: n/a
Thanks Bart!

"Bart Mermuys" <bm*************@hotmail.com> wrote in message
news:OU*************@TK2MSFTNGP15.phx.gbl...
Hi,

"Geoff" <no********@email.com> wrote in message
news:95******************************@pipex.net...
Hi

Can anybody explain to me what I'm doing wrong or misunderstood in the
following:

I have two tables, for example, customers and orders. I have loaded these
into a dataset. I have also created a relation between these two tables
using the Relation keyword e.g.

ds.Relationships.Add("Customers_Orders", _
ds.Tables("Customers").Columns("CustomerID"), _
ds.Tables("Orders").Columns("CustomerID"), True)

I populate both tables with data.

Everything works ok. However, when I choose a row from the Customers
tables and try to delete it as follows:

Try
row.Delete() ' where row has child rows in the Orders table
Catch ex Exception
MessageBox(ex.Message)
End Try

The thing is that no exception is thrown!!! I was expecting one to be
thrown. Have I missed something obvious?


When you add a DataRelation, then there's also a ForeignKeyConstraint
added to the child table. The ForeignKeyConstraint has a property
'DeleteRule'. By default that property is set to Rule.Cascade. So when
you delete a parent row, child rows are deleted too and therefore there is
no reason for an Exception.

You can change this behaviour, eg. :

ds.Relations("Customers_Orders").ChildKeyConstrain t.DeleteRule = Rule.None

If you delete a parent row now and the parent has child rows, it will
throw an exception, because the child rows would become orphaned.
HTH,
Greetings


Thanks for any help in advance

Geoff


Feb 12 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.