PC Review


Reply
Thread Tools Rate Thread

Deleting a data set row with relationships (and just one DataTable!)

 
 
Jorge
Guest
Posts: n/a
 
      1st Aug 2003
Hello all!,

I have a dataset with just one DataTable : "titles" (using pubs
database). I want to delete a specific DataRow, the problem is that
"titles" is related with the table "sales" on the field "title_id",
and when I try to delete the record, ADO.NET throws me an exception
....

Dim table As DataTable = dataset.Tables("titles")
table.Rows(0).Delete()
dataadapter.Update(dataset, "titles") 'error!


I know I can to create DataRelations between DataTables, but in this
case I just have one DataTable. How can I delete the related records
too? Do I need to use a Command object before the Delete method??

I'm confused! ...

Any suggestion will be appreciated ...

Robert
 
Reply With Quote
 
 
 
 
Ian Phillips
Guest
Posts: n/a
 
      1st Aug 2003
You will need to do one of two things:

1. Cascade deletes in your database so that,when you update the database
with the dataset modifications, the child rows are deleted as well.

2. Add in the other, related tables into the dataset and add a cascade
delete rules for the dataset relations.

In the second case, you will need to make sure that the child tables are
updated to the database before you update the parent.

HTH,
ian


"Jorge" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello all!,
>
> I have a dataset with just one DataTable : "titles" (using pubs
> database). I want to delete a specific DataRow, the problem is that
> "titles" is related with the table "sales" on the field "title_id",
> and when I try to delete the record, ADO.NET throws me an exception
> ...
>
> Dim table As DataTable = dataset.Tables("titles")
> table.Rows(0).Delete()
> dataadapter.Update(dataset, "titles") 'error!
>
>
> I know I can to create DataRelations between DataTables, but in this
> case I just have one DataTable. How can I delete the related records
> too? Do I need to use a Command object before the Delete method??
>
> I'm confused! ...
>
> Any suggestion will be appreciated ...
>
> Robert



 
Reply With Quote
 
Jorge
Guest
Posts: n/a
 
      1st Aug 2003
Kathleen, thank you for your help. You misunderstood me (or my
explanation was not the best). I want delete a parent AND delete its
child records related too.

The "titles" table is the parent and "sales" is the child. I filled a
dataset with the content of the first one, my dataset just have ONE
DataTable.

When I use the Delete method, it launches an exception obviously
because first at all I need to delete its related records (on "sales"
table).

My question is: how can I delete the related records on the "sales"
table? Do I need to use a Command?

Regards!


"Kathleen Dollard" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> Jorge,
>
> You are trying to break relational rules in your database. It's suppose to
> raise an error and stop you from doing that. It is not clear why you want to
> delete a parent and leave its child.
>
> --
> Kathleen (MVP-VB)

 
Reply With Quote
 
Kathleen Dollard
Guest
Posts: n/a
 
      1st Aug 2003
Jorge,

I'd do it with a stored procedure, but if that's not a good solution for
you...

You can use the RowUpdatign event to create a new command with a SQL
statement that delete the children. Or you could cycle through the DataSet
prior to the update, and again, create a command iwth a SQL statement to
delete each set of children. Short of cascading deletes on your server, and
I'd be hesitant to do that, I think those are about yoru only options.

--
Kathleen (MVP-VB)



"Jorge" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Kathleen, thank you for your help. You misunderstood me (or my
> explanation was not the best). I want delete a parent AND delete its
> child records related too.
>
> The "titles" table is the parent and "sales" is the child. I filled a
> dataset with the content of the first one, my dataset just have ONE
> DataTable.
>
> When I use the Delete method, it launches an exception obviously
> because first at all I need to delete its related records (on "sales"
> table).
>
> My question is: how can I delete the related records on the "sales"
> table? Do I need to use a Command?
>
> Regards!
>
>
> "Kathleen Dollard" <(E-Mail Removed)> wrote in message

news:<(E-Mail Removed)>...
> > Jorge,
> >
> > You are trying to break relational rules in your database. It's suppose

to
> > raise an error and stop you from doing that. It is not clear why you

want to
> > delete a parent and leave its child.
> >
> > --
> > Kathleen (MVP-VB)



 
Reply With Quote
 
Ian Phillips
Guest
Posts: n/a
 
      2nd Aug 2003
If you are unwilling to load the tables into the dataset, you will have to
add the logic into the database itself. You can do that with a cascading
delete rule as discussed in option 1 of my initial post.

If you want all your cascading to be done in the app and not the RDBMS, then
you will have to at least load the rows for the records that will have to be
deleted. you can put a select clause into the sql for the selectCommand so
only the rows you are going to delete will be loaded. You need to update
the sales table before you update the titles table in this process.

HTH,

ian

"Jorge" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Thanks Ian,
>
> I will try the second option but, can i do that if my data set has
> only one data table? I wouldn't like to load the "sales" table on the
> data set (or is required?)
>
> I have tried de FillSchema method to create just the schema of the
> "sales" table, with no data rows. After that I created a data relation
> but my code still not working. This is a part of my code :
>
> ----------
> adapter = New SqlDataAdapter("SELECT * FROM titles", conexion)
> cmdBuilder = New SqlCommandBuilder(adapter)
>
> dset = New DataSet()
> adapter.Fill(dset, "titles")
>
> adapter.FillSchema(dset, SchemaType.Source, "sales")
>
> dset.Relations.Add("Titles_Sales", _
> dset.Tables("titles").Columns("title_id"), _
> dset.Tables("sales").Columns("title_id"))
> ----------
>
> Then when I delete a data row here, a exception is thrown :
>
> dset.Tables(0).Rows(index).Delete()
> adapter.Update(dset, "titles")
>
>
> Regards!
>
>
> "Ian Phillips" <(E-Mail Removed)> wrote in message

news:<2EsWa.4$(E-Mail Removed)>...
> > You will need to do one of two things:
> >
> > 1. Cascade deletes in your database so that,when you update the database
> > with the dataset modifications, the child rows are deleted as well.
> >
> > 2. Add in the other, related tables into the dataset and add a cascade
> > delete rules for the dataset relations.
> >
> > In the second case, you will need to make sure that the child tables are
> > updated to the database before you update the parent.
> >
> > HTH,
> > ian
> >
> >
> > "Jorge" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> > > Hello all!,
> > >
> > > I have a dataset with just one DataTable : "titles" (using pubs
> > > database). I want to delete a specific DataRow, the problem is that
> > > "titles" is related with the table "sales" on the field "title_id",
> > > and when I try to delete the record, ADO.NET throws me an exception
> > > ...
> > >
> > > Dim table As DataTable = dataset.Tables("titles")
> > > table.Rows(0).Delete()
> > > dataadapter.Update(dataset, "titles") 'error!
> > >
> > >
> > > I know I can to create DataRelations between DataTables, but in this
> > > case I just have one DataTable. How can I delete the related records
> > > too? Do I need to use a Command object before the Delete method??
> > >
> > > I'm confused! ...
> > >
> > > Any suggestion will be appreciated ...
> > >
> > > Robert



 
Reply With Quote
 
Ian Phillips
Guest
Posts: n/a
 
      2nd Aug 2003
BTW...

You will also have to cascade the delete into the titleAuthor and roysched
tables.

ian

"Ian Phillips" <(E-Mail Removed)> wrote in message
news:BhPWa.14$(E-Mail Removed)...
> If you are unwilling to load the tables into the dataset, you will have to
> add the logic into the database itself. You can do that with a cascading
> delete rule as discussed in option 1 of my initial post.
>
> If you want all your cascading to be done in the app and not the RDBMS,

then
> you will have to at least load the rows for the records that will have to

be
> deleted. you can put a select clause into the sql for the selectCommand

so
> only the rows you are going to delete will be loaded. You need to update
> the sales table before you update the titles table in this process.
>
> HTH,
>
> ian
>
> "Jorge" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > Thanks Ian,
> >
> > I will try the second option but, can i do that if my data set has
> > only one data table? I wouldn't like to load the "sales" table on the
> > data set (or is required?)
> >
> > I have tried de FillSchema method to create just the schema of the
> > "sales" table, with no data rows. After that I created a data relation
> > but my code still not working. This is a part of my code :
> >
> > ----------
> > adapter = New SqlDataAdapter("SELECT * FROM titles", conexion)
> > cmdBuilder = New SqlCommandBuilder(adapter)
> >
> > dset = New DataSet()
> > adapter.Fill(dset, "titles")
> >
> > adapter.FillSchema(dset, SchemaType.Source, "sales")
> >
> > dset.Relations.Add("Titles_Sales", _
> > dset.Tables("titles").Columns("title_id"), _
> > dset.Tables("sales").Columns("title_id"))
> > ----------
> >
> > Then when I delete a data row here, a exception is thrown :
> >
> > dset.Tables(0).Rows(index).Delete()
> > adapter.Update(dset, "titles")
> >
> >
> > Regards!
> >
> >
> > "Ian Phillips" <(E-Mail Removed)> wrote in message

> news:<2EsWa.4$(E-Mail Removed)>...
> > > You will need to do one of two things:
> > >
> > > 1. Cascade deletes in your database so that,when you update the

database
> > > with the dataset modifications, the child rows are deleted as well.
> > >
> > > 2. Add in the other, related tables into the dataset and add a cascade
> > > delete rules for the dataset relations.
> > >
> > > In the second case, you will need to make sure that the child tables

are
> > > updated to the database before you update the parent.
> > >
> > > HTH,
> > > ian
> > >
> > >
> > > "Jorge" <(E-Mail Removed)> wrote in message
> > > news:(E-Mail Removed)...
> > > > Hello all!,
> > > >
> > > > I have a dataset with just one DataTable : "titles" (using pubs
> > > > database). I want to delete a specific DataRow, the problem is that
> > > > "titles" is related with the table "sales" on the field "title_id",
> > > > and when I try to delete the record, ADO.NET throws me an exception
> > > > ...
> > > >
> > > > Dim table As DataTable = dataset.Tables("titles")
> > > > table.Rows(0).Delete()
> > > > dataadapter.Update(dataset, "titles") 'error!
> > > >
> > > >
> > > > I know I can to create DataRelations between DataTables, but in this
> > > > case I just have one DataTable. How can I delete the related records
> > > > too? Do I need to use a Command object before the Delete method??
> > > >
> > > > I'm confused! ...
> > > >
> > > > Any suggestion will be appreciated ...
> > > >
> > > > Robert

>
>



 
Reply With Quote
 
Kathleen Dollard
Guest
Posts: n/a
 
      2nd Aug 2003
Ian,

Those are certainly two valid options, but they are not the only options.
You can issue explicit deletes via stored proc or TSQL for the chlidren
before each parent record is deleted.

--
Kathleen (MVP-VB)



"Ian Phillips" <(E-Mail Removed)> wrote in message
news:BhPWa.14$(E-Mail Removed)...
> If you are unwilling to load the tables into the dataset, you will have to
> add the logic into the database itself. You can do that with a cascading
> delete rule as discussed in option 1 of my initial post.
>
> If you want all your cascading to be done in the app and not the RDBMS,

then
> you will have to at least load the rows for the records that will have to

be
> deleted. you can put a select clause into the sql for the selectCommand

so
> only the rows you are going to delete will be loaded. You need to update
> the sales table before you update the titles table in this process.
>
> HTH,
>
> ian
>
> "Jorge" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > Thanks Ian,
> >
> > I will try the second option but, can i do that if my data set has
> > only one data table? I wouldn't like to load the "sales" table on the
> > data set (or is required?)
> >
> > I have tried de FillSchema method to create just the schema of the
> > "sales" table, with no data rows. After that I created a data relation
> > but my code still not working. This is a part of my code :
> >
> > ----------
> > adapter = New SqlDataAdapter("SELECT * FROM titles", conexion)
> > cmdBuilder = New SqlCommandBuilder(adapter)
> >
> > dset = New DataSet()
> > adapter.Fill(dset, "titles")
> >
> > adapter.FillSchema(dset, SchemaType.Source, "sales")
> >
> > dset.Relations.Add("Titles_Sales", _
> > dset.Tables("titles").Columns("title_id"), _
> > dset.Tables("sales").Columns("title_id"))
> > ----------
> >
> > Then when I delete a data row here, a exception is thrown :
> >
> > dset.Tables(0).Rows(index).Delete()
> > adapter.Update(dset, "titles")
> >
> >
> > Regards!
> >
> >
> > "Ian Phillips" <(E-Mail Removed)> wrote in message

> news:<2EsWa.4$(E-Mail Removed)>...
> > > You will need to do one of two things:
> > >
> > > 1. Cascade deletes in your database so that,when you update the

database
> > > with the dataset modifications, the child rows are deleted as well.
> > >
> > > 2. Add in the other, related tables into the dataset and add a cascade
> > > delete rules for the dataset relations.
> > >
> > > In the second case, you will need to make sure that the child tables

are
> > > updated to the database before you update the parent.
> > >
> > > HTH,
> > > ian
> > >
> > >
> > > "Jorge" <(E-Mail Removed)> wrote in message
> > > news:(E-Mail Removed)...
> > > > Hello all!,
> > > >
> > > > I have a dataset with just one DataTable : "titles" (using pubs
> > > > database). I want to delete a specific DataRow, the problem is that
> > > > "titles" is related with the table "sales" on the field "title_id",
> > > > and when I try to delete the record, ADO.NET throws me an exception
> > > > ...
> > > >
> > > > Dim table As DataTable = dataset.Tables("titles")
> > > > table.Rows(0).Delete()
> > > > dataadapter.Update(dataset, "titles") 'error!
> > > >
> > > >
> > > > I know I can to create DataRelations between DataTables, but in this
> > > > case I just have one DataTable. How can I delete the related records
> > > > too? Do I need to use a Command object before the Delete method??
> > > >
> > > > I'm confused! ...
> > > >
> > > > Any suggestion will be appreciated ...
> > > >
> > > > Robert

>
>



 
Reply With Quote
 
Ian Phillips
Guest
Posts: n/a
 
      4th Aug 2003
That is true....

But you can also set up explicit triggers for OnDelete events or you can
.....

I was just giving the two easiest methods. Now, don't get me wrong, I think
that using stored procedures is important whenever interacting with a
database management system to pull data. It is faster than sql delete
statements and it removes the risk of a malicious user imbedding sql into
your query. But, even when using a stored procedure to delete records from
a parent table, I have found that having the relation set up to
automatically delete child rows is faster and is, IMHO, better design. When
you have strict composition relationships, it only makes sense to cascade
updates and deletes.

ian

"Kathleen Dollard" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Ian,
>
> Those are certainly two valid options, but they are not the only options.
> You can issue explicit deletes via stored proc or TSQL for the chlidren
> before each parent record is deleted.
>
> --
> Kathleen (MVP-VB)
>
>
>
> "Ian Phillips" <(E-Mail Removed)> wrote in message
> news:BhPWa.14$(E-Mail Removed)...
> > If you are unwilling to load the tables into the dataset, you will have

to
> > add the logic into the database itself. You can do that with a

cascading
> > delete rule as discussed in option 1 of my initial post.
> >
> > If you want all your cascading to be done in the app and not the RDBMS,

> then
> > you will have to at least load the rows for the records that will have

to
> be
> > deleted. you can put a select clause into the sql for the selectCommand

> so
> > only the rows you are going to delete will be loaded. You need to

update
> > the sales table before you update the titles table in this process.
> >
> > HTH,
> >
> > ian
> >
> > "Jorge" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> > > Thanks Ian,
> > >
> > > I will try the second option but, can i do that if my data set has
> > > only one data table? I wouldn't like to load the "sales" table on the
> > > data set (or is required?)
> > >
> > > I have tried de FillSchema method to create just the schema of the
> > > "sales" table, with no data rows. After that I created a data relation
> > > but my code still not working. This is a part of my code :
> > >
> > > ----------
> > > adapter = New SqlDataAdapter("SELECT * FROM titles", conexion)
> > > cmdBuilder = New SqlCommandBuilder(adapter)
> > >
> > > dset = New DataSet()
> > > adapter.Fill(dset, "titles")
> > >
> > > adapter.FillSchema(dset, SchemaType.Source, "sales")
> > >
> > > dset.Relations.Add("Titles_Sales", _
> > > dset.Tables("titles").Columns("title_id"), _
> > > dset.Tables("sales").Columns("title_id"))
> > > ----------
> > >
> > > Then when I delete a data row here, a exception is thrown :
> > >
> > > dset.Tables(0).Rows(index).Delete()
> > > adapter.Update(dset, "titles")
> > >
> > >
> > > Regards!
> > >
> > >
> > > "Ian Phillips" <(E-Mail Removed)> wrote in message

> > news:<2EsWa.4$(E-Mail Removed)>...
> > > > You will need to do one of two things:
> > > >
> > > > 1. Cascade deletes in your database so that,when you update the

> database
> > > > with the dataset modifications, the child rows are deleted as well.
> > > >
> > > > 2. Add in the other, related tables into the dataset and add a

cascade
> > > > delete rules for the dataset relations.
> > > >
> > > > In the second case, you will need to make sure that the child tables

> are
> > > > updated to the database before you update the parent.
> > > >
> > > > HTH,
> > > > ian
> > > >
> > > >
> > > > "Jorge" <(E-Mail Removed)> wrote in message
> > > > news:(E-Mail Removed)...
> > > > > Hello all!,
> > > > >
> > > > > I have a dataset with just one DataTable : "titles" (using pubs
> > > > > database). I want to delete a specific DataRow, the problem is

that
> > > > > "titles" is related with the table "sales" on the field

"title_id",
> > > > > and when I try to delete the record, ADO.NET throws me an

exception
> > > > > ...
> > > > >
> > > > > Dim table As DataTable = dataset.Tables("titles")
> > > > > table.Rows(0).Delete()
> > > > > dataadapter.Update(dataset, "titles") 'error!
> > > > >
> > > > >
> > > > > I know I can to create DataRelations between DataTables, but in

this
> > > > > case I just have one DataTable. How can I delete the related

records
> > > > > too? Do I need to use a Command object before the Delete method??
> > > > >
> > > > > I'm confused! ...
> > > > >
> > > > > Any suggestion will be appreciated ...
> > > > >
> > > > > Robert

> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
deleting relationships =?Utf-8?B?UGF0dGlQ?= Microsoft Access Database Table Design 4 24th Feb 2006 08:21 PM
Creating multiple columns in a DataTable based on Relationships Kevin Schneider Microsoft ADO .NET 0 22nd Jul 2004 11:49 PM
DataSet/DataTable relationships question =?Utf-8?B?TWFyaw==?= Microsoft ADO .NET 4 19th May 2004 10:20 AM
Deleting Relationships? Mark Microsoft Access Database Table Design 6 26th Feb 2004 05:08 PM
VBA Deleting Relationships Jim Microsoft Access Database Table Design 3 2nd Oct 2003 12:31 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:08 PM.