开发者

Passing object through ObjectDataSource Asp.net Error

I'm working on a school project and I'm running into an error that has stumped me. I'm using entity framework, repository pattern, and object data source. The error I'm getting happens when I'm trying to insert a productvariant using the product ID foreign key(which is a urlparameter).

Error

ObjectDataSource 'ProductVariantObjectDataSource' could not find a non-generic method 'InsertProductVariantByProductId' that takes parameters of type 'FV_ProductVariant'.

Link to image

http://i.stack.imgur.com/fJeR1.png

Stack Trace:

[InvalidOperationException: ObjectDataSource 'ProductVariantObjectDataSource' could not find a non-generic method 'InsertProductVariantByProductId' that takes parameters of type 'FV_ProductVariant'.] System.Web.UI.WebControls.ObjectDataSourceView.GetResolvedMethodData(Type type, String methodName, Type dataObjectType, Object oldDataObject, Object newDataObject, DataSourceOperation operation) +1355789 System.Web.UI.WebControls.ObjectDataSourceView.ExecuteInsert(IDictionary values) +339 System.Web.UI.DataSourceView.Insert(IDictionary values, DataSourceViewOperationCallback callback) +89 System.Web.UI.WebControls.DetailsView.HandleInsert(String commandArg, Boolean causesValidation) +379 System.Web.UI.WebControls.DetailsView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +574 System.Web.UI.WebControls.DetailsView.OnBubbleEvent(Object source, EventArgs e) +95 System.Web.UI.Control.Rai开发者_如何学JAVAseBubbleEvent(Object source, EventArgs args) +37 System.Web.UI.WebControls.DetailsViewRow.OnBubbleEvent(Object source, EventArgs e) +112 System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37 System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +125 System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +169 System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +9 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +176 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563

The object datasource within the aspx page

    <asp:ObjectDataSource ID="ProductVariantObjectDataSource" runat="server" 
    DataObjectTypeName="FV_ProductVariant" InsertMethod="InsertProductVariantByProductId" 
    SelectMethod="GetProductVariantByProductId" TypeName="ProductBL">
    <InsertParameters>
        <asp:Parameter Name="productVariant" Type="Object" />
        <asp:QueryStringParameter Name="productId" QueryStringField="ProductId" 
            Type="Int32" />
    </InsertParameters>
    <SelectParameters>
        <asp:QueryStringParameter Name="productId" QueryStringField="ProductId" 
            Type="Int32" />
    </SelectParameters>
</asp:ObjectDataSource>

ProductBL where the ObjectDataSource is inserting with

    public void InsertProductVariantByProductId(FV_ProductVariant productVariant, int productId)
{
    try
    {
        productRepository.InsertProductVariantByProductId(productVariant, productId);
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

The IRepository

    void InsertProductVariantByProductId(FV_ProductVariant productVariant, int ProductId);

The actual Repository

    public void InsertProductVariantByProductId(FV_ProductVariant productVariant, int productId)
{
    try
    {
        productVariant.ProductVariantId = GenerateProductVariantID();
        productVariant.ProductId = productId;
        context.FV_ProductVariant.AddObject(productVariant);
        context.SaveChanges();
    }
    catch (Exception ex)
    {
        throw ex;
    }
}


Creating an ObjectDataSource Control Source Object

If the source object for an ObjectDataSource control exposes public static methods (Shared in Visual Basic) that can be called to retrieve and modify data, an ObjectDataSource control will call those methods directly. If an ObjectDataSource control must create an instance of the source object in order to make method calls, the object must include a public constructor that takes no parameters. The ObjectDataSource control will call this constructor when it creates a new instance of the source object.

If the source object does not contain a public constructor without parameters, you can create an instance of the source object that will be used by the ObjectDataSource control in the ObjectCreating event...

Also, try making your method static.

public static void InsertProductVariantByProductId(FV_ProductVariant productVariant, int productId)
{
    try
    {
        productRepository.InsertProductVariantByProductId(productVariant, productId);
    }
    catch (Exception ex)
    {
        throw ex;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜