Επεξεργασία

Κοινή χρήση μέσω


LinqDataSourceStatusEventArgs.ExceptionHandled Property

Definition

Gets or sets a value that indicates whether the exception was handled and that it should not be thrown again.

public:
 property bool ExceptionHandled { bool get(); void set(bool value); };
public bool ExceptionHandled { get; set; }
member this.ExceptionHandled : bool with get, set
Public Property ExceptionHandled As Boolean

Property Value

true if the exception was handled; otherwise, false.

Examples

The following example shows an event handler for the Inserted event. In the event handler, if the Exception property is null, the product ID is retrieved from the object in the Result property. The product ID is a primary key for the table and is set by the database, therefore the value is not known until the insert operation has finished. The exception message is logged if the Exception property is not equal to null. The ExceptionHandled property is then set to true.

protected void LinqDataSource_Inserted(object sender, LinqDataSourceStatusEventArgs e)
{
    if (e.Exception == null)
    {
        Product newProduct = (Product)e.Result;
        Literal1.Text = "The new product id is " + newProduct.ProductID;
        Literal1.Visible = true;            
    }
    else
    {
        LogError(e.Exception.Message);
        Literal1.Text = "We are sorry. There was a problem saving the record. The administrator has been notified.";
        Literal1.Visible = true;
        e.ExceptionHandled = true;            
    }
}
Protected Sub LinqDataSource_Inserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LinqDataSourceStatusEventArgs)
    If (IsNothing(e.Exception)) Then
        Dim newProduct As Product
        newProduct = CType(e.Result, Product)
        Literal1.Text = "The new product id is " & newProduct.ProductID
        Literal1.Visible = True
    Else
        LogError(e.Exception.Message)
        Literal1.Text = "We are sorry. There was a problem saving the record. The administrator has been notified."
        Literal1.Visible = True
        e.ExceptionHandled = True
    End If
End Sub

Remarks

You can create event handlers for the ContextCreated, Deleted, Inserted, Selected, and Updated events to examine any exceptions that occurred during those operations. If you handle the exception and do not want it to be thrown again, set the ExceptionHandled property to true. If you do not set the ExceptionHandled property to true, the exception will be propagated to the next event handler in the call stack.

Applies to