views:

9805

answers:

2

I'm trying to add parameters to an objectDataSource at runtime like this:

        Parameter objCustomerParameter = new Parameter("CustomerID", DbType.String, customerID);
        Parameter objGPDatabaseParameter = new Parameter("Database", DbType.String, gpDatabase);

        //set up object data source parameters
        objCustomer.SelectParameters["CustomerID"] = objCustomerParameter;
        objCustomer.SelectParameters["Database"] = objGPDatabaseParameter;

At what point in the objectDataSource lifecycle should these parameters be added (what event)? Also, some values are coming from a master page property (which loads after the page_load of the page containing the objectDataSource).

+3  A: 

Add as early as possible; at the PreInit event. This is part of initialization so should be done there.

See the ASP.NET Page Life Cycle Overview for more information.

SoloBold
+8  A: 

Add them to the event for the operation you are trying to use. For example, if these parameters are part of the SELECT command then add them to the Selecting event, if they need to go with the UPDATE command then add them on the Updating event.

The ObjectDataSource raises an event before it performs each operation, that's when you can insert parameters (or validate/alter existing parameters).

Also, don't try and modify the parameters collection of the ODS itself. You want to add your parameters to the ObjectDataSourceSelectingEventArgs that is passed to the event handler.

Something like:

e.inputParemeters["CustomerID"] = customerId;
e.inputParameters["database"] = dbName;
Andy C.
Thanks, but it appears that the parameters collection was read only. Is there a way to set the value in that event handler?
TheImirOfGroofunkistan
Yes, see my edit. You want to modify the inputParemeters member of the event arg.
Andy C.
awesome, thanks for the example. It wasn't as intuitive as I would think it could be.
TheImirOfGroofunkistan
THANK YOU!! You've made my day...
lmsasu