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.