Detect object reference constraint (before context.SaveChanges())

Thread is closed for posting
2 posts, 1 answers
  1. Lextendo
    Lextendo avatar
    12 posts
    Member since:
    Feb 2010

    Posted 20 Sep 2011 Link to this post

    Hi Telerik OA team,

    In a winforms situation, I have several databound grids in several tabs on one form. I want to save all changes with one button. Sofar this works nicely, however I have one problem.
    Deleting a (referenced) object from the grid does not generate a "The delete statement conflicted with the reference constraint"-error, until _context.SaveChanges() is called from the save button. So the question is (given this grid event code):

    Private Sub rg_UserDeletingRow(sender As Object, e As GridViewRowCancelEventArgs)
        Dim row As GridViewDataRowInfo = e.Rows(0)
        Try
            If row.DataBoundItem IsNot Nothing Then
                'if NoReferenceConstraint() Then _ <- How to detect a reference constaint?
                _context.Delete(row.DataBoundItem)
            End If
        Catch ex As Exception
            e.Cancel = True
            RadMessageBox.Show(ex.Message)
        End Try
    End Sub

    Added complication: the grid(s) are filled runtime, so the containing objects (row.DataBouddItem) are not typed.
    Any ideas to get this working?

    Thanks in advance,
    Alex
  2. Answer
    Alexander
    Admin
    Alexander avatar
    727 posts

    Posted 21 Sep 2011 Link to this post

    Hello Lextendo,

    You can achieve this with the Flush() method of the internal object scope that the context instance is using. In order to use it, you will have to extend your context  with another partial class (with the same name) and expose the internal Flush method. This would look like this:
    Partial Public Class YourContextClassNameHere 
        Public Sub Flush()
            Me.GetScope().Transaction.Flush()
        End Sub
    End Class

    Then you could do the following:
    Try
        If row.DataBoundItem IsNot Nothing Then
            _context.Delete(row.DataBoundItem) 
            _context.Flush()
        End If
    Catch ex As Exception 
        e.Cancel = True
        RadMessageBox.Show(ex.Message) 
    End Try

    The Flush method would start a transaction on the database server and execute the actions performed so far in the OpenAccess transaction (without committing the transaction, everything can still be rolled back). This means that if there is a conflict with a constraint, the database server would throw an error, which you could handle.
    Please just note that once a transaction is open on the database side, it has a timeout (by default 120 seconds) and if you do not commit or roll it back by that time, you would get an exception. If you experience such problems, you could increase the connection timeout from the backend configuration.
    I hope that helps.

    Kind regards,
    Alexander
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's SQL Server Community Awards. We are competing in TWO categories and every vote counts! VOTE for Telerik NOW >>

Back to Top