When do you catch data entry exceptions? (Like bad date strings)

  • Thread starter Thread starter David
  • Start date Start date
D

David

I have an asp.net page (version 3.5) with some straightforward stuff on it.
For example, I have a formview object that is linked to a sqldatasource. One
of the items is a textbox in which a user is supposed to enter a date. That
date is bound to a field, and when the user hits the "insert" button at the
bottom, a new entry is created.

It works great, unless the string entered into the textbox isn't really a
valid date. Then there is a problem. Not surprisingly, it throws an
exception.

What I want to know is where I can catch that exception. Where do I put the
code that catches the exception and forces the user to go back and put in a
correct date before the new record is created?
 
The best way to handle exceptions is to avoid them, if at all possible and
with simple user input, the exception is completely avoidable with simple
validation.

User input should *** ALWAYS *** be validated before accepted as input that
the program should use.

The most straight-forward way to do this in ASP .NET is to place the
approprate validation controls on the page, associate them with the control
they are to validate and configure properties per your requirements.

The ASP .NET Validation controls will cause the appropriate JavaScript to be
inserted into the page, causing the validation to occur at the client and
not waste bandwith or server resources. They also include a second
server-side validation by default to guard against a hacker who makes a
local copy of the page and removes the client side JavaScript and attempts
to send invalid data.

-Scott
 
David said:
I have an asp.net page (version 3.5) with some straightforward stuff on it.
For example, I have a formview object that is linked to a sqldatasource.
One
of the items is a textbox in which a user is supposed to enter a date.
That
date is bound to a field, and when the user hits the "insert" button at
the
bottom, a new entry is created.

It works great, unless the string entered into the textbox isn't really a
valid date. Then there is a problem. Not surprisingly, it throws an
exception.

What I want to know is where I can catch that exception. Where do I put
the
code that catches the exception and forces the user to go back and put in
a
correct date before the new record is created?


In your simple example, you do it at the UI, that is, validate user input
at the "insert" button event. The data on the page is valid or is not valid
for it do be inserted into or update the database.. You don't catch
exceptions in this case, because you know by using validation code if data
for a field is valid well before the attempt is made to do a CRUD operation
against a database.

If data is not valid for a field that you have validation field logic, then
you notify the user that data for a field is not valid and not do the insert
of data, in this case, at the "insert" button event. Only if the data is
valid by using validation logic do actually insert the record to the
database.

You can validate a date string field to be a date well before you try to use
the data as date data to be saved to a database.





__________ Information from ESET NOD32 Antivirus, version of virus signature database 4323 (20090810) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
Back
Top