Performing Insert Operations
With the OpenAccessLinqDataSource control, you can create ASP.NET Web pages that enable users to insert, update, and delete data. You don't have to write any additional code, because the OpenAccessLinqDataSource control dynamically performs these operations. To let users modify data, you can enable insert, update, or delete operations on the OpenAccessLinqDataSource control. You can then connect the control to a data-bound control that lets the users insert, update, and delete data, such as the RadGrid control.
This topic describes how to enable the OpenAccessLinqDataSource control to automatically handle insert operations. It also describes how to set properties programmatically before an insert operation is performed. Finally, the topic makes an overview of the Inserting/Inserted events, exposed by the OpenAccessLinqDataSource control.
How to: Enable Automatic Inserts
To enable a data-bound control to automatically perform insert operations, you must do the following:
-
Connect the OpenAccessLinqDataSource control to a context class and specify the target table. To do that you need to set values for the ContextTypeName, and ResourceSetName properties:
<telerik:OpenAccessLinqDataSource ID="OpenAccessLinqDataSourceCategory" runat="server" ContextTypeName="SofiaCarRental.Model.FluentModel" ResourceSetName="Categories"> </telerik:OpenAccessLinqDataSource>
-
Set the EnableInsert property to True:
<telerik:OpenAccessLinqDataSource ID="OpenAccessLinqDataSourceCategory" runat="server" EnableInsert="True" ContextTypeName="SofiaCarRental.Model.FluentModel" ResourceSetName="Categories"> </telerik:OpenAccessLinqDataSource>
The control should not be set up to execute any GroupBy operations, or to retrieve a subset of the class properties.
How to: Add Default Values for Insert Data
By default, you don't need to add parameters for insert data. The data-bound control passes its values to the OpenAccessLinqDataSource control, and the data source control uses these values to set the corresponding properties. However, you have the option to provide default values for inserting data in the InsertParameters collection. These values are used only in the case when no values are entered for the corresponding properties.
Consider the following example. You have a data-bound control that is bound to a table named Categories with columns CategoryId, CategoryName and ImageFileName. InsertParameter is specified for the ImageFileName property. If no value is specified for the ImageFileName property during the insert, then the specified default value is used; otherwise, the default value is ignored.
<telerik:OpenAccessLinqDataSource
ID="OpenAccessLinqDataSourceCategory"
runat="server" EnableInsert="True"
ContextTypeName="SofiaCarRental.Model.FluentModel"
ResourceSetName="Categories">
<InsertParameters>
<asp:Parameter Name="ImageFileName" DefaultValue="Demo.png" />
</InsertParameters>
</telerik:OpenAccessLinqDataSource>
Using the Inserting/Inserted Events
The Inserting event occurs before an insert operation. You could handle the Inserting event to validate the object to be inserted, to change a value before the insert operation, or to cancel the insert operation. The OpenAccessLinqDataSourceInsertEventArgs object passed to the event handler for this event contains the new object to insert in the database. The OpenAccessLinqDataSourceInsertEventArgs object also exposes information about validation errors. For example, a validation error occurs if a value to be inserted does not match the type of the property in the domain class. You can retrieve the validation exceptions via the Exception property and take appropriate actions. For example:
protected void OpenAccessLinqDataSourceCategory_Inserting(object sender,
Telerik.OpenAccess.Web.OpenAccessLinqDataSourceInsertEventArgs e)
{
if (e.Exception != null)
{
foreach (KeyValuePair<string, Exception> innerException in
e.Exception.InnerExceptions)
{
// Process the exception
}
e.ExceptionHandled = true;
}
}
Protected Sub OpenAccessLinqDataSourceCategory_Inserting(ByVal sender As Object,_
ByVal e As Telerik.OpenAccess.Web.OpenAccessLinqDataSourceInsertEventArgs) _
Handles OpenAccessLinqDataSource.Inserting
If e.Exception IsNot Nothing Then
For Each innerException As KeyValuePair(Of String, Exception) In e.Exception.InnerExceptions
' Process the exception
Next innerException
e.ExceptionHandled = True
End If
End Sub
The Inserting event allows you to modify data before the insert operation. The following example demonstrates how to do this. Note that you need to access the NewObject property. It is of type object and you have to cast it to the type that represents the table.
protected void OpenAccessLinqDataSourceCategory_Inserting(object sender,
Telerik.OpenAccess.Web.OpenAccessLinqDataSourceInsertEventArgs e)
{
Category newCategory = e.NewObject as Category;
if (newCategory != null)
{
newCategory.ImageFileName = "Demo.png";
}
}
Protected Sub OpenAccessLinqDataSourceCategory_Inserting(ByVal sender As Object,_
ByVal e As Telerik.OpenAccess.Web.OpenAccessLinqDataSourceInsertEventArgs) _
Handles OpenAccessLinqDataSource.Inserting
Dim newCategory As Category = TryCast(e.NewObject, Category)
If newCategory IsNot Nothing Then
newCategory.ImageFileName = "Demo.png"
End If
End Sub
The OpenAccessLinqDataSourceInsertEventArgs derives from CancelEventArgs, i.e. it is a cancelable event. You can cancel the insert operation by setting the Cancel property to True.
protected void OpenAccessLinqDataSourceCategory_Inserting(object sender,
Telerik.OpenAccess.Web.OpenAccessLinqDataSourceInsertEventArgs e)
{
e.Cancel = true;
}
Protected Sub OpenAccessLinqDataSourceCategory_Inserting(ByVal sender As Object,_
ByVal e As Telerik.OpenAccess.Web.OpenAccessLinqDataSourceInsertEventArgs) _
Handles OpenAccessLinqDataSource.Inserting
e.Cancel = True
End Sub
The Inserted event occurs when an insert operation has finished. You could handle the Inserted event to catch any backend exceptions from the insert operation or to examine the values after the insert operation has finished. To access the new object, you need to examine the Result property of the OpenAccessLinqDataSourceStatusEventArgs class.