Monday, 10 June 2013

Custom SQL exceptions (error messages) to end users

Protected Sub FormView_Paper_ItemInserted(sender As Object, e As FormViewInsertedEventArgs) Handles FormView_Paper.ItemInserted
       
    If Not IsNothing(e.Exception) Then
            Dim DoDisplayCustomError As Boolean = False

            Dim SQLError As System.Data.SqlClient.SqlException
            If e.Exception.Source = ".Net SqlClient Data Provider" Then
                SQLError = DirectCast(e.Exception, SqlException)
                If SQLError.ErrorCode = -2146232060 Then
                    DoDisplayCustomError = True
                    ' Develop ur message display function!
                    Me.CurrentMasterPage.DisplayError("Missing Mandatory fields", "Fill in all mandatory fields.")
                End If
            End If

            If Not DoDisplayCustomError Then
                Me.CurrentMasterPage.DisplayError(e.Exception)
            End If
            e.ExceptionHandled = True
            e.KeepInInsertMode = True
        Else
            Response.Redirect("OtherPage.aspx")
        End If
    End Sub

No comments:

Post a Comment