Catching SQL Exceptions in vb.net
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 /P开发者_如何学JAVAroject page from showing up?
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.
I would suggest catching error in the Application_Error event inside your Global.asax in your web application.
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.
精彩评论