none
Odd ( to me ) Dataset issue... RRS feed

  • Question

  • I was working with an application that has a grid that is databound to a table in a dataset. The grid allows the changeing, deleteing and adding of new records. When ever I process a delete transaction I have to see if the row selected to delete is in the database already or not. If it isnt I just delete the record from the dataset. I then have to refresh the data in the database. In order to save any work that the user may have done adding any new rows to the grid, I save any unsaved rows into an array of rows of the approriate type from the DataSet type.

     

    I then do a DataSet.Table.Clear(). ( And this is where the more experieced .net developers are chuckling Im sure.... ) This, to my dismay, cleared out my Array of rows in addition to the the rows in the datasets table!

     

    Now, my thought was that the rows in my array of rows shouldnt have been cleared out, as they have 2 references to them,1 for the array, and 1 for the dataset. Im still unclear why the data in my array of rows was cleared at the same time as the dataset table.

     

    To get around this problem I made a Struct that had the same fields in it as my strongly typed dataset and copied ( field by field ) the data from the Rows to the arraw of structs.( I chose Structs btw because I was paranoid at the time wanted to have a Value type rather than a reference type to put the data into. )

     

    Can someone explain to me what was happening and why? Im still a little new to the whole managed code paradigm and I dont want to make assumptions like I made here and have them  bite me like they did here.

     

    Thanks in advance for any and all information!!

    Friday, June 29, 2007 8:57 PM

All replies

  • Two things:

     

    1) Why are you refreshing from the database at this point? You should just delete the row from the DataSet and allow the user to continue with their edits. When all editing is complete, then you can perform the actual updates and deletes. This may save you some headaches.

     

    2) Regarding the array rows getting deleted - you were not putting the actual rows into the array, but rather a reference to the rows from the dataset. So when you deleted the dataset, the rows were gone.

     

    Hope this helps!

    Friday, June 29, 2007 9:22 PM
  • Re: #1 - I have to refresh the dataset from the database after every delete, as there isa com object that could potentially have changed the data in the database.  I know its a horrible practice but it is, sadly a fact of life for me dealing with a 3rd parties database and thier com interface.

     

    Re: #2 - So if I am understanding you correctly when I call Dataset.Table.Clear() its NOT just dereferencing the objects ( making all the ones not in the array eligible for GC ), but rather it is deleteing all the rows in the Table regardless of reference count ?

    Friday, June 29, 2007 9:43 PM