Hi All,

I have a very simple DataSet linked to a ListBox. I need the User to be able to add Data and have used the following code (in a ToolStrip command button):

Dim newFileRow As DataRow = Database1DataSet.Tables("tbl_Files").NewRow()
        newFileRow("File Name") = fleName
        newFileRow("File Path") = flePath
        newFileRow("Times Opened") = 0
        newFileRow("Last Opened") = DateTime.Now
        Database1DataSet.Tables("tbl_Files").Rows.Add(newFileRow)

This seems to work ok - the new Data immediately appears in the ListBox. However, as soon as I close the form all of the Data in the DataSet is lost. Does anyone know what I'm doing wrong?

Thanks for looking, Chris.

Recommended Answers

All 9 Replies

add
Database1DataSet.Tables("tbl_Files").AcceptChanges()
at the end

That did it. Thanks!

Ok I'm lost I guess because this is marked as solved. AcceptChanges does not save the data to the database; it instead changes the row state properties of the dataset/datatable making the new records to UnChanged so that they cant be saved to a database when calling update.

Hey Tom.

I can't even pretend to understand it - all I know is it works. Either that or something else has changed (I've been working on other areas of the project) that makes it work.

I can open and close the form as often as I like now and the Data is right where it's supposed to be.

This is the first time I've worked with a DataSet (and I nearly gave up on the idea).

Take a look at AcceptChanges in the help file, it does not insert data to the database. I cant say much besides that since I dont know what else your code is doing.

Ahh.. The penny has dropped.
I changed the original (posted) code to this:

tbl_FilesAdapter.Insert(fleName, flePath, 0, DateAndTime.Now)
tbl_FilesAdapter.Update(Database1DataSet.tbl_Files)
Database1DataSet.Tables("tbl_Files").AcceptChanges()

after poking around on the net.
I think it's working as a combination of the Update/AcceptChanges commands.
Does it make sense now?

Perfect sense, the DataAdapter.Update is what sends the new records to the database. The AcceptChanges isnt needed (its redundent), the call to the Update method automatically changes the records row state to Unchanged as it completes the update for each row. Calling the AcceptChanges after that changes any un-updated records to unchanged but since that was already done...

Also Im not sure what the insert line is doing (the first line) you have already added the rows to the table in the first example unless you removed that coding too...

Altogether I would say the all that is needed is your orginal example which adds the new rows to your dataset & datatable and then the call to the DataAdapter.Update method right after that which saves all the changes in the datatable/dataset to the database.

Thanks for that Tom, it's all making much more sense now.

The 'Insert' is indeed a replacement for the code in my original post (one line of code in place of 6!) - I thought it was much neater.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.