Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Handling validation errors on the page level

Reply
Thread Tools

Handling validation errors on the page level

 
 
hardieca@hotmail.com
Guest
Posts: n/a
 
      07-30-2007
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

 
Reply With Quote
 
 
 
 
bruce barker
Guest
Posts: n/a
 
      07-30-2007
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)

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
>

 
Reply With Quote
 
 
 
 
hardieca@hotmail.com
Guest
Posts: n/a
 
      07-30-2007
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.com> wrote:
> 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



 
Reply With Quote
 
bruce barker
Guest
Posts: n/a
 
      07-30-2007
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)

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.com> wrote:
>> 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

>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Handling errors using Custom Errors configuration settings in web.config jayeshsorathia@gmail.com ASP .Net 0 08-17-2012 08:08 AM
c is a low-level language or neither low level nor high level language pabbu C Programming 8 11-07-2005 03:05 PM
page-level vs control-level enableViewState =?Utf-8?B?Sm9l?= ASP .Net 3 10-26-2005 07:14 PM
Page Level and Applicatoin Level Custom Errors rranveer@gmail.com ASP .Net 2 02-13-2005 02:03 AM
Errors, errors, errors Mark Goldin ASP .Net 2 01-17-2004 08:05 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57