473,386 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Handling validation errors on the page level

Hi,

I've created an n-tier app where validation rules reside in the
business layer. When a webform is saved, a business object examines
its state, and if some property is invalid, throws a custom exception
(ValidationException)

The exception bubbles up to the UI where it gets trapped:

Private Sub Page_Error(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Error
Dim ctx As HttpContext = HttpContext.Current

Dim exception As Exception = ctx.Server.GetLastError()

If TypeOf (exception) Is ValidationException Then
Me.txtErrorMessages.Text = exception.message
ctx.Server.ClearError()
End If
End Sub

The exception is being successfully caught, but at the end of the
process I'm served with an empty page. What I would like to have
happen is the page reloads with all the form's information in addition
to a textbox being populated with the exception's message. I tried
using response.redirect(), but of course the textbox does not get
populated.

I'd rather not redirect the user to an error page, I want them to be
able to fix the validation errors on the form and continue on from
there. What's the best way to tackle this?

Regards,

Chris

Jul 30 '07 #1
3 2255
just create a custom validator and add to page.

CustomValidator cv = new CustomValidator();
cv.IsValid = false;
cv.ErrorMessage = "my error";
cv.Visible = true;
cv.Display = ValidatorDisplay.None;
this.Form.Control.Add(cv);
-- bruce (sqlwork.com)

ha******@hotmail.com wrote:
Hi,

I've created an n-tier app where validation rules reside in the
business layer. When a webform is saved, a business object examines
its state, and if some property is invalid, throws a custom exception
(ValidationException)

The exception bubbles up to the UI where it gets trapped:

Private Sub Page_Error(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Error
Dim ctx As HttpContext = HttpContext.Current

Dim exception As Exception = ctx.Server.GetLastError()

If TypeOf (exception) Is ValidationException Then
Me.txtErrorMessages.Text = exception.message
ctx.Server.ClearError()
End If
End Sub

The exception is being successfully caught, but at the end of the
process I'm served with an empty page. What I would like to have
happen is the page reloads with all the form's information in addition
to a textbox being populated with the exception's message. I tried
using response.redirect(), but of course the textbox does not get
populated.

I'd rather not redirect the user to an error page, I want them to be
able to fix the validation errors on the form and continue on from
there. What's the best way to tackle this?

Regards,

Chris
Jul 30 '07 #2
Thanks for the response, but I'm still getting served an empty page.
My handler now looks like this:

Private Sub Page_Error(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Error
Dim ctx As HttpContext = HttpContext.Current

Dim exception As Exception = ctx.Server.GetLastError()

If TypeOf (exception) Is ApplicationException Then
Dim cv As New CustomValidator()
cv.IsValid = False
cv.ID = "myCV"
cv.ErrorMessage = Exception.Message
cv.Visible = True
cv.Display = ValidatorDisplay.None
Me.Form.Controls.Add(cv)
End If

' --------------------------------------------------
' To let the page finish running we clear the error
' --------------------------------------------------
ctx.Server.ClearError()

End Sub

I would really like to figure out a way to validate and preserve the
form information in the Page_Error handler because I intend to have
the handler inherited by every page that has a form on it. I would
rather have this validation check performed in the Page_Error of the
base page class all my pages inherit from rather than have a Try/Catch
block on every page. Is there any way to do this?

Chris

On Jul 30, 12:18 pm, bruce barker <nos...@nospam.comwrote:
just create a custom validator and add to page.

CustomValidator cv = new CustomValidator();
cv.IsValid = false;
cv.ErrorMessage = "my error";
cv.Visible = true;
cv.Display = ValidatorDisplay.None;
this.Form.Control.Add(cv);

-- bruce (sqlwork.com)

hardi...@hotmail.com wrote:
Hi,
I've created an n-tier app where validation rules reside in the
business layer. When a webform is saved, a business object examines
its state, and if some property is invalid, throws a custom exception
(ValidationException)
The exception bubbles up to the UI where it gets trapped:
Private Sub Page_Error(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Error
Dim ctx As HttpContext = HttpContext.Current
Dim exception As Exception = ctx.Server.GetLastError()
If TypeOf (exception) Is ValidationException Then
Me.txtErrorMessages.Text = exception.message
ctx.Server.ClearError()
End If
End Sub
The exception is being successfully caught, but at the end of the
process I'm served with an empty page. What I would like to have
happen is the page reloads with all the form's information in addition
to a textbox being populated with the exception's message. I tried
using response.redirect(), but of course the textbox does not get
populated.
I'd rather not redirect the user to an error page, I want them to be
able to fix the validation errors on the form and continue on from
there. What's the best way to tackle this?
Regards,
Chris

Jul 30 '07 #3
if the Page Error event fires, page processing is cancelled. you are
supposed to redirect to the error page of your choice. clearing the
errors does not restart page processing.

you should be catching your BI validation errors in the calling method,
not at the page level.

-- bruce (sqlwork.com)

ha******@hotmail.com wrote:
Thanks for the response, but I'm still getting served an empty page.
My handler now looks like this:

Private Sub Page_Error(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Error
Dim ctx As HttpContext = HttpContext.Current

Dim exception As Exception = ctx.Server.GetLastError()

If TypeOf (exception) Is ApplicationException Then
Dim cv As New CustomValidator()
cv.IsValid = False
cv.ID = "myCV"
cv.ErrorMessage = Exception.Message
cv.Visible = True
cv.Display = ValidatorDisplay.None
Me.Form.Controls.Add(cv)
End If

' --------------------------------------------------
' To let the page finish running we clear the error
' --------------------------------------------------
ctx.Server.ClearError()

End Sub

I would really like to figure out a way to validate and preserve the
form information in the Page_Error handler because I intend to have
the handler inherited by every page that has a form on it. I would
rather have this validation check performed in the Page_Error of the
base page class all my pages inherit from rather than have a Try/Catch
block on every page. Is there any way to do this?

Chris

On Jul 30, 12:18 pm, bruce barker <nos...@nospam.comwrote:
>just create a custom validator and add to page.

CustomValidator cv = new CustomValidator();
cv.IsValid = false;
cv.ErrorMessage = "my error";
cv.Visible = true;
cv.Display = ValidatorDisplay.None;
this.Form.Control.Add(cv);

-- bruce (sqlwork.com)

hardi...@hotmail.com wrote:
>>Hi,
I've created an n-tier app where validation rules reside in the
business layer. When a webform is saved, a business object examines
its state, and if some property is invalid, throws a custom exception
(ValidationException)
The exception bubbles up to the UI where it gets trapped:
Private Sub Page_Error(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Error
Dim ctx As HttpContext = HttpContext.Current
Dim exception As Exception = ctx.Server.GetLastError()
If TypeOf (exception) Is ValidationException Then
Me.txtErrorMessages.Text = exception.message
ctx.Server.ClearError()
End If
End Sub
The exception is being successfully caught, but at the end of the
process I'm served with an empty page. What I would like to have
happen is the page reloads with all the form's information in addition
to a textbox being populated with the exception's message. I tried
using response.redirect(), but of course the textbox does not get
populated.
I'd rather not redirect the user to an error page, I want them to be
able to fix the validation errors on the form and continue on from
there. What's the best way to tackle this?
Regards,
Chris

Jul 30 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Hans-Joachim Widmaier | last post by:
Hi all. Handling files is an extremely frequent task in programming, so most programming languages have an abstraction of the basic files offered by the underlying operating system. This is...
2
by: Rajeev Soni | last post by:
Hi, Considering the scenario for handling exceptions in Web Application where we have Presentation layer, Business layer and Data Access layer; if there any exception is occurred in DAL, what is...
12
by: Nalaka | last post by:
Hi, I suddenly started getting a lot of errors from html validation (some CSS) so I followed the following instructions to disable it. If you'd rather not have these types of HTML validation...
7
by: Garth Wells | last post by:
I'm trying to create a DAL and am wondering what's the proper way to handle errors in this Insert method. public string Insert() { Database db = DatabaseFactory.CreateDatabase(); string...
4
by: Rob | last post by:
Hey all, So.. a simple FormView/SqlDataSource to handle inserting records into a table. The table has a primary key that the user enters (eg DiscountCode). If the user enters a duplicate the...
1
by: metsys | last post by:
We have an ASP.NET 2.0 (C#) application that is divided into multiple layers. The multiple layers come from having a web project and 2 different class library projects in the same solution. I'm...
27
by: Chris | last post by:
Hi, I have a form for uploading documents and inserting the data into a mysql db. I would like to validate the form. I have tried a couple of Javascript form validation functions, but it...
7
by: Tim_Mac | last post by:
hi, using .net 2.0, i have a web form with lots of textboxes, drop-down-lists etc. There are lots of required field validators and regular expression validators. When i click the 'save' button,...
2
by: Kevin Frey | last post by:
One of my chief criticisms of validators in an ASP.NET page is that they can result in a developer re-implementing much of the "business logic" of a transaction at the page level. Assuming we...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.