JBickford
JBickford

Reputation: 409

ASP.NET Pass Error Messages

This is probably going to end up as a stupid question, but countless research has offered me no results.

I know there are different types of errors I want to check for, and when I should be throwing an exception for "exceptional" errors, and that I should create validating functions for input and other checks.

My problem is, how do I send an error back to a page when the data entered fails in a separate class?

For Example:

I can do it on inline page, no problem, its passing it through a separate class that's causing me issues... Maybe I'm not even thinking of this correctly.

Any points in the right direction would be of huge help.

Thanks for the help in advance.

Upvotes: 4

Views: 369

Answers (1)

Jason Berkan
Jason Berkan

Reputation: 8884

The simplest solution is to have Submit() return a boolean indicating whether there was an error or not:

If class.Submit() = False Then
    lblError.Text = "Hey, that is not right."
End If

It is a good practice to put your class in charge of its own errors, in which case you would expose an error message property:

If class.Submit() = False Then
    lblError.Text = class.GetErrorMessage()
End If

The Submit function would look something like this:

Public Function Submit() As Boolean
    Dim success As Boolean = False
    Try
        ' Do processing here.  Depending on what you do, you can
        ' set success to True or False and set the ErrorMessage property to
        ' the correct string.
    Catch ex As Exception
        ' Check for specific exceptions that indicate an error.  In those
        ' cases, set success to False.  Otherwise, rethrow the error and let
        ' a higher up error handler deal with it.
    End Try

    Return success
End Function

Upvotes: 2

Related Questions