简体   繁体   English

执行OnRowCommand(ASP.NET)后未给出名称的System.InvalidOperationException

[英]System.InvalidOperationException with no name given after executing an OnRowCommand (ASP.NET)

So I'm getting a System.InvalidOperationException, the error below, after I delete an item from the database by using the onRowCommand on a GridView. 因此,在使用GridView上的onRowCommand从数据库中删除一项后,我得到了System.InvalidOperationException,以下错误。 The thing is that it perfectly works, it deletes the item but after the method specified in the onRowCommand finished it gives me the error, which doesn't make sense. 问题是它可以正常工作,可以删除项目,但是在onRowCommand中指定的方法完成后,它给了我错误,这没有任何意义。 Already looked at this and this 已经看着这个这个

error:A public method with the name '' was either not found or there were multiple methods with the same name on the type 'ASP.admin_overview_aspx' 错误:找不到名称为“''的公用方法,或者类型为“ ASP.admin_overview_aspx”的名称相同的多个方法

stack trace: 堆栈跟踪:

 [InvalidOperationException: A public method with the name '' was either not found or there were multiple methods with the same name on the type 'ASP.admin_overview_aspx'.] System.Web.UI.WebControls.ModelDataSourceView.FindMethod(String methodName) +2439378 System.Web.UI.WebControls.ModelDataSourceView.RequireAsyncModelBinding(String methodName, ModelDataSourceMethod& method) +67 System.Web.UI.WebControls.ModelDataSourceView.Delete(IDictionary keys, IDictionary oldValues, DataSourceViewOperationCallback callback) +86 System.Web.UI.WebControls.GridView.HandleDelete(GridViewRow row, Int32 rowIndex) +930 System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +1183 System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e) +89 System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37 System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +90 System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37 System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e) +114 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +260 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +12 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1639 

here is the Gridview: 这是Gridview:

 <h4>General Policies </h4> <asp:GridView runat="server" ID="GeneralGrid" ItemType="Source.GeneralPolicy" DataKeyNames="id" SelectMethod="GPolicyGrid_Get" AutoGenerateColumns="false" OnRowCommand="GeneralGrid_RowCommand"> <Columns> <asp:DynamicField DataField="id" /> <asp:DynamicField DataField="name" /> <asp:DynamicField DataField="appliedGroup" /> <asp:DynamicField DataField="author" /> <asp:DynamicField DataField="createdOn" /> <asp:DynamicField DataField="modifiedOn" /> <asp:TemplateField> <ItemTemplate> <asp:Button ID="ButtonEdit" runat="server" CommandName="EDIT" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" Text="Edit" /> <asp:Button ID="ButtonDelete" runat="server" CommandName="DELETE" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" Text="Delete" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> 

and here is the code-behind: 下面是代码:

protected void passwordGrid_RowCommand(object sender, GridViewCommandEventArgs e)
    {

        GridView gv = (GridView)sender;
        int index = Convert.ToInt32(e.CommandArgument.ToString());
        int id = Convert.ToInt32(gv.DataKeys[index].Value);

        if (e.CommandName == "EDIT")
        {
            Response.Redirect("~/Admin,false);
        }
        else if (e.CommandName == "DELETE")
        {
            using (NubeSSPRContext db = new NubeSSPRContext())
            {

                var ppolicy = db.PasswordPolicies.Find(id);
                db.Entry(ppolicy).State = EntityState.Deleted;

                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    ModelState.AddModelError("",
                     String.Format("Item with id {0} no longer exists in the database.", id));
                }
            }

        }

Any idea on the source? 在源头上有任何想法吗? I have four tables where I can delete from with different onRowCommand methods, and all of them return the same error after deletion. 我有四个表,可以使用不同的onRowCommand方法从中删除它们,所有这些表在删除后都会返回相同的错误。

Thanks in advance 提前致谢

Ok, so I found the source of the problem. 好的,所以我找到了问题的根源。 I´ll leave the answer just in case anyone has the same problem. 万一有人遇到同样的问题,我将留下答案。 It seemed to be a problem with the OnRowCommand="GeneralGrid_RowCommand" and precisely the DELETE command. OnRowCommand =“ GeneralGrid_RowCommand”以及确切的DELETE命令似乎是一个问题。 So I found an alternate easier way to do the edit and delete commands. 因此,我找到了执行编辑和删除命令的另一种简便方法。 Basically it was easier to do an updateMethod and a deleteMethod like this: 基本上,这样执行updateMethod和deleteMethod更加容易:

 public void AnswerGrid_DeleteItem(int id)
    {
        using (NubeSSPRContext db = new NubeSSPRContext())
        {

            var policy = db.AnswerPolicies.Find(id);
            db.Entry(policy).State = EntityState.Deleted;

            try
            {
                db.SaveChanges();

            }
            catch (DbUpdateConcurrencyException)
            {
                ModelState.AddModelError("",
                     String.Format("Item with id {0} no longer exists in the database.", id));
            }
        }
    }

    // The id parameter name should match the DataKeyNames value set on the control
    public void GeneralGrid_UpdateItem(int id)
    {
        Response.Redirect("~/Admin/GeneralSettings?GeneralPolicyID=" + id.ToString(), false);
    }

Then updating the buttons of the grid with having CommandName="Update","Delete". 然后通过使用CommandName =“ Update”,“ Delete”更新网格按钮。 I didn´t find the reason of the error but at least a work around. 我没有找到错误的原因,但至少可以解决。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.Net 错误 System.InvalidOperationException - ASP.Net error System.InvalidOperationException 错误:ASP.NET WebAPI中的System.InvalidOperationException - Error: System.InvalidOperationException in ASP.NET WebAPI ASP.NET 核心 API System.InvalidOperationException 在本地化 - ASP.NET Core API System.InvalidOperationException in localization System.InvalidOperationException:无法解析 ASP.NET Core 中的服务 - System.InvalidOperationException: Unable to resolve service in ASP.NET Core ASP.NET Core 6 Web API 的集成测试抛出 System.InvalidOperationException - Integration test for ASP.NET Core 6 web API throws System.InvalidOperationException Asp.net 核心System.InvalidOperationException: 无法追踪实体类型x的实例 - Asp.net core System.InvalidOperationException: The instance of entity type x cannot be tracked ASP.NET 核心 Web API - System.InvalidOperationException: LINQ 表达式 'DbSet<mandate> ()</mandate> - ASP.NET Core Web API - System.InvalidOperationException: The LINQ expression 'DbSet<Mandate>() ASP.NET 核心错误:System.InvalidOperationException:尝试激活时无法解析服务类型 - ASP.NET Core error: System.InvalidOperationException: Unable to resolve service for type while attempting to activate System.InvalidOperationException:无法在数据迁移中的asp.net mvc 6上解析EF7中的项目“ ef” - System.InvalidOperationException: Unable to resolve project 'ef' in EF7 at asp.net mvc 6 in data migration EF继续抛出错误System.InvalidOperationException ASP.NET MVC - EF keep throwing error System.InvalidOperationException ASP.NET MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM