Error messages cause program not to work


I have used the data connection wizard to connect VB.NET to my Access database - the result is a success. However when I try to add error messages like this:

If editCType.Text = "" Then
            MsgBox("Contact type cannot be blank", MsgBoxStyle.Information, "Error")
            editCType.Focus()
End If

the program no longer works because a second error message appears - that I did not create and this causes the program to stop.  

There is already some code in the program which appears to be designed to catch errors but I don't know how to use it correctly - it looks like this:

Try
            'Attempt to load the dataset.
            Me.LoadDataSet()
        Catch eLoad As System.Exception
            'Add your error handling code here.
            'Display error message, if any.
            System.Windows.Forms.MessageBox.Show(eLoad.Message)
End Try
Me.objContactDetailDataSet_PositionChanged()

Any suggestions?
tuck1996Asked:
Who is Participating?
 
SanclerConnect With a Mentor Commented:
That is happening because your program is attempting to commit the value in editCType.Text to the datatable (and/or from the datatable to the database) before your code has checked whether it has a valid value in it.  It's difficult to suggest in detail how you should change things to overcome this without knowing much more about your program's structure.  But have a look at this

http://msdn2.microsoft.com/en-us/library/ms229603.aspx

Roger
0
 
Carl TawnSystems and Integration DeveloperCommented:
What error are you getting ?
0
 
SanclerCommented:
As carl_tawn says, we really need to know what error message you are getting and, ideally, on which line it is occurring.  But here's a couple of comments.

Yes, that bit of code you quoted is "designed to catch errors" but it only does so with the code placed between Try and Catch.  So, in the specific code you quoted it means that if, and only if, there is an error when the program tries to execute the line

            Me.LoadDataSet()

that error will be "caught" and the message box will display a message about what it is.  If you want similar things to happen for errors with any other code, you will need to put that code after a Try and then put code to say what you want to happen after  a Catch and then put  an End Try.

One possible source of an unhandled error is in the last line of the code you show

Me.objContactDetailDataSet_PositionChanged()

It depends what objContactDetailDataSet_PositionChanged refers to, but it looks suspiciously like a sub to handle a binding manager's (probably a CurrencyManager's) PositionChanged Event.  And, if it is a call to such a sub, and the sub is in the standard form, it needs to be called with the arguments (sender As Object, e As EventArgs).

Finally, where does the code you show at the start come in your program?  I doubt this is the case with you, as your program would just have appeared to freeze rather than reporting an error, but if it were in the code handling the textbox's GotFocus Event, it would be circular.  Every time the last line of your code reset the focus to the textbox it would fire again.

Roger

0
 
tuck1996Author Commented:
The field 'Contact.CType' cannot contain a Null value because the Required property for this field is set to True.  Enter a value in this field.  I did resolve this at one point but then I could not get the form to write back to the database.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.