ASP.NET SQL Data Source


Example

Controls that can be bound with data can make use of SqlDataSource controls. The SqlDataSource control not only allows you to retrieve data from a database, but also edit and sort the data.

Retrieving Data

Stored Procedure:

<asp:SqlDataSource ID="SqlDataSourceEmployees"
    runat="server"
    ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
    SelectCommand="sp_GetEmployees"
    SelectCommandType="StoredProcedure">
</asp:SqlDataSource>

SQL Query:

<asp:SqlDataSource ID="SqlDataSourceEmployees"
    runat="server"
    ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
    SelectCommand="SELECT
                       EmployeeID, 
                       EmployeeFirstName,
                       EmployeeLastName
                   FROM
                       dbo.Employees">
</asp:SqlDataSource>

Parameters:

<asp:SqlDataSource ID="SqlDataSourceEmployees"
    runat="server"
    ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
    SelectCommand="SELECT
                       EmployeeID, 
                       EmployeeFirstName,
                       EmployeeLastName
                   FROM
                       dbo.Employees
                   WHERE
                       DepartmentID = @DepartmentID;">
    <SelectParameters>
        <asp:ControlParameter ControlID="ddlDepartment"
            Name="DepartmentID"
            PropertyName="SelectedValue" />
    </SelectParameters>
</asp:SqlDataSource>

Be aware of the CancelSelectOnNullParameter option, that if set to true (default) will stop the data binding if any parameter is NULL

Basic Usage

GridView:

<asp:GridView ID="GridViewEmployees"
    runat="server"
    AutoGenerateColumns="false"
    DataSourceID="SqlDataSourceEmployees">
    <Columns>
        <asp:BoundField DataField="EmployeeID" HeaderText="Employee ID" />
        <asp:BoundField DataField="EmployeeFirstName" HeaderText="First Name" />
        <asp:BoundField DataField="EmployeeLastName" HeaderText="Last Name" />
    </Columns>
</asp:GridView>