ObjectDataSource : how to set insert / update parameters programmatically
November 10, 2009 – 5:02 pmHaving begun programming with ASP.NET from its inception where you wrote your own solutions to common problems, I’ve found the introduction of ObjectDataSource objects and similar functionality a blessing and a curse. Although they can save a bit of coding I always find my self wasting lots of time trying to find which events I should be performing certain tasks in. Trying to find how to do simple tasks such as setting insert or update parameters can often waste considerable amount of time.
To that end, I’ve written the following the “remind” myself of these simple tasks…
First, if you’re working with the ObjectDataSource using the DataObjectTypeName property (passing objects in the CRUD methods) the dictionaries passed to the inserting, updating and deleting events are read only by design. If you try to modify these values you will get a “OrderedDictionary is readonly and cannot be
modified.” error. For example, you can’t access an InputParameter and modify it. You can however, access the InputParameter and modify the object it contains! For example in your ObjectDataSource_Updating method…
Location entity = (Location)e.InputParameters["entity"];
entity.CityTownID = some_value;
If you’re not using DataObjectTypeName and are therefore not passing an entity object in your CRUD methods, then you can modify parameters in your ObjectDataSource inserting/updating methods. For example…
e.InputParameters["some_param"] = some_value;
Or if the parameter doesn’t exists…
ObjectDataSource1.UpdateParameters.Add(”some_param”, “some_value”);