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”>

    <Columns>

        <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” />

    </Columns>

</asp:GridView>

<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”>

    <UpdateParameters>

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

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

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

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

    </UpdateParameters>

    <DeleteParameters>

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

    </DeleteParameters>

</asp:SqlDataSource>

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!!

https://www.vskills.in/certification/certified-aspnet-programmer

Back to Tutorial

Share this post
[social_warfare]
Opening a file in PHP
Reading from a file

Get industry recognized certification – Contact us

keyboard_arrow_up