views:

83

answers:

1

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:

  • User input entered in Page1.aspx, click calls Submit() in Class.vb
  • Class.vb finds that input is invalid
  • How do I update Page1.aspx label to say "Hey, that is not right".

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.

+2  A: 

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
Jason Berkan
Thanks, I knew it was something crazy easy. I ended up doing this, and was able to create an error property to pass any custom message if success = false.I wasn't looking at it the correct way, thanks again!
JBickford