1

I'm using EF 6.1.3 and SQL server 2014 for this ASP.NET Web Forms application. I drag&dropped a gridview on my .aspx page design view and used Smart Tag to choose my data source and automatically fill the grid. I enabled edit and delete operations on gridview and it works with no problem but I want it to use the functions I wrote.

This is what I tried for delete operation

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                    int rowIndex = Convert.ToInt32(e.CommandArgument);
                    Result<Models.UserList> result;
                    using (ModelOperations.UserList.IUserListOperations dao = new ModelOperations.UserList.UserListOperations())
                    {
                        result = dao.removeUser(new Models.UserList()
                        {
                            ID = Convert.ToInt32((GridView1.Rows[rowIndex].Cells[1].Text))
                        });
                    }
            }
        }

It actually works but the page gives an error. The method in my UserListOperations have a try-catch block but there is no exception caught because the code is simply working.

This is the GridView code generated by EF

<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="UserID" DataSourceID="myEntities">
            <Columns>
                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
                <asp:BoundField DataField="UserID" HeaderText="UserID" ReadOnly="True" SortExpression="UserID" />
                <asp:BoundField DataField="UserName" HeaderText="UserName" SortExpression="UserName" />
                <asp:BoundField DataField="UserPW" HeaderText="UserPW" SortExpression="UserPW" />
                <asp:BoundField DataField="UserType" HeaderText="UserType" SortExpression="UserType" />
            </Columns>
        </asp:GridView>

And this is the error

Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.

If I take the values from textbox and send values that way, there is no error so I'm guessing it's something about the auto-generated grid.

ardatosun
  • 457
  • 4
  • 22
  • if you check this link : http://stackoverflow.com/questions/1836173/entity-framework-store-update-insert-or-delete-statement-affected-an-unexpec you can have more informations about your problem i think – Paul Jul 15 '15 at 08:04
  • Thanks for the reply. I did check that link but didn't/couldn't find an answer for my problem. In my case the delete operation works and I still get this error that is what I'm trying to understand why that happens. – ardatosun Jul 15 '15 at 08:16

0 Answers0