468,720 Members | 1,699 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,720 developers. It's quick & easy.

Deleting a Row from the Data Source with Table Adapter

Hi,
Trying to pass along a table row delete to the datasource, but I'm
crashing. Here is the code:

Private Sub btnDeleteIngr_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnDeleteIngr.Click
Dim vbResponse As Integer
vbResponse = MessageBox.Show("Are you sure that you want to
delete " + tbIngr.Text + "?", "Confirm Delete",
MessageBoxButtons.OKCancel, MessageBoxIcon.Warning)
If vbResponse = 1 Then
Dim dr As DataRow
Dim drv As DataRowView
drv = bsIngredients.Current
dr = drv.Row
dr.Delete()
bsIngredients.MoveNext()
taIngredients.Update(dsMenuPlanner.Ingredients)
dsMenuPlanner.AcceptChanges()
End If
End Sub

It's the "taIngredients.Update(dsMenuPlanner.Ingredient s)" line that
crashes. The error is "Update requires a valid DeleteCommand when
passed DataRow collection with deleted rows."

I can't figure out how to resolve this. Can anybody help?

Thanks,
Randy

Apr 22 '07 #1
12 18716
Your data adapter must have InsertCommand, DeleteCommand, and UpdateCommand
objects defined for it in order to perform updates.

Robin S.
--------------------------------
"Randy" <sp***********@gmail.comwrote in message
news:11**********************@p77g2000hsh.googlegr oups.com...
Hi,
Trying to pass along a table row delete to the datasource, but I'm
crashing. Here is the code:

Private Sub btnDeleteIngr_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnDeleteIngr.Click
Dim vbResponse As Integer
vbResponse = MessageBox.Show("Are you sure that you want to
delete " + tbIngr.Text + "?", "Confirm Delete",
MessageBoxButtons.OKCancel, MessageBoxIcon.Warning)
If vbResponse = 1 Then
Dim dr As DataRow
Dim drv As DataRowView
drv = bsIngredients.Current
dr = drv.Row
dr.Delete()
bsIngredients.MoveNext()
taIngredients.Update(dsMenuPlanner.Ingredients)
dsMenuPlanner.AcceptChanges()
End If
End Sub

It's the "taIngredients.Update(dsMenuPlanner.Ingredient s)" line that
crashes. The error is "Update requires a valid DeleteCommand when
passed DataRow collection with deleted rows."

I can't figure out how to resolve this. Can anybody help?

Thanks,
Randy

Apr 22 '07 #2
Randy,

I have somehow the idea that your update should be
taIngredients.Update(dsMenuPlanner.bsIngredients)

If it is not that, and it is as Robin suggest, add than before that row
cmb = oledbCommandbuilder(taIngredients)
or
SQLCommandBuilder(taIngredients)

'depending on what you use
http://msdn2.microsoft.com/en-us/lib...ndbuilder.aspx

I hope this helps,

Cor
"Randy" <sp***********@gmail.comschreef in bericht
news:11**********************@p77g2000hsh.googlegr oups.com...
Hi,
Trying to pass along a table row delete to the datasource, but I'm
crashing. Here is the code:

Private Sub btnDeleteIngr_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnDeleteIngr.Click
Dim vbResponse As Integer
vbResponse = MessageBox.Show("Are you sure that you want to
delete " + tbIngr.Text + "?", "Confirm Delete",
MessageBoxButtons.OKCancel, MessageBoxIcon.Warning)
If vbResponse = 1 Then
Dim dr As DataRow
Dim drv As DataRowView
drv = bsIngredients.Current
dr = drv.Row
dr.Delete()
bsIngredients.MoveNext()
taIngredients.Update(dsMenuPlanner.Ingredients)
dsMenuPlanner.AcceptChanges()
End If
End Sub

It's the "taIngredients.Update(dsMenuPlanner.Ingredient s)" line that
crashes. The error is "Update requires a valid DeleteCommand when
passed DataRow collection with deleted rows."

I can't figure out how to resolve this. Can anybody help?

Thanks,
Randy

Apr 22 '07 #3
Thank you both for your advice. I thought that I had a DeleteCommand
set up, but I did it wrong. I did it in the table adapter on the
designer rather than in the properties. That's now fixed and the code
executes without error.

I still have one problem. I haven't set up the CommandText
correctly. What I have in there is "DELETE FROM Ingredients", which
obviously deletes all records from the table Ingredients. Can you
help me to change this query to just delete the table that the user
has selected? It's always going to be the current row. The code
above has not changed. Let me know if I can provide more information.

Thanks again!
Randy
Apr 22 '07 #4
Can you help me to change this query to just delete the table that the user
has selected?
Sorry, I meant the row that the user has selected (not the table).

Apr 22 '07 #5
Randy,

That depends how the user has selected it, I know at least 10 simple ways.

Cor

"Randy" <sp***********@gmail.comschreef in bericht
news:11*********************@o5g2000hsb.googlegrou ps.com...
>Can you help me to change this query to just delete the table that the
user
has selected?

Sorry, I meant the row that the user has selected (not the table).

Apr 23 '07 #6
Using the binding navigator, the user scrolls through the records in
the table. If they see one that they don't want any longer, they
click a button intended to delete the current row from the data
table. Very straightforward from a user perspective, and I think is
pretty simple programatically. I just don't know enough to write the
SQL command so that it just deletes the current row and not all of the
records.

Thanks for any advice.
Randy

Apr 23 '07 #7
Randy,

If it is a row in a table, than the row will be marked as deleted. (it will
not deleted, if you want that, than you have to remove it, but than it will
not be deleted from yourdatabase).

It will not be showed anymore, however it is still there, the same is for an
updated and a new record, they are marked as updated and new. It is clever
done, if you delete a row and afterwards insert in your idea a new one, the
rowstate is set to "update".

When you now give your datatable to the database using the dataadapter or
tableadapter, than select those the rowstate which are changed, those will
be processed in the database and automatically is done by the adapter, the
acceptchanges which is to set the rowstates to all changes done.

This is one of the methods to do your updates, I hope it gives an idea,

Cor


"Randy" <sp***********@gmail.comschreef in bericht
news:11**********************@l77g2000hsb.googlegr oups.com...
Using the binding navigator, the user scrolls through the records in
the table. If they see one that they don't want any longer, they
click a button intended to delete the current row from the data
table. Very straightforward from a user perspective, and I think is
pretty simple programatically. I just don't know enough to write the
SQL command so that it just deletes the current row and not all of the
records.

Thanks for any advice.
Randy

Apr 23 '07 #8
As Cor said, if he deletes one, it is marked as a delete and no longer
displayed. Usually you call your tableadapter or dataadapter after the user
has completed all changes. The adapter checks the state of each row. If it
was Added, it executes the Insert command; if it was Modified, it executes
the Update command; if it was Delete, it executes the Delete command. The
delete command should be something like "DELETE FROM myTable WHERE
myPrimaryKey = @myPrimaryKey", and you pass in the parameter.

I don't use table adapters; they're not precise enough for me. I write my
own stored procs and execute my own code against them. Are you actually
using a strongly typed dataset and table adapter, or are you using a data
adapter and regular dataset?

Robin S.
--------------------------
"Randy" <sp***********@gmail.comwrote in message
news:11**********************@l77g2000hsb.googlegr oups.com...
Using the binding navigator, the user scrolls through the records in
the table. If they see one that they don't want any longer, they
click a button intended to delete the current row from the data
table. Very straightforward from a user perspective, and I think is
pretty simple programatically. I just don't know enough to write the
SQL command so that it just deletes the current row and not all of the
records.

Thanks for any advice.
Randy

Apr 24 '07 #9
I was using a table adapter, but I've since switched to data
adapters. I'm finding more resources explaining data adapters, so I'm
heading in that direction. I think that for the simple types of
actions that I am doing, either would probably work fine.

Thanks again for all of your help.

Randy

Apr 26 '07 #10
You're welcome. You might check out Dave Sceppa's book ADO.Net The Core
Reference -- it's very readable, and has a lot of code examples.

Good luck.
Robin S.
--------------------------------------
"Randy" <sp***********@gmail.comwrote in message
news:11**********************@b40g2000prd.googlegr oups.com...
>I was using a table adapter, but I've since switched to data
adapters. I'm finding more resources explaining data adapters, so I'm
heading in that direction. I think that for the simple types of
actions that I am doing, either would probably work fine.

Thanks again for all of your help.

Randy

Apr 27 '07 #11
tni
On Apr 24, 2:47 am, "RobinS" <Rob...@NoSpam.yah.nonewrote:
As Cor said, if he deletes one, it is marked as a delete and no longer
displayed. Usually you call yourtableadapteror dataadapter after the user
has completed all changes. The adapter checks the state of each row. If it
was Added, it executes the Insert command; if it was Modified, it executes
the Update command; if it was Delete, it executes the Delete command. The
delete command should be something like "DELETE FROM myTable WHERE
myPrimaryKey = @myPrimaryKey", and you pass in the parameter.

I don't use table adapters; they're not precise enough for me. I write my
own stored procs and execute my own code against them. Are you actually
using a strongly typed dataset and table adapter, or are you using a data
adapter and regular dataset?

Robin S.
--------------------------"Randy" <spam.eastl...@gmail.comwrote in message

news:11**********************@l77g2000hsb.googlegr oups.com...
Using the binding navigator, the user scrolls through the records in
the table. If they see one that they don't want any longer, they
click a button intended to delete the current row from the data
table. Very straightforward from a user perspective, and I think is
pretty simple programatically. I just don't know enough to write the
SQL command so that it just deletes the current row and not all of the
records.
Thanks for any advice.
Randy- Hide quoted text -

- Show quoted text -
What do yo umean by "not precise"?

May 1 '07 #12
IMO, table adapters and strongly typed datasets are limiting. First of all,
every time you change the data source, you have to regenerate the strongly
typed dataset and re-set up the table adapters. And it is difficult to
separate the layers of an application.

I generally write my business apps following the 3-layer model --
separating my UI, data access class, and business layer. I use business
objects that are populated from stored procedures, and I can handle updates
however I want to. So if I have to do a join to pull the data I want, I
can handle the updates on both tables myself.

If you have to do a join to pull data using a table adapter, you can not
use the same table adapter and strongly typed dataset to do the update.

That's what I meant by "not precise enough". I think they work fine for
general vanilla stuff, and some people have gotten them to work with a
3-layer model by passing them back and forth via serialization, or by
creating the dataset in the business layer and passing it back to the UI,
where it is cast as the strongly typed dataset. To each his own.

Robin S.
----------------------------
<tn*@nc.rr.comwrote in message
news:11**********************@l77g2000hsb.googlegr oups.com...
On Apr 24, 2:47 am, "RobinS" <Rob...@NoSpam.yah.nonewrote:
>As Cor said, if he deletes one, it is marked as a delete and no longer
displayed. Usually you call yourtableadapteror dataadapter after the
user
has completed all changes. The adapter checks the state of each row. If
it
was Added, it executes the Insert command; if it was Modified, it
executes
the Update command; if it was Delete, it executes the Delete command.
The
delete command should be something like "DELETE FROM myTable WHERE
myPrimaryKey = @myPrimaryKey", and you pass in the parameter.

I don't use table adapters; they're not precise enough for me. I write
my
own stored procs and execute my own code against them. Are you actually
using a strongly typed dataset and table adapter, or are you using a
data
adapter and regular dataset?

Robin S.
--------------------------"Randy" <spam.eastl...@gmail.comwrote in
message

news:11**********************@l77g2000hsb.googleg roups.com...
Using the binding navigator, the user scrolls through the records in
the table. If they see one that they don't want any longer, they
click a button intended to delete the current row from the data
table. Very straightforward from a user perspective, and I think is
pretty simple programatically. I just don't know enough to write the
SQL command so that it just deletes the current row and not all of the
records.
Thanks for any advice.
Randy- Hide quoted text -

- Show quoted text -

What do yo umean by "not precise"?

May 3 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Brad | last post: by
9 posts views Thread by Jenden0 | last post: by
2 posts views Thread by ashish1985s | last post: by
6 posts views Thread by insirawali | last post: by
1 post views Thread by CARIGAR | last post: by
9 posts views Thread by bryonone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.