Handling exceptions

minn

Active member
Joined
Apr 10, 2006
Messages
37
Programming Experience
Beginner
Hello,

I have a simple data entry webform that is used to add records toa database. Some of the controls are linked to fields in the database that always require a value, others do not.

I also have a submit button that when clicked add the record to the database with the entered and selected values.

The controls that require a value are application(via text box), vendor(via dropdownlist) and software tye (via dropdownlist). Now, At the moment when i leave one of these fields empty and click the the submit button i get an error like: 'Field 'GKLS Software.Application' cannot be a zero-length string.'

This i expect so to counter this i put in the following if statements in my submit click event to handle this:

VB.NET:
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] txtApplication.Text = "" [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]lblapp.Text = "You must Enter an Application"
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] ddlVendors.SelectedItem.Value = "" [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]lblvend.Text = "You must select a Vendor"
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] ddlsoftwaretype.SelectedItem.Value = "" [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]lblsofttype.Text = " You must select a Software Type"
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]


However this does not work and i get the same error. So what do i need to do to take care of this problem? Please Help!
 
I have no experience in Web Forms but you could try this :

VB.NET:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ValidData As Boolean = True
        If txtApplication.Text = "" Then
            lblapp.Text = "You must Enter an Application"
            ValidData = False
            'Use ErrorProvider
        End If
        If ddlVendors.SelectedItem.Value = "" Then
            lblvend.Text = "You must select a Vendor"
            ValidData = False
            'Use ErrorProvider
        End If
        If ddlsoftwaretype.SelectedItem.Value = "" Then
            lblsofttype.Text = " You must select a Software Type"
            ValidData = False
            'Use ErrorProvider
        End If
        If ValidData Then
            'Update Database
        End If
    End Sub
 
Back
Top