[VB.NET] Errorprovider doet zijn werk niet

Pagina: 1
Acties:

Onderwerpen


Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
Dag iedereen,

Voor mijn eindwerk programmeren maak ik een klein facturatiepakketje, waarmee ik zowel klanten, leveranciers, contactpersonen, ... als producten kan beheren en facturen mee kan maken.
Dit alles moet op een objectgeoriënteerde manier geprogrammeerd worden. Ik maakte dus verschillende klassen aan, en zoek nu een manier om deze te gaan controleren. Ik controleer niet in mijn properties, maar maak verschillende methoden waarmee ik ga controleren. Dit doe ik omdat ik daarna ook wil gaan werken met een errorprovider.

De methode die de controles uitvoert ziet er als volgt uit:

Visual Basic:
1
2
3
4
5
6
7
8
9
10
11
Public Overrides Function Validate() As Boolean
        MyBase.Validate()
        ValidateNaam()
        ValidateFax()

        If _ValidationErrors.Count > 0 Then
            Return False
        Else
            Return True
        End If
    End Function


Een voorbeeldje van een controle (In dit geval de controle voor de naam)

Visual Basic:
1
2
3
4
5
6
7
8
9
10
    Private Sub ValidateNaam()
        Dim ValError As ValidationError

        If String.IsNullOrEmpty(_Naam) And Not IsNumeric(_Naam) Then
            ValError = New ValidationError() 'Gebruik hier een constructor
            ValError.PropertyName = "Naam"
            ValError.ValidationError = "Naam is een verplicht veld"
            _ValidationErrors.Add(ValError)
        End If
    End Sub


Nu krijg ik allerlei fouten wanneer ik wil gaan testen. Allereerst kan ik niet meer deftig saven. Ik schreef een functie om op te slaan die er als volgt uitziet:

Visual Basic:
1
2
3
4
Public Sub Save(ByVal Datarow As tblLeveranciersRow)
        Dim taLeveranciers As New tblLeveranciersTableAdapter
        taLeveranciers.Insert(DataRow.Naam, DataRow.Adres, DataRow.Postcode, DataRow.Gemeente, DataRow.Telefoon, DataRow.Fax, DataRow.URL, DataRow.Opmerking, DataRow.ContactpersoonID, DataRow.Emailadres)
    End Sub


Wanneer ik test en ik alle gegevens correct invul krijg ik in de auto-generatedcode die er als volgt uit ziet:
Visual Basic:
1
2
3
4
5
6
7
8
9
10
11
12
13
<Global.System.Diagnostics.DebuggerNonUserCodeAttribute()>  _
        Public Property Emailadres() As String
            Get
                Try 
                    Return CType(Me(Me.tabletblLeveranciers.EmailadresColumn),String)
                Catch e As Global.System.InvalidCastException
                    Throw New Global.System.Data.StrongTypingException("The value for column 'Emailadres' in table 'tblLeveranciers' is DBNull.", e)
                End Try
            End Get
            Set
                Me(Me.tabletblLeveranciers.EmailadresColumn) = value
            End Set
        End Property


een foutmelding:
code:
1
InvalidCastException: Conversion from type 'DBNull' to type 'String' is not valid.


De velden moeten niet eens allemaal zijn ingevuld, maar ik deed het toch, om zeker geen fouten te krijgen. Om de een of andere reden loopt hier dus blijkbaar iets verkeerd.

De code in het formulier ziet er als volgt uit:
Visual Basic:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
Public Class frmLeveranciersDetails
    Private ErpLeverancier As ErrorProvider = New ErrorProvider(Me)
    Private myLeverancier As Leverancier

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        ErpLeverancier.BlinkStyle = ErrorBlinkStyle.NeverBlink
        ErpLeverancier.SetIconAlignment(Me, ErrorIconAlignment.MiddleLeft)
        ErpLeverancier.SetIconPadding(Me, 2)

    End Sub

    Private Sub TblLeveranciersBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TblLeveranciersBindingNavigatorSaveItem.Click
        myLeverancier = New Leverancier(AdresTextBox.Text, PostcodeTextBox.Text, GemeenteTextBox.Text, EmailadresTextBox.Text, TelefoonTextBox.Text, URLTextBox.Text, OpmerkingTextBox.Text, NaamTextBox.Text, FaxTextBox.Text, 1, 20)

        Try
            myLeverancier.Naam = NaamTextBox.Text
            myLeverancier.Adres = AdresTextBox.Text
            myLeverancier.Postcode = PostcodeTextBox.Text
            myLeverancier.Gemeente = GemeenteTextBox.Text
            myLeverancier.Telefoonnummer = TelefoonTextBox.Text
            myLeverancier.Fax = FaxTextBox.Text
            myLeverancier.URL = URLTextBox.Text
            'myLeverancier.LeverancierID = CType(LeverancierIDTextBox.Text, Integer)
            myLeverancier.ContactpersoonID = CType(ContactpersoonIDTextBox.Text, Integer)
            myLeverancier.Emailadres = EmailadresTextBox.Text
            myLeverancier.Opmerking = OpmerkingTextBox.Text

            ' Eerst de validation errors clearen voor we saven
            ErpLeverancier.SetError(NaamTextBox, "")
            ErpLeverancier.SetError(AdresTextBox, "")
            ErpLeverancier.SetError(PostcodeTextBox, "")
            ErpLeverancier.SetError(GemeenteTextBox, "")
            ErpLeverancier.SetError(TelefoonTextBox, "")
            ErpLeverancier.SetError(FaxTextBox, "")
            'ErpLeverancier.SetError(LeverancierIDTextBox, "")
            ErpLeverancier.SetError(ContactpersoonIDTextBox, "")
            ErpLeverancier.SetError(EmailadresTextBox, "")
            ErpLeverancier.SetError(OpmerkingTextBox, "")

            Dim CurrentRow As tblLeveranciersRow = CType(CType(Me.TblLeveranciersBindingSource.Current, DataRowView).Row, tblLeveranciersRow)
            Try
                myLeverancier.Save(CurrentRow)
            Catch ex As Exception
                MessageBox.Show(ex.ToString)
            End Try

        Catch ex As Exception
            If myLeverancier.ValidationErrors.Count > 0 Then
                For Each valError As ValidationError In myLeverancier.ValidationErrors
                    Select Case valError.PropertyName
                        Case "Naam"
                            ErpLeverancier.SetError(NaamTextBox, valError.ValidationError)
                        Case "Adres"
                            ErpLeverancier.SetError(AdresTextBox, valError.ValidationError)
                        Case "Postcode"
                            ErpLeverancier.SetError(PostcodeTextBox, valError.ValidationError)
                        Case "Gemeente"
                            ErpLeverancier.SetError(GemeenteTextBox, valError.ValidationError)
                        Case "Telefoon"
                            ErpLeverancier.SetError(TelefoonTextBox, valError.ValidationError)
                        Case "Fax"
                            ErpLeverancier.SetError(FaxTextBox, valError.ValidationError)
                            'Case "LeverancierID"
                            'ErpLeverancier.SetError(LeverancierIDTextBox, valError.ValidationError)
                        Case "ContactpersoonID"
                            ErpLeverancier.SetError(ContactpersoonIDTextBox, valError.ValidationError)
                        Case "Emailadres"
                            ErpLeverancier.SetError(EmailadresTextBox, valError.ValidationError)
                        Case "Opmerking"
                            ErpLeverancier.SetError(OpmerkingTextBox, valError.ValidationError)
                    End Select
                Next
            End If
        End Try

    End Sub

    Private Sub frmLeveranciersDetails_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'DsBoekhoudDing.tblLeveranciers' table. You can move, or remove it, as needed.
        Me.TblLeveranciersTableAdapter.Fill(Me.DsBoekhoudDing.tblLeveranciers)
    End Sub


Er zit nog wat code in het formulier, maar deze is niet relevant om hier te posten.

Ik heb het projectje ergens ge-upload, zodat je het hier ook kan downloaden en eens kan bekijken.