Catching Data Exceptions and Display it in Validationsummary

When a SQLDatasource (or other DataSource Control) fail on a SQL operation a exception is fired like: duplicate key exception

user should not see the plain exception message. Goal is to display a custom message in validation summary.  My case use a formview control and a sqldatasource for inserting.  I hook into the Inserted Event and cancel the exception. A Customvalidator exists on page for error handling and is set to isvalid=false.

Protected Sub SQLDataSource1_Inserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs)

If Not IsNothing(e.Exception) Then

e.ExceptionHandled = True

CustomValidator1.IsValid = False

End Sub

Most posts which i found by search engine are meaning that this is not possible. Partial true. The trick is that the Validationsummary and the Customvalidator must be placed outside the Formview, which works fine also for the other inside validation controls.

<asp:CustomValidator ID="CustomValidator1"

Display=none

runat="server" ErrorMessage="Email Adresse bereits vorhanden"

>

</asp:CustomValidator>

So easy!

Published Tuesday, October 10, 2006 8:33 AM by preishuber
Filed under: ,

Comments

# re: Catching Data Exceptions and Display it in Validationsummary

Tuesday, April 10, 2007 3:10 PM by Brandon Duncan

Could you be a bit more specific?  I would appreciate it very much if you could send me a simple aspx page with a customvalidator and a valiationsummary which demonstrates what you are talking about.

Leave a Comment

(required) 
(required) 
(optional)
(required)