473,387 Members | 1,483 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 19919
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: VMI | last post by:
How can I dump a datatable into an Access table (which contais exactly the same structure)? Somebody suggested doing this with a dataAdapter. I don't want to run an Insert query for each record (I...
12
by: VMI | last post by:
For some reason, the process of retrieving data (about 20 records) from an Access table that has 400K records to a dataTable is taking over 3 mins. to complete. Below is my code to connect to the...
9
by: Brad | last post by:
I have written some code to manipulate data/records in a MASTER (order header) and DETAIL (order details) tables. What I have written is too extensive to post but essentially trying to: 1....
9
by: Jenden0 | last post by:
I'm new to C# (and Microsoft in general) so this may be a simple problem, but I haven't been able to figure it out yet. I've got a database with a number of different tables and I want the user...
7
by: Susan Mackay | last post by:
I have a data table that is connected to a database table with a data adapter in the 'standard' manner. However I want to be able to remove selected rows from the data table (i.e. no longer...
6
Cintury
by: Cintury | last post by:
Hi all, I've developed a mobile application for windows mobile 5.0 that has been in use for a while (1 year and a couple of months). It was developed in visual studios 2005 with a back-end sql...
0
by: mwenz | last post by:
I am trying to update an Access table using OLEDB in VB.Net 2005. I can add rows but I cannot update them. Code to instantiate the Access database and table... Dim conn As New...
2
by: ashish1985s | last post by:
using System.IO; using System.Data; using System.Collections; using System.Configuration; using System.Xml; using System.Data.SqlClient; using System; using log4net; using log4net.Config;
6
by: insirawali | last post by:
Hi all, I have this problem, i need to know is there a way i cn use the data adapter's update method in this scenario. i have 3 tables as below create table table1{ id1 int identity(1,1)...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.