none
Typed DATAROW Delete Problem RRS feed

  • Question

  • Hi all,
    I have been a problem related to deleting a record (typed datarow) in my Typed Dataset for 3-4 days and I have not seen a solution neither on MSDN nor the other forums. Could you help me please???

    There is a Typed Dataset and I get one of its table to my Datatgridview. In order to delete a record on the Datargirview, first I select the row, and the I show a confirmation dialog. If user click Yes, I have proceed like that in order to delete selected row.

    public
     void
     DeleteRecord(string
     id)
            {
                MyTypedDataset dataSet; //This is my typed dataset
    
                MyTypedDataset.STUDENTRow row; //I create a typed datarow 
    
               
                try
    
                {
                    FillStudentTable(dataSet.STUDENT); //I fill the STUDENT table before deleting 
    a row of it
    row = dataSet.STUDENT.FindByID(id); //I search the row for deleting in the STUDENT table
    by using given record's id.
    dataSet.STUDENT.RemoveSTUDENTRow(row); //! This works (because when debugging I observe
    that after executing this, the row is deleted from the STUDENT table)
    FullUpdate(dataSet.STUDENT); //This is the SAME method I use for updating a datatable in
    my Typed Dataset. That normally works, but for this purpose, It does not work. I think there must not be
    a problem related to FullUpdate.
    //Because if I use Delete() method of datarow, I succeed. But I want to use
    RemoveTypedDatarow() method due to I use Typed Dataset.
    } catch (Exception ex) { throw new Exception(ex.Message); } }

    As I said above, I can delete selected record by using Delete() method. But I do not want to use Delete() method instead of using RemoveTypedDatarow() method. Because I use TypedDataset and I think it is more logical to use RemoveTypedDatarow() property. Could you help me please?

    Best regards...

    Tuesday, March 16, 2010 3:53 PM

Answers

  • The DataRow.Delete method transitions an existing row to DataRowState.Deleted.  The row is hidden for data binding purposes, but it is not actually removed from the DataSet -- the DataRowState.Deleted acts as a flag so that the adapter knows that a DELETE SQL statement needs to be issued for the row in question.

    The Remove method you mentioned completely removes the row from the DataSet.  Therefore, the adapter has no way to know to submit a DELETE SQL statement.  This behavior is useful in advanced scenarios, but in simple scenarios Delete is what you usually want.

    There really isn't a strong-typing issue here -- Delete can be called as a method on the strongly typed DataRow.  The reason for what you are seeing with Remove is because it is actually a method on the strongly-typed DataTable, not the strongly-typed DataRow.  Remove is a method on the table instead of the row to emphasize that it completely removes the object from the collection.  This is consistent with how other collections, such as List<T> work.

    YourStrongRowType dr = /* ... */;
    dr.Delete();
    Use Delete(), it's what you need!
    Tuesday, March 16, 2010 11:43 PM
  • > if I do not use boxing, I have got a converting exception. So, is there any mistake for using boxing above?

    The word for what you did is "cast".  (Details:  Boxing does not happen in this particular case.  Boxing happens is when a struct is cast to type object or an interface type.)

    Accessing a row by index is relatively uncommon, and the designer does not generate a method specifically for your type.  So, you typically need the cast.  (Note: Delete() is actually a method implemented in DataRow, the base class of MyDataset.STUDENTRow, so if you do not need any of the STUDENTRow-specific properties/methods, you could use a variable of type DataRow and omit the cast.)

    DataRow dr = dataSet.STUDENT.Rows[0];
    dr.Delete();

    > Do I have to use AcceptChanges()

    No.  The adapter (when configured the default way) will automatically perform the AcceptChanges after performing the database operations.  That's exactly when you need the row taken out of the DataSet to match the database.





    Wednesday, March 17, 2010 10:26 PM

All replies

  • The DataRow.Delete method transitions an existing row to DataRowState.Deleted.  The row is hidden for data binding purposes, but it is not actually removed from the DataSet -- the DataRowState.Deleted acts as a flag so that the adapter knows that a DELETE SQL statement needs to be issued for the row in question.

    The Remove method you mentioned completely removes the row from the DataSet.  Therefore, the adapter has no way to know to submit a DELETE SQL statement.  This behavior is useful in advanced scenarios, but in simple scenarios Delete is what you usually want.

    There really isn't a strong-typing issue here -- Delete can be called as a method on the strongly typed DataRow.  The reason for what you are seeing with Remove is because it is actually a method on the strongly-typed DataTable, not the strongly-typed DataRow.  Remove is a method on the table instead of the row to emphasize that it completely removes the object from the collection.  This is consistent with how other collections, such as List<T> work.

    YourStrongRowType dr = /* ... */;
    dr.Delete();
    Use Delete(), it's what you need!
    Tuesday, March 16, 2010 11:43 PM

  • Dear BinaryCoder,
    thanks for your suggestion. Actually I had tried your suggestion before, but now I have not so trouble after your suggestion. There is some changes I have to make I think and I indicated them below. Could you inform me if there is any mistakes? Because, after your confirmation, I will start my deleting construction according to this approach.

    MyDataset dataSet = new MyDataset();
    MyDataset.STUDENTRow drRemoveStudent;
    SelectWithFilter(dataSet.STUDENT, "Student_Id= 100"); //With this method, I fill STUDENT table with only the student whose id=100.
    drRemoveStudent = (MyDataset.STUDENTRow)dataSet.STUDENT.Rows[0]; //Due to there is one record, I just take the first row.
    drRemoveStudent.Delete();
    dataAccess.FullUpdate(dataSet.STUDENT); //This method Updates all STUDENT table in MyDataset.

    My questions are;

    1) At line
    drRemoveStudent = (MyDataset.STUDENTRow)dataSet.STUDENT.Rows[0];
    if I do not use boxing, I have got a converting exception. So, is there any mistake for using boxing above?


    2) On the other hand, is it logical to use Delete() instead of using  RemoveSTUDENTRow(row)? Because, I think that; for Datarow, Remove() method means Delete() and then calling AcceptChanges(). But I do not use AcceptChanges() method. Although I do not use it, the record is deleted. Do I have to use AcceptChanges() with Delete() or can I use Delete() method without AcceptChanges()??? Could you clarify a little bit more please?

    With kindest regards...




    Wednesday, March 17, 2010 8:41 AM
  • > if I do not use boxing, I have got a converting exception. So, is there any mistake for using boxing above?

    The word for what you did is "cast".  (Details:  Boxing does not happen in this particular case.  Boxing happens is when a struct is cast to type object or an interface type.)

    Accessing a row by index is relatively uncommon, and the designer does not generate a method specifically for your type.  So, you typically need the cast.  (Note: Delete() is actually a method implemented in DataRow, the base class of MyDataset.STUDENTRow, so if you do not need any of the STUDENTRow-specific properties/methods, you could use a variable of type DataRow and omit the cast.)

    DataRow dr = dataSet.STUDENT.Rows[0];
    dr.Delete();

    > Do I have to use AcceptChanges()

    No.  The adapter (when configured the default way) will automatically perform the AcceptChanges after performing the database operations.  That's exactly when you need the row taken out of the DataSet to match the database.





    Wednesday, March 17, 2010 10:26 PM

  • Ok, thank you very much for your good explanations.
    BR.
    Thursday, March 18, 2010 7:32 AM
  • The DataRow.Delete method transitions an existing row to DataRowState.Deleted.  The row is hidden for data binding purposes, but it is not actually mov from the DataSet -- the DataRowState.Deleted acts as a flag so that the adapter knows that a DELETE SQL statement needs to be issued for the row in question.

    The Remove method you mentioned completely removes the row from the DataSet.  Therefore, the adapter has no way to know to submit a DELETE SQL statement.  This behavior is useful in advanced scenarios, but in simple scenarios Delete is what you usually want.

    There really isn't a strong-typing issue here -- Delete can be called as a method on the strongly typed DataRow.  The reason for what you are seeing with Remove is because it is actually a method on the strongly-typed DataTable, not the strongly-typed DataRow.  Remove is a method on the table instead of the row to emphasize that it completely removes the object from the collection.  This is consistent with how other collections, such as List<T> work.

    YourStrongRowType dr = /* ... */;
    dr.Delete();
    
    Use Delete(), it's what you need!

    Thanks for your explanation! It's comprehensive.
    Saturday, August 28, 2010 3:06 AM