0

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. 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. 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'

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:

<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.

Thanks in advance

Enixf
  • 87
  • 2
  • 9

1 Answers1

0

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. 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:

 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". I didn´t find the reason of the error but at least a work around.

Enixf
  • 87
  • 2
  • 9