none
Cannot delete a row from a dataset ("Value does not fall within the expected range") RRS feed

  • Question

  • Hi all,

    I am not sure whether this is actually the right forum to ask this question, but I couldn't find a better match. Please feel free to point me to the correct forum should I be in the wrong place.
    I am using the System.Data classes to handle interaction with a database. Data is loaded into DataTables, and individual rows are forwarded to the various objects. The objects themselves take care of all updates back to the database and the data is written back into the rows. Works fine for inserts and updates. But deleting rows gives me headaches.

    Here is an extract from the code that throws the error:

    This code throws the following Exception:

            public virtual void save() { 
                if (!dirty && !deleted) return
                data.BeginEdit(); 
                if (deleted) { 
                    if (!provider.canDelete(this.data)) throw new InvalidOperationException("Cannot delete this record, other records depend on it"); 
                    DataUpdated(new PersistenceEventArgs(DeleteSQL,data, DatabaseAction.DELETE)); 
                    data.Delete(); 
                    data.AcceptChanges(); 
                    dirty = false
                    return
                } 
     


    System.ArgumentException : Value does not fall within the expected range

    on the line that says: data.AcceptChanges
    Explanations: data is a DataRow. "dirty" and "deleted" are bools indicating the status of the object. "provider" is the object that loads the datatables and provides the rows, and it also executes all SQL statements (in that way I only need one database connection in the entire application). As I am not using foreign key relationships in the database itself, the "provider" also has a set of constraints it can use to determine whether it "canDelete" a row.

    I have checked the RowState after the "data.Delete()" statement, and it is "Deleted". However, the count of rows is still the same as before, i.e. the row is still there. And if I comment out the statement "data.AcceptChanges()", then subsequent reads from the table can retrieve the deleted record as if it wasn't deleted (which is to be expected, as the change, i.e. the deletion has not been committed).
    Unfortunately the exception message doesn't tell me which value falls outside of the expected range or what the expected range is.
    I have tried all sorts of combinations, i.e. calling EndEdit() on the row and the table, calling AcceptChanges() on the table and so on, but I always get the same exception.

    I would be truly greatful for any pointers, at the moment I am fresh out of ideas.

    cheers
    Wolfgang

    Wednesday, January 7, 2009 4:24 PM

Answers

  • Hi Joakim,

    I figured it out by separating the code into various bits. The "Delete" command on a row doesn't like it if the row is put into Edit mode. If I do a "Delete()" without the "BeginEdit()", it works fine. Strange, but true.

    cheers
    Wolfgang
    • Marked as answer by Zhi-Xin Ye Tuesday, January 13, 2009 11:12 AM
    Monday, January 12, 2009 5:24 PM

All replies

  • Hi!

    So, if you call delete() on the dataRow and then AcceptChanges() on the dataTabel you still get the same exception?

    Cheers
    --Jocke

    Thursday, January 8, 2009 12:40 PM
  • Hi Joakim,

    I figured it out by separating the code into various bits. The "Delete" command on a row doesn't like it if the row is put into Edit mode. If I do a "Delete()" without the "BeginEdit()", it works fine. Strange, but true.

    cheers
    Wolfgang
    • Marked as answer by Zhi-Xin Ye Tuesday, January 13, 2009 11:12 AM
    Monday, January 12, 2009 5:24 PM