Export (0) Print
Expand All

LinqDataSource.Inserting Event

Occurs before an insert operation.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web.Extensions (in System.Web.Extensions.dll)

public event EventHandler<LinqDataSourceInsertEventArgs> Inserting

Handle the Inserting event to validate the object to be inserted, to examine data validation errors from the data class, to change a value before the insert operation, or to cancel the insert operation. The LinqDataSourceInsertEventArgs object passed to event handlers for this event contains the new object to insert in the data source.

If a validation error occurs during the insert operation, the LinqDataSourceInsertEventArgs object contains the validation exceptions that are thrown by the data class. A validation error occurs if a value to be inserted does not match the type of the property in the data class, or if it does not pass a custom validation check. In an event handler for the Inserting event, you can retrieve the validation exceptions and take appropriate action.

If an exception is thrown in an event handler for the Inserting event, you must handle the exception in that event handler. The exception will not be passed to an event handler for the Inserted event (through the Exception property of the LinqDataSourceStatusEventArgs object). The Exception property contains only the exceptions that are thrown after the Inserting event.

The following example shows an event handler for the Inserting event that modifies data before the insert operation. The object from the NewObject property is cast to a type named Product. The DateModified property of the Product object is set to the current date and time.

protected void LinqDataSource_Inserting(object sender, LinqDataSourceInsertEventArgs e)
    Product product = (Product)e.NewObject;
    product.DateModified = DateTime.Now;

The following example shows an event handler for the Inserting event that retrieves validation exceptions.

Protected Sub LinqDataSource_Inserting(ByVal sender As Object, _
        ByVal e As LinqDataSourceInsertEventArgs)
    If (e.Exception IsNot Nothing) Then
        For Each innerException As KeyValuePair(Of String, Exception) _
               In e.Exception.InnerExceptions
            Label1.Text &= innerException.Key & ": " & _
                innerException.Value.Message & "<br />"
        e.ExceptionHandled = True
    End If
End Sub


protected void LinqDataSource_Inserting(object sender, 
        LinqDataSourceInsertEventArgs e)
    if (e.Exception != null)
        foreach (KeyValuePair<string, Exception> innerException in 
        Label1.Text += innerException.Key + ": " + 
            innerException.Value.Message + "<br />";
        e.ExceptionHandled = true;

The previous example retrieves validation exceptions. An exception might be thrown if a value does not match the type of the property. It might also be thrown from a customized check such as the one in the following example. The OnAgeChanging method checks that the number for the Age property is not negative.

partial void  OnAgeChanging(int? value)
    if (value < 0)
        throw new Exception("Age cannot be a negative number.");

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5
© 2015 Microsoft