Web Forms Server Controls Controls You Can Use on Web Forms
The ObjectDataSource control calls business object methods based on the name of the method identified in the SelectMethod, InsertMethod, UpdateMethod, or DeleteMethod property, and based additionally on the parameter names that make up the business object method’s signature. When you create methods in a business object, you must ensure that the parameter names and types accepted by the business object method match the parameter names and types that the ObjectDataSource control passes. ( Parameter order is not important ).
Like all data source controls, the ObjectDataSource control accepts input parameters at run time and manages them in parameter collections. Each data operation has a related parameter collection. For select operations, you can use the SelectParameters collection; for updates, you can use the UpdateParameters collection; and so on.
You can specify a name, type, direction, and default value for each parameter. Parameters that get values from a specific object, such as a control, session variable, or the user profile, require you to set additional properties. For example, a ControlParameter object requires that you set the ControlID property to identify the control to take the parameter value, and set the PropertyName property to identify the property that contains the parameter value. For more information, see Using Parameters with Data Source Controls.
The following code example shows a select method that can be called by an ObjectDataSource control. The method takes a parameter and selects a single record from the data source.
[ DataObjectMethod ( DataObjectMethodType.Select ) ]
public static DataTable GetEmployee ( int EmployeeID ) {
if ( !_initialized ) { Initialize ( ); }
SqlConnection conn = new SqlConnection ( _connectionString );
SqlDataAdapter da = new SqlDataAdapter (
"SELECT EmployeeID, LastName, FirstName, Address, City, Region, PostalCode FROM Employees " +
"WHERE EmployeeID = @EmployeeID", conn );
da.SelectCommand.Parameters.Add ( "@EmployeeID", SqlDbType.Int ).Value = EmployeeID;
DataSet ds = new DataSet ( );
try {
conn.Open ( );
da.Fill ( ds, "Employees" );
}
catch ( SqlException e ) {
// Handle exception.
}
finally {
conn.Close ( );
}
if ( ds.Tables [ "Employees" ] != null )
return ds.Tables [ "Employees" ] ;
return null;
}
<DataObjectMethod ( DataObjectMethodType.Select ) > _
Public Shared Function GetEmployee ( EmployeeID As Integer ) As DataTable
If Not _initialized Then Initialize ( )
Dim conn As SqlConnection = New SqlConnection ( _connectionString )
Dim da As SqlDataAdapter = _
New SqlDataAdapter ( "SELECT EmployeeID, LastName, FirstName, Address, City, Region, PostalCode FROM Employees WHERE EmployeeID = @EmployeeID", conn )
da.SelectCommand.Parameters.Add ( "@EmployeeID", SqlDbType.Int ).Value = EmployeeID
Dim ds As DataSet = New DataSet ( )
Try
conn.Open ( )
da.Fill ( ds, "Employees" )
Catch e As SqlException
’ Handle exception.
Finally
conn.Close ( )
End Try
If ds.Tables ( "Employees" ) IsNot Nothing Then _
Return ds.Tables ( "Employees" )
Return Nothing
End Function |
|
C# |
VB |