Showing posts with label FormView. Show all posts
Showing posts with label FormView. Show all posts

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

Maintain FormView values on validation errors

Protected Sub FormView_Paper_ItemInserted(sender As Object, e As FormViewInsertedEventArgs) Handles FormView_Paper.ItemInserted

If Not IsNothing(e.Exception) Then
   
    e.ExceptionHandled = True
    e.KeepInInsertMode = True

End If

End Sub

Thursday, 9 August 2012

Manipulate FormView values in the code behind

Instead of:
<asp:Label ID="Label_Age" runat="server" Text='<%# Eval("CalculatedAgeMonths") %>' />

Use:
<asp:Label ID="Label_Age" runat="server" Text='<%# ConvertToYearsMonths(Eval("CalculatedAgeMonths")) %>' />

And in your code behind:

Function ConvertToYearsMonths(CurrentMonths As Integer) As String
     Return "in Years!!"
End Function