ASP.NET Pass Error Messages

This will probably be a stupid question, but countless studies have not given me any results.

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

My problem is how to send an error message to the page when the entered data failed in a separate class?

Example:

  • User input entered in Page1.aspx, click on Submit () calls in Class.vb
  • Class.vb detects input is invalid
  • How to update the Page1.aspx shortcut to say "Hey, this is wrong."

I can do this on the embedded page, without problems, passing it through a separate class that causes me problems ... Perhaps I don’t even think about it correctly.

Any points in the right direction will be of great help.

Thanks for the help in advance.

+4
source share
1 answer

The simplest solution is to send Submit (), returning a boolean value indicating whether the error was or not:

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

It is good practice that your class is responsible for its errors, in which case you will find the error message property:

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

The Submit function will 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 
+2
source

Source: https://habr.com/ru/post/1300198/


All Articles