asp.net.ph

Skip Navigation Links

Using Parameters with the ObjectDataSource Control

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 ).

Using Parameters

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;
}
  C# VB


© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note