Parameter Order Wrong With ObejctDataSource

This one came up today and I felt in a sharing mood. I had a GridView using and ObjectDataSource (ODS) and this was used for automatic updates via Inplace editing. When updating the GridView this would then call the UpdateMethod of the ObjectDataSource, but it would fail with the above error.

After a little playing, it turns out that if you have got some of your columns in a GridView set to ReadOnly Visible=false, then there is no way of guranteeing the order of the parameters passed through to the UpdateMethod of the ODS.

To get around this little problem, you can either create an Update Method on your Custom Object that supports the method signature being passed through, or the better option is to hack the parameter order that is passed through.

In order to change order of the Parameters passed through you need to hook in into the Bold"Updating" event of the Object Datasource and Pass in the params in the order that you want. There is no way of actually re-ording the parameters in the InputParameters collection, so you need to copy them out then put them back in the order that you want.

The following code demostrates my implementation of this:-

void odsUsers_Updating(object sender, ObjectDataSourceMethodEventArgs e)
{
System.Collections.Specialized.ListDictionary prms = new System.Collections.Specialized.ListDictionary();
prms.Add("UserName", e.InputParameters["UserName"]);
prms.Add("email", e.InputParameters["Email"]);
prms.Add("isApproved", e.InputParameters["isApproved"]);
prms.Add("Comment", e.InputParameters["Comment"]);
prms.Add("LastActivityDate", e.InputParameters["LastActivityDate"]);
prms.Add("lastLogindate", e.InputParameters["LastLoginDate"]);
e.InputParameters.Clear();
foreach (DictionaryEntry de in prms)
{
    e.InputParameters.Add(de.Key, de.Value);
}
}
Author Greg Duffield

Greg has too many years experience in developement from VB for DOS all the way through to Windows Workflow and LINQ while covering pretty much every technology in between. A complete MS evangelist he is now Director of the only MS Gold Partner IT services company in Norfolk. Wehere they are producing Web 2 Applications for various sectors, and are currently entering the Haulage industry with their latest product.

Add Comment

Name
Comment
 

Your comment has been received and will be shown once it passes moderation.