views:

40

answers:

3

Is there a way to catch all SQL exceptions in a project? I have several gridviews and multiple sqldatasources and most of the errors are going to occur when a user enters something incorrectly or in the wrong column. So how do I stop the Server Error in /Project page from showing up?

A: 

The simple answer is, validate the input before you send it to SQL Server. That way, there won't be any exceptions thrown.

If you wish to handle all your errors centrally (which is not the ideal solution for this particular problem), you can set up custom error handling in your web.config file.

Mark
That makes sense, but how can I check and see if the data the user entered is valid?
Shawn
Know what is valid and test the user's entered data before passing it to SQL Server.
Russ
There has to be a way to avoid hard programming the correct parameters for every cell in the 20 sqldatasources I have.
Shawn
Not really, sorry. This is the tedious part of software development.
Mark
Not up for tedious development. I'll use the sp_columns to get the datatype for each column and check against that.
Shawn
That'll ensure you get correct TYPES, but not that you get reasonable VALUES. Just because it's an integer doesn't mean it makes sense to order -12 items (for example).
Mark
If they do try to do something like that it's their problem. :)
Shawn
Lol, I like your style, and I hope it works out for you :)
Mark
Luckily this program is for fairly intelligent human beings, so I don't mind taking a few shortcuts like this. So far I've got it working how I want it, it stops the update and tells them what column is incorrect, so they should eventually get it right.
Shawn
A: 

I would suggest catching error in the Application_Error event inside your Global.asax in your web application.

Mikeware
However, I must agree with Mark's answer, you must validate input before SQL call
Mikeware
Is there a way to combine this with Marks answer, so any errors that I miss are caught by this?
Shawn
Actually, my suggestion will catch errors within the page and work acordingly, but custom error handling in web.config will only allow you to replace the ugly error page with your custom (more user friendly) error page. (You may be able to add some code in it but I'm unsure you can deal with the exceptions on that page)
Mikeware
I'll probably just leave the page as is then and attempt to catch as many as possible.
Shawn
A: 

The easiest way to catch all exceptions is to do it in Application_error event handler in global.asax, and utilize the web.config custom error handling to show a friendly error page. These are already mentioned by mikeware.

If you want to put all your validation logic in one place / separate them from your aspx pages, you could do it by making your website multi-layered and put your custom validation logic in the middle layer. By using this method, your data will get validated in that layer no matter which page is accessing your data. You can also use objectdatasource to do select/insert/update/delete operation. Obviously it requires some work, but it can clearly separate your data and your UI.

Gan