Propogate from Listbox back to database with DeleteCommand

O

Ori

Hi,

I have a listbox which is bounded to a datatable. I allow the users to
add/remove items from the list while adding/remove rows from the
datatable which function as the datasource.

When I come to do the "update" back to the datatbase using the
SqlDataAdapter and the insert/delete command, I see that only the new
rows are added and the old one doesn't. removed from the database.

After a short investigation, I notice that if I remove the rows
directly from the datatable the deletecommand doesn't have a row to
operate on.

Can someone explain me exactly how to use the delete command along
with a binding to a listbox which you can remove and add items.

Thanks,

Ori.
 
W

William Ryan eMVP

Hi Ori:

--
W.G. Ryan MVP Windows - Embedded

www.devbuzz.com
www.knowdotnet.com
http://www.msmvps.com/williamryan/
Ori said:
Hi,

I have a listbox which is bounded to a datatable. I allow the users to
add/remove items from the list while adding/remove rows from the
datatable which function as the datasource.

When I come to do the "update" back to the datatbase using the
SqlDataAdapter and the insert/delete command, I see that only the new
rows are added and the old one doesn't. removed from the database.
--- There are a few things to look for .

How are you 'removing' the rows from your datatable
?http://www.knowdotnet.com/articles/efficient_pt4.html
Are you calling the .Remove method or the .Delete method? If you are
callling remove, then you are actually taking the rows out of the
colllection so when you call update, the Adapter doesn't even see these
rows, hence it has nothing to call against them. Delete on the other hand
doesn't do anything but change the rowstate on the row. So when the adapter
loops through the rows collection checking the Rowstate, it sees the .Delete
(d) rowstates and then uses those rows in conjuntion with your adapter's
delete commadn.

If you are calling .Delete, then make sure that you can not 'editing' ... do
this by calling .EndCurrentEdit. Another thign may be your Delete commadn.
Make sure it's configured properly and not raising any exceptions... many
times these are wrapped in a try catch block that's not responded to so it
is actually breaking but there's no indication of it b/c the exception
handler is eating it.

Another thing you may want to try, is right before the call to update,
ccreate a DataView on the bound table.

DataView dv = ListBoxstable.DefaultView;
dv.RowStateFilter = DataViewRowState.Deleted;
Debug.Assert(dv.Count > 0); //just to verify that you in fact have deleted
rows.

Let me know how what happens and we'll take it from there.

HTH,

Bill
 

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