R – ObjectDataSource not calling Insert method when it has extra parameters

asp.netobjectdatasource

I'm new to ASP.NET, and I'm trying to implement a custom ObjectDataSource Insert method.
The default business method works fine (with a single object parameter). However when I add an extra parameter, the business method does not get called.

Here is my code – Insert() always gets called, but InsertWithParams() never does.

// Business Layer 
public class MyObject
{
  public MyObject() {}
  public string Foo { get; set;}
}

namespace MyLogic {
public static Insert(MyObject m)
{
  // ... do DB insert
}

public static InsertWithParams(MyObject m, string p)
{
  // ... do more fancy DB insert
}
} // MyLogic

Given the following ObjectDataSource declaration, "InsertWithParams" and "MyObjectDS_Inserting" are never called.

However if "MyLogic.Insert" is specified instead, it does get called along with the event. Also If I remove the p parameter from InsertWithParams, it does get called.

<asp:ObjectDataSource runat="server" ID="MyObjectDS"
        TypeName="Business.MyLogic"
        DataObjectTypeName="Business.MyObject"
        InsertMethod="InsertWithParams"
        OnInserting="MyObjectDS_Inserting"
        >
        <InsertParameters>
            <asp:Parameter Name="m" Type="Object" />
            <asp:Parameter Name="p" Type="String" />
        </InsertParameters>
</asp:ObjectDataSource>

So it seems in this case I can't add parameters to an insert method in an ObjectDataSource.
Adding or removing declared InsertParameters does not seem to make a difference.

Best Answer

I also had trouble inserting record, however, I found out after testing for a while and google a bit, that if your specify 'DataObjectTypeName' parameter in ObjectDataSource, InsertParameters get ignored. So I just don't specify that, and it works for me.

From here: http://www.velocityreviews.com/forums/t297725-objectdatasource-has-no-values-to-insert-error.html

Related Topic