The Command Attributes in the Markup

The Command Attributes in the Markup

In ASP.NET, you can use the Command attributes in the markup of data-bound controls, such as the GridView, to specify the SQL commands that the control uses to retrieve, update, or delete data from a database. The Command attributes are:

SelectCommand: specifies the SQL query or stored procedure that the control uses to retrieve data from the database.

InsertCommand: specifies the SQL statement or stored procedure that the control uses to insert new data into the database.

UpdateCommand: specifies the SQL statement or stored procedure that the control uses to update existing data in the database.

DeleteCommand: specifies the SQL statement or stored procedure that the control uses to delete data from the database.

Each of these attributes takes a string value that represents the SQL command to be executed. You can specify the command directly in the markup, or you can use a parameterized query or stored procedure to pass dynamic values to the command at runtime.

Here’s an example of using the Command attributes in a GridView control:

<asp:GridView ID=”CustomersGridView” runat=”server” AutoGenerateColumns=”False”

    DataSourceID=”CustomersDataSource” DataKeyNames=”CustomerID”>


        <asp:BoundField DataField=”CustomerID” HeaderText=”Customer ID” ReadOnly=”True” />

        <asp:BoundField DataField=”CompanyName” HeaderText=”Company Name” />

        <asp:BoundField DataField=”ContactName” HeaderText=”Contact Name” />

        <asp:BoundField DataField=”Country” HeaderText=”Country” />

        <asp:CommandField ShowEditButton=”True” ShowDeleteButton=”True” />



<asp:SqlDataSource ID=”CustomersDataSource” runat=”server”

    ConnectionString=”<%$ ConnectionStrings:NorthwindConnectionString %>”

    SelectCommand=”SELECT CustomerID, CompanyName, ContactName, Country FROM Customers”

    UpdateCommand=”UPDATE Customers SET CompanyName = @CompanyName, ContactName = @ContactName, Country = @Country WHERE CustomerID = @CustomerID”

    DeleteCommand=”DELETE FROM Customers WHERE CustomerID = @CustomerID”>


        <asp:Parameter Name=”CompanyName” Type=”String” />

        <asp:Parameter Name=”ContactName” Type=”String” />

        <asp:Parameter Name=”Country” Type=”String” />

        <asp:Parameter Name=”CustomerID” Type=”String” />



        <asp:Parameter Name=”CustomerID” Type=”String” />



In this example, the GridView control displays data from the Customers table in the Northwind database. The SelectCommand attribute of the SqlDataSource control specifies the SQL query to retrieve the data. The UpdateCommand and DeleteCommand attributes specify the SQL statements to update or delete data, respectively. Note that the parameterized queries use named parameters (prefixed with “@”), and that the corresponding Parameter elements are defined in the UpdateParameters and DeleteParameters collections of the SqlDataSource control.

Apply for ASP.NET Certification Now!!

Back to Tutorial

Get industry recognized certification – Contact us