none
How to prevent cascade delete in dataset RRS feed

  • Question

  • I have a dataset that consists of two DataTables and a DataRelation that links the parent/child.

     

    I want to prevent the user from deleting the parent if it has child records.

     

    To do this I have :

     

    // Create DataRelation.

    DataRelation relRelation;

    relRelation= new DataRelation("relRelation", Master_ID, Child_Master_ID);

     

    ...

     

    ForeignKeyConstraint foreignKey = relRelation.ChildKeyConstraint;

    foreignKey.DeleteRule = Rule.None;

     

    The VS2005 documentation states:

     

    "None specifies that no action be taken, but exceptions are generated."

     

    This is fine but how do I trap this exception.  I do not want to shut the form down - I just want to let the user know that child records exist so the row cannot be deleted.

     

    Please help!!

     

    Friday, July 20, 2007 10:08 AM

Answers

  • Sometimes I just astound myself with my stupidity.

     

    I modified the BindingSource for the master table  - I removed the delete button but added a new Delete button and added the following Click event code:

     

    private void btnDeleteRole_Click(object sender, EventArgs e)

    {

    try

    {

    bnRoles.BindingSource.RemoveCurrent();

    }

    catch (Exception ex)

    {

    if (ex.GetType().ToString() == "System.Data.InvalidConstraintException")

    {

    MessageBox.Show("Deleting this row will strand child rows .. please delete child rows first","Invalid Delete Option");

    }

    else

    {

    throw (ex);

    }

    }

    }

    Friday, July 20, 2007 12:39 PM

All replies

  • What you could do is to create try .. catch and put only the method that deletes the row from the DataTable inside of this try ... catch. In this case you will know for sure that error happened when it was attempt to delete the row. I cannot find information about specific type of exception, which will be thrown in this case, but I believe you could do it and then narrow your exception handler to catch only that specific exception type.

    Friday, July 20, 2007 10:31 AM
    Moderator
  • Thanks for the reply.

     

    I should have said that my parent and child tables are in DataGridViews and I have used BindingNavigators to perform the navigation/deletion etc.

     

    The don't know where to put the try/catch block as the Deletion code is already part of the navigator (I think).

    Friday, July 20, 2007 11:01 AM
  • Sometimes I just astound myself with my stupidity.

     

    I modified the BindingSource for the master table  - I removed the delete button but added a new Delete button and added the following Click event code:

     

    private void btnDeleteRole_Click(object sender, EventArgs e)

    {

    try

    {

    bnRoles.BindingSource.RemoveCurrent();

    }

    catch (Exception ex)

    {

    if (ex.GetType().ToString() == "System.Data.InvalidConstraintException")

    {

    MessageBox.Show("Deleting this row will strand child rows .. please delete child rows first","Invalid Delete Option");

    }

    else

    {

    throw (ex);

    }

    }

    }

    Friday, July 20, 2007 12:39 PM