Databind Listbox

I have a listbox that I've bound to a datatable and I also have three buttons below that (Add, Edit and Delete).  When the users peforms one of these functions what is the best way to update the contents of the listbox to reflect the changes

Should I call a stored proc to update the record and then refresh the list box's datasource

or

Should I perform the add/update/delete on the datatable it is bound to   If so, how exactly do I do that   Will those changes be made in the database as well or will I still have to call the stored proc

I'm out of ideas...

Thx


Answer this question

Databind Listbox

  • Bhagvat

    Yes Ken... That's exaclty what I'd like to do!  I guess I just need help figuring out how to do it.

    My thought is to create one connection at the beginning of the program and then load the data for the current screen into datatables and databind so the user can modify the data as necessary and then call Update on the data adapter to commit the changes to the database.

    So... where and when do I create the data adapter so that it is in scope at the point I need to call Update   or do I even need to use the original data adapter that I got the data with   Can I just create a new data adapter at the time I need to update   If so, how   

    I'd love to see a WindowsForms multi-tier sample that handles updates to the backend through databinding.

  • Mimzo

    There's no right answer here -- like so many other things when dealing with data, there are just too many variables for me to tell you how to solve this. Concurrency and up-to-the-second accuracy are both issues that play into the "right" way to solve this. 

    In any case, given the information I know, I can just add that you can certainly instantiate a new SqlDataAdapter object when you need it -- it's the DataSet/DataTable itself that keeps track of changes to data. All the SqlDataAdapter does is keep track of HOW to retrieve/update/insert/delete rows, and if you're using stored procedures for that, or have connection/command text lying about that you can get to, you can always recreate the SqlDataAdapter on demand. No need to keep it "alive" throughout your application.

  • Asma_Shaolj

    Depends on how you want to handle this. Generally, I like to perform the updates locally, caching changes to the database, and then updating multiple fields when the user clicks an Update button on the form.

    You can bind the ListBox to a DataTable, and make changes locally. This won't update the actual data until you call the stored proc. If you use a DataAdapter to retrieve your data, you can have it call the stored procedure for you when you call its Update method. This will iterate through all the rows with changes within the dataset, and call the appropriate stored proc for each changed row. 

    Personally, if your goal is to update items in the list box, I'd update the DataTable locally and only update the actual data when it's necessary, if that's reasonable in your application.

  • Databind Listbox