LinqDataSourceStatusEventArgs.Exception Egenskap

Definition

Hämtar undantaget som utlöstes under dataåtgärden.

public:
 property Exception ^ Exception { Exception ^ get(); };
public Exception Exception { get; }
member this.Exception : Exception
Public ReadOnly Property Exception As Exception

Egenskapsvärde

Ett Exception objekt som representerar undantaget, om ett fel uppstod, annars null.

Exempel

I följande exempel visas en händelsehanterare för Inserted händelsen. Om egenskapen är Exceptioni händelsehanteraren null hämtas produkt-ID:t från objektet i Result egenskapen . Produkt-ID:t är en primärnyckel för tabellen och anges av databasen, så värdet är inte känt förrän infogningsåtgärden har slutförts. Undantagsmeddelandet loggas om Exception egenskapen inte är lika med null. Egenskapen ExceptionHandled är sedan inställd på 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

Kommentarer

Om ett undantag utlöses under dataåtgärden lagras undantaget i Exception egenskapen . Du kan skapa händelsehanterare för ContextCreatedhändelserna , Deleted, Inserted, Selectedoch Updated och hämta undantaget, om det finns några, via Exception egenskapen .

Gäller för