LinqDataSourceStatusEventArgs.Exception Property

Definition

Gets the exception that was thrown during the data operation.

C#
public Exception Exception { get; }

Property Value

An Exception object that represents the exception, if an error occurred; otherwise, null.

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, so 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.

C#
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;            
    }
}

Remarks

If an exception is raised during the data operation, the exception is stored in the Exception property. You can create event handlers for the ContextCreated, Deleted, Inserted, Selected, and Updated events and retrieve the exception, if any, through the Exception property.

Applies to

Product Versions
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1