Re: Update from Datasource to the database with sqlDataAdapter

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: William Ryan eMVP (dotnetguru_at_comcast.nospam.net)
Date: 05/22/04


Date: Sat, 22 May 2004 16:21:46 -0400

Hi Ori:

Like Pete mentioned, it's impossible to tell for sure without looking at the
code, but from the words you use I have a pretty good guess as to what your
problem is. .Delete and .Remove are two totally different methods. .Delete
marks your rows with a Deleted Rowstate. Remove takes them out of the
collection. If you have 100 rows, delete 50 of them and check your row
count before update (or callling AcceptChanges), you'll still have 100 rows.
If you do the same with remove you'll have 50. I have some examples here
http://www.knowdotnet.com/articles/efficient_pt4.html

Anyway, if your bindings are set up correctly, and you delete a row, you
should be fine. If you delete the records in the datatable directly you
will be fine too (provided you call update and it completes successfully).
another thing you can check is the RowStateFilter and set it to Added,
Modified and Deleted (create a DataView on the table and this is very easy
to check http://www.knowdotnet.com/articles/dataviews1.html). You can also
just call update on those rows but the main value of doing this is so that
you can see what's changed (you can also loop through everything but it's a
bit more work.)

So, if you have a listBox and you want to remove Item 20 (assuming it's
bound to a DataTable dtt) you can check the SelectedIndex for instance
(which will be 20 in my example)

dtt.Rows[cboWhatever.SelectedIndex].Delete(); //You can do this with a
context menu for instance or any other UI cue you want. If you are using a
BindingContext directly, you can also call .Remove (which is effecitvely the
opposite of .AddNew)

Hopefully this explains it, but if not, please let me know.
http://www.knowdotnet.com/articles/dataviews1.html
Cheers,

Bill

-- 
W.G. Ryan, eMVP
http://forums.devbuzz.com/
http://www.knowdotnet.com/williamryan.html
http://www.msmvps.com/WilliamRyan/
"Ori Anavim via .NET 247" <anonymous@dotnet247.com> wrote in message
news:e%23dbZ2CQEHA.3016@tk2msftngp13.phx.gbl...
> 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.
>
>
> --------------------------------
> From: Ori Anavim
>
> -----------------------
> Posted by a user from .NET 247 (http://www.dotnet247.com/)
>
> <Id>6Z0phwcpukajpwngCx+Jyw==</Id>


Relevant Pages

  • Re: BindingManagerBase Not Working
    ... Commented out the datarelations and the listbox - same result. ... The Position updates, but the fields do not refresh with the new data. ... I put the bindingmanagerbase before I added the bindings. ...
    (microsoft.public.dotnet.framework.adonet)
  • ANN: listbox 0.0.2 (alpha)
    ... and routines for listbox widgets. ... The package does not modify Listbox class bindings, ... A chronological selection is maintained for each listbox, ...
    (comp.lang.tcl)
  • ListBox Populating Question
    ... I populate a ListBox with a LogFile that has about. ... ListBox's datasource is a BindingList. ... elements, with the datasource set, it takes about 2 mins. ... updating its bindings? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Tkinter Listbox - Different Text colors in one listbox
    ... Is it possible to have different items in a listbox in different ... AFAIK, this is not possible with a listbox. ... You can however quite easily emulate the behaviour of a listbox with a text widget, which allows to mix fonts and colors in any way you like. ... removing all the bindings defined in the text widgets to get all the events, then widget.bind ...
    (comp.lang.python)