Data source controls greatly expand the capabilities of data-bound controls such as the
Data Modification Commands
You can configure the
Modifying Data with the ObjectDataSource Control
The ObjectDataSource control enables you to specify a data object method for performing a specific type of update. The
The following code example shows an ObjectDataSource control with its InsertMethod, UpdateMethod, and DeleteMethod properties configured with the names of methods from the underlying data object.
Visual BasicВ | ![]() |
---|---|
<asp:ObjectDataSource ID="EmployeeDetailsObjectDataSource" runat="server" TypeName="Samples.AspNet.Controls.NorthwindEmployee" SelectMethod="GetEmployee" UpdateMethod="UpdateEmployee" DeleteMethod="DeleteEmployee" InsertMethod="InsertEmployee" OnInserted="EmployeeDetailsObjectDataSource_OnInserted" > <SelectParameters> <asp:Parameter Name="EmployeeID" /> </SelectParameters> <InsertParameters> <asp:Parameter Name="NewEmployeeID" Direction="Output" Type="Int32" DefaultValue="0" /> </InsertParameters> </asp:ObjectDataSource> |
C#В | ![]() |
---|---|
<asp:ObjectDataSource ID="EmployeeDetailsObjectDataSource" runat="server" TypeName="Samples.AspNet.Controls.NorthwindEmployee" SelectMethod="GetEmployee" UpdateMethod="UpdateEmployee" DeleteMethod="DeleteEmployee" InsertMethod="InsertEmployee" OnInserted="EmployeeDetailsObjectDataSource_OnInserted" > <SelectParameters> <asp:Parameter Name="EmployeeID" /> </SelectParameters> <InsertParameters> <asp:Parameter Name="NewEmployeeID" Direction="Output" Type="Int32" DefaultValue="0" /> </InsertParameters> </asp:ObjectDataSource> |
For more information, see Creating an ObjectDataSource Control Source Object.
Modifying Data with SQL Commands
The SqlDataSource and AccessDataSource controls enable you to supply SQL commands for modifying data at the data source. The
The following code example shows a SqlDataSource control with its InsertCommand, UpdateCommand, and DeleteCommand properties configured with SQL commands that perform update tasks.
Visual BasicВ | ![]() |
---|---|
<asp:sqlDataSource ID="EmployeeDetailsSqlDataSource" SelectCommand="SELECT EmployeeID, LastName, FirstName FROM Employees WHERE EmployeeID = @EmpID" InsertCommand="INSERT INTO Employees(LastName, FirstName) VALUES (@LastName, @FirstName); SELECT @EmpID = SCOPE_IDENTITY()" UpdateCommand="UPDATE Employees SET LastName=@LastName, FirstName=@FirstName WHERE EmployeeID=@EmployeeID" DeleteCommand="DELETE Employees WHERE EmployeeID=@EmployeeID" ConnectionString="<%$ ConnectionStrings:NorthwindConnection %>" OnInserted="EmployeeDetailsSqlDataSource_OnInserted" RunAt="server"> <SelectParameters> <asp:Parameter Name="EmpID" Type="Int32" DefaultValue="0" /> </SelectParameters> <InsertParameters> <asp:Parameter Name="EmpID" Direction="Output" Type="Int32" DefaultValue="0" /> </InsertParameters> </asp:sqlDataSource> |
C#В | ![]() |
---|---|
<asp:sqlDataSource ID="EmployeeDetailsSqlDataSource" SelectCommand="SELECT EmployeeID, LastName, FirstName FROM Employees WHERE EmployeeID = @EmpID" InsertCommand="INSERT INTO Employees(LastName, FirstName) VALUES (@LastName, @FirstName); SELECT @EmpID = SCOPE_IDENTITY()" UpdateCommand="UPDATE Employees SET LastName=@LastName, FirstName=@FirstName WHERE EmployeeID=@EmployeeID" DeleteCommand="DELETE Employees WHERE EmployeeID=@EmployeeID" ConnectionString="<%$ ConnectionStrings:NorthwindConnection %>" OnInserted="EmployeeDetailsSqlDataSource_OnInserted" RunAt="server"> <SelectParameters> <asp:Parameter Name="EmpID" Type="Int32" DefaultValue="0" /> </SelectParameters> <InsertParameters> <asp:Parameter Name="EmpID" Direction="Output" Type="Int32" DefaultValue="0" /> </InsertParameters> </asp:sqlDataSource> |
For more information, see Modifying Data using the SqlDataSource Control.
Parameters
You can make your data-update scenarios more flexible secure by using parameters to pass values to be inserted, updated, or deleted in a data store. Parameter values can include the values of controls on the page, ASP.NET application variables, session values, and so on.
Parameter values typically come from data-bound controls when those controls invoke an update, insert, or delete operation. Additionally, you can create explicit parameter objects for the data source control for a given operation, which allows you to customize the parameters. For example, you can use an explicit parameter object to specify the type or direction of a parameter, or to define a default value for a parameter in case a null value is passed.
For the ObjectDataSource control, parameter values are passed as arguments when calling the appropriate method of the underlying data object. For the SqlDataSource or AccessDataSource control, parameters are passed to the SQL command used for the update. For more information and examples, see Using Parameters with Data Source Controls.
Events
Data source controls raise events both before and after data is modified. You can use these events to execute code before the data operation occurs, including canceling the operation, and after the data operation has taken place. For example, you can use the