EntityDataSource.CommandText Property


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Gets or sets an Entity SQL command that defines the query.

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

public string CommandText { get; set; }

Property Value

Type: System.String

A string value that is the Entity SQL query.

The CommandText property of the EntityDataSource control enables you to specify a query that uses a custom Entity SQL expression. Like the SELECT statement, CommandText creates a projection of the original data that is not updatable.

When the CommandText property is assigned, update, insert, and delete functionalities are disabled. In this case, the methods CanDelete, CanInsert, and CanUpdate of the related EntityDataSourceView control all return false.

When the CommandText property is set, an exception is thrown when the ExecuteSelect method is called by a control that is data bound to an EntityDataSource control if the value of EnableInsert, EnableUpdate, or EnableDelete is set to true.

If the CommandText is set to a query that returns a projection of properties (for example, "SELECT p.ProductID, p. Name FROM AdventureWorksEntities.Products AS p"), a DbDataRecord will be returned.

The following example uses the supplied Entity SQL command to returns a collection of Product objects.

<asp:EntityDataSource ID="ProductDataSource" runat="server" 
    CommandText="SELECT value p FROM Products AS p
        WHERE p.ProductID
        BETWEEN @OrderIdMin AND @OrderIdMax"
    DefaultContainerName="AdventureWorksEntities" >
        <asp:ControlParameter Name="OrderIdMin" 
            ControlID="ProductIdMin" Type="Int32"/>
        <asp:ControlParameter Name="OrderIdMax" 
            ControlID="ProductIdMax" Type="Int32" />

The following example returns a projected series of columns:

<asp:EntityDataSource ID="ProductDataSource" runat="server" 
    CommandText="SELECT p.ProductID, p.ProductName, p.UnitsOnOrder
          FROM Products AS p 
          WHERE p.ProductID BETWEEN @OrderIDMin AND @OrderIDMax" 
          ContextTypeName="AdventureWorksModel. AdventureWorksEntities">
        <asp:ControlParameter Name="OrderIDMin" 
                ControlID="ProductIDMinTextBox" Type="Int32"/>
        <asp:ControlParameter Name="OrderIDMax" 
                ControlID="ProductIDMaxTextBox" Type="Int32" />

.NET Framework
Available since 3.5
Return to top