This is a migrated thread and some comments may be shown as answers.

Validation

7 Answers 165 Views
GridView
This is a migrated thread and some comments may be shown as answers.
mampus
Top achievements
Rank 1
mampus asked on 12 Oct 2010, 07:45 AM
Hi,
is there any way to shorten my validation code?? like using case, etc..???
I have been tried many ways to shortened it, but i got nothing but error because the parameter (ByVal e As Telerik.WinControls.UI.CellValidatingEventArgs)
here my code:

Private Sub TabelBuku_CellValidating(ByVal sender As System.Object, ByVal e As Telerik.WinControls.UI.CellValidatingEventArgs) Handles TabelBuku.CellValidating
    Dim kolom = TryCast(e.Column, GridViewDataColumn)
    If ((TypeOf e.Row Is GridViewDataRowInfo Or (TypeOf e.Row Is GridViewNewRowInfo)) AndAlso (kolom IsNot Nothing) AndAlso (kolom.FieldName = "Tahun Terbit")) Then
        Dim nilai = DirectCast(e.Value, String)
        If (String.IsNullOrEmpty(nilai)) Then
            e.Cancel = True
            e.Row.ErrorText = "Tahun Terbit Belum Diisi"
            warningKesalahan(Color.LightYellow, Color.Red, "Tahun Terbit Belum Diisi!!")
        Else
            e.Row.ErrorText = String.Empty
            warningKesalahan(Color.Transparent, Color.LimeGreen, "Editor Data Buku")
        End If
    End If
    If ((TypeOf e.Row Is GridViewDataRowInfo Or (TypeOf e.Row Is GridViewNewRowInfo)) AndAlso (kolom IsNot Nothing) AndAlso (kolom.FieldName = "IdBuku")) Then
        Dim nilai = DirectCast(e.Value, String)
        If (String.IsNullOrEmpty(nilai)) Then
            e.Cancel = True
            e.Row.ErrorText = "ID Buku Harus Diisi"
            warningKesalahan(Color.LightYellow, Color.Red, "ID Buku Harus Diisi")
        Else
            e.Row.ErrorText = String.Empty
            warningKesalahan(Color.Transparent, Color.LimeGreen, "Editor Data Buku")
        End If
    End If
End Sub

7 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 12 Oct 2010, 08:34 AM
Hello,

I would suggest something like this, i don't think it's shorter, but in my point of view, it's clearer

Private Sub TabelBuku_CellValidating(ByVal sender As System.Object, ByVal e As Telerik.WinControls.UI.CellValidatingEventArgs) Handles TabelBuku.CellValidating
       Dim kolom = TryCast(e.Column, GridViewDataColumn)
 
       If ((kolom Is Nothing) Or (Not (TypeOf e.Row Is GridViewDataRowInfo Or TypeOf e.Row Is GridViewNewRowInfo)) Or _
           (kolom.FieldName <> "Tahun Terbit" And kolom.FieldName <> "IdBuku")) Then
           Return
       End If
 
       Dim nilai = DirectCast(e.Value, String)
       If (String.IsNullOrEmpty(nilai)) Then
           e.Cancel = True
 
           lblKesalahan.BackColor = Color.LightYellow
           lblKesalahan.ForeColor = Color.Red
 
           If (kolom.FieldName = "Tahun Terbit") Then
               e.Row.ErrorText = "Tahun Terbit Belum Diisi"
               lblKesalahan.Text = "Tahun Terbit Belum Diisi!!"
           Else
               e.Row.ErrorText = "ID Buku Harus Diisi"
               lblKesalahan.Text = "ID Buku Harus Diisi"
           End If
       Else
           e.Row.ErrorText = String.Empty
           warningKesalahan(Color.Transparent, Color.LimeGreen, "Editor Data Buku")
       End If
   End Sub

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga
0
mampus
Top achievements
Rank 1
answered on 13 Oct 2010, 07:25 AM
Hi there, it's me again....
the validation above work when the datatype is string, what if the datatype is integer..??
can u give me an example..??
0
Emanuel Varga
Top achievements
Rank 1
answered on 13 Oct 2010, 08:35 AM
Hello again,

There is one important question here: Is performance important?
1. Yes, and you don't mind treating each case separately then i would suggest the following:
Private Function IsNullOrEmptyValue(ByVal obj As Object) As Boolean
    If obj Is Nothing Then
        Return True
    ElseIf obj.[GetType]().Equals(GetType(String)) Then
        Return String.IsNullOrEmpty(DirectCast(obj, String))
    ElseIf obj.[GetType]().Equals(GetType(Integer)) Then
        Return CInt(obj) = 0
    Else
        Throw New NotImplementedException("This method has not been implemented for other types")
    End If
End Function

2. Not so much, and you just want to check for default values (be careful, this does not include values like string.empty), you can use a static dictionary to store the values after the first time you use them, to improve performance:
Shared ReadOnly cache As New Dictionary(Of Type, Object)()
Private Shared Function GetDefaultValue(t As Type) As Object
    If Not t.IsValueType Then
        Return Nothing
    End If
    Dim ret As Object
    If cache.TryGetValue(t, ret) Then
        Return ret
    End If
    ret = Activator.CreateInstance(t)
    cache(t) = ret
    Return t
End Function

3. Not that important, you can define an extension method for all types:
Public NotInheritable Class TypeExtension
    Private Sub New()
    End Sub
    <System.Runtime.CompilerServices.Extension> _
    Public Shared Function GetDefault(type As Type) As Object
        Return GetType(TypeExtension).GetMethod("GetDefaultImp").MakeGenericMethod(type).Invoke(Nothing, New Type(-1) {})
    End Function
 
    Public Shared Function GetDefaultImp(Of T)() As T
        Return Nothing
    End Function
End Class

You can choose which one best suits your needs.

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga
0
mampus
Top achievements
Rank 1
answered on 13 Oct 2010, 07:28 PM
hello there, thanks, i choose option 1 for solving this...but i got an error:
Unable to cast object of type 'System.Decimal' to type 'System.String'.
from this code:
Dim nilai = DirectCast(e.Value, String)
What's the problem...??
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 13 Oct 2010, 07:39 PM
Hello again Mampus,

First, if you decided to go with the first choice, then you should change your method, something like this, i cannot test it now, so please let me know if everything is OK, I have to warn you again, all of the types you are expecting have to be added in that method, to check for nulls or default values.

Private Sub TabelBuku_CellValidating(ByVal sender As System.Object, ByVal e As Telerik.WinControls.UI.CellValidatingEventArgs) Handles TabelBuku.CellValidating
        Dim kolom = TryCast(e.Column, GridViewDataColumn)
 
        If ((kolom Is Nothing) Or (Not (TypeOf e.Row Is GridViewDataRowInfo Or TypeOf e.Row Is GridViewNewRowInfo)) Or _
            (kolom.FieldName <> "Tahun Terbit" And kolom.FieldName <> "IdBuku")) Then
            Return
        End If
 
        If (IsNullOrEmptyValue(e.Value)) Then
            e.Cancel = True
 
            lblKesalahan.BackColor = Color.LightYellow
            lblKesalahan.ForeColor = Color.Red
 
            If (kolom.FieldName = "Tahun Terbit") Then
                e.Row.ErrorText = "Tahun Terbit Belum Diisi"
                lblKesalahan.Text = "Tahun Terbit Belum Diisi!!"
            Else
                e.Row.ErrorText = "ID Buku Harus Diisi"
                lblKesalahan.Text = "ID Buku Harus Diisi"
            End If
        Else
            e.Row.ErrorText = String.Empty
            warningKesalahan(Color.Transparent, Color.LimeGreen, "Editor Data Buku")
        End If
    End Sub
 
    Private Function IsNullOrEmptyValue(ByVal obj As Object) As Boolean
        If obj Is Nothing Then
            Return True
        ElseIf obj.[GetType]().Equals(GetType(String)) Then
            Return String.IsNullOrEmpty(DirectCast(obj, String))
        ElseIf obj.[GetType]().Equals(GetType(Integer)) Then
            Return CInt(obj) = 0
        ElseIf obj.[GetType]().Equals(GetType(Decimal)) Then
            Return CDec(obj) = 0
        Else
            Throw New NotImplementedException("This method has not been implemented for other types")
        End If
    End Function


Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga
0
mampus
Top achievements
Rank 1
answered on 13 Oct 2010, 07:55 PM
Whoopss...i Miss the decimal one...
thanks for assist me....
0
Emanuel Varga
Top achievements
Rank 1
answered on 13 Oct 2010, 07:58 PM
You're welcome,

If you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga
Tags
GridView
Asked by
mampus
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
mampus
Top achievements
Rank 1
Share this question
or