LinqDataSourceStatusEventArgs.Exception 屬性

定義

取得在資料作業期間擲回的例外狀況。

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

屬性值

如果發生錯誤,則為表示例外狀況的 Exception 物件,否則為 null

範例

下列範例顯示 事件的事件處理常式 Inserted 。 在事件處理常式中 Exception ,如果 屬性為 null ,則會從 屬性中的 Result 物件擷取產品識別碼。 產品識別碼是資料表的主鍵,而且是由資料庫設定,因此在插入作業完成之前,不會知道此值。 如果 屬性不等於 nullException 則會記錄例外狀況訊息。 屬性 ExceptionHandled 接著會設定為 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

備註

如果在資料作業期間引發例外狀況,例外狀況會儲存在 屬性中 Exception 。 您可以建立 、、、 和 事件的事件處理常式 ContextCreated ,並透過 Exception 屬性擷取例外狀況。 UpdatedSelectedInsertedDeleted

適用於