LinqDataSourceContextEventArgs.ObjectInstance Property

Definition

Gets or sets the data context object to create.

public:
 property System::Object ^ ObjectInstance { System::Object ^ get(); void set(System::Object ^ value); };
public object ObjectInstance { get; set; }
member this.ObjectInstance : obj with get, set
Public Property ObjectInstance As Object

Property Value

The data context object that you have set to the ObjectInstance property; otherwise, null. The default is null.

Examples

The following example shows how to create the data context object by calling a non-parameterless constructor in an event handler for the ContextCreating event. The code assigns the custom object to the ObjectInstance property.

protected void LinqDataSource_ContextCreating(object sender, LinqDataSourceContextEventArgs e)
{
    e.ObjectInstance = new ExampleDataContext(ConfigurationManager.ConnectionStrings["ExampleConnectionString"].ConnectionString);
}
Protected Sub LinqDataSource_ContextCreating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LinqDataSourceContextEventArgs)
    e.ObjectInstance = New ExampleDataContext(ConfigurationManager.ConnectionStrings("ExampleConnectionString").ConnectionString)
End Sub

Remarks

By default, the LinqDataSource control creates an instance of the class specified in the ContextTypeName property. It does this by calling the parameterless constructor of that class.

You can manually create the object for data operations by handling the ContextCreating event and setting the ObjectInstance property to an object you have created.

You create the object manually if you want to use a non-parameterless constructor or if you want to create an object that is different from the one specified in the ContextTypeName property.

The ObjectInstance property does not contain an instance of the type specified in the ContextTypeName property, because that instance is created after the ContextCreating event. The ObjectInstance property is used only when you manually create the data context object during the ContextCreating event. When you are updating, inserting, or deleting data, you must set the ObjectInstance property to an object that derives from the DataContext class.

Applies to