views:

267

answers:

3

Hello,


Even though UpdateNames stored procedure only takes one parameter named @First, we are still able to define additional parameter named Last, and the update will still be successful:

    <asp:SqlDataSource ... UpdateCommand="UpdateNames"
        UpdateCommandType="StoredProcedure">
        <UpdateParameters>
            <asp:Parameter Name="First" Type="String" />
            <asp:Parameter Name="Last" Type="String" />
        </UpdateParameters>
    </asp:SqlDataSource>


But when dealing with ObjectDataSource control, the number of parameters defined in ObjectDataSource must exactly match the number of parameters defined in UpdateNames() method. Thus, if UpdateNames() takes only one parameter named First, the following will cause an exception

     <asp:ObjectDataSource ... UpdateMethod="UpdateNames">
        <UpdateParameters>
            <asp:Parameter Name="First" Type="String" />
            <asp:Parameter Name="Last" Type="String" />
        </UpdateParameters>
    </asp:ObjectDataSource>


A) Why isn’t ObjectDataSource’s matching algorithm as flexible as SqlDataSource’s matching algorithm and thus ignores any extra parameters?


Thank you

+2  A: 

The ObjectDataSource trades one kind of flexibility for another.

You are correct that your function's parameters much match the listed parameters exactly, but it is for a good reason.

The ObjectDataSource lets you define multiple functions to handle different parameters, so if you want to support a UpdateNames() method that can take either a first name or a first and last name, you just define both functions and handle them however you see fit.

using System.ComponentModel;

[DataObject]
public class MyODS
{
    [DataObjectMethod(DataObjectMethodType.Update)]
    public void UpdateNames(string First)
    {
     UpdateNames(First, null)
    }

    [DataObjectMethod(DataObjectMethodType.Update)]
    public void UpdateNames(string First, string Last)
    {
     //Do the update
    }
}
Michael La Voie
So if ObjectDataSource had the same matching algorithm as SqlDataSource, then it might not know which of the overloaded methods to call?!
SourceC
+1  A: 

A complete guess, from experience with the two:

The SQL version will just go through the array an SQL script to excute the stored procedure, Appending the variables as it goes.

The ObjectDataSource uses reflection to find a update method that matches the parameters passed. So it fails if no matching method exists on the object that takes the supplied parameters.

Adrian
+1  A: 

You can think of it as if your SqlDataSource were "translated" into sql, and this is perfectly valid sql:

DECLARE @First varchar(50)
DECLARE @Last varchar(50)
SELECT @First = 'some value', @Last = 'some other value'

SELECT * FROM [MyTable] WHERE FirstName= @First

On the other hand, you can think of an ObjectDataSource as if it were translated into calls using Reflection functions like PropertyInfo.GetValue() and PropertyInfo.SetValue(). If you call one of those using an object or field that doesn't exist, you'll get an exception.

Joel Coehoorn