objectDataSource删除方法麻烦

时间:2014-12-09 12:37:46

标签: gridview objectdatasource

我的方法不断抛出异常,我不能为我的生活看到什么。任何帮助将不胜感激。

具有数据源和网格视图的manager.aspx页面的HTML。

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="ObjectDataSource1" AllowPaging="True"  OnSelectedIndexChanged="GridView1_SelectedIndexChanged1">
     <Columns>
         <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
         <asp:BoundField DataField="DvdID" HeaderText="DvdID" SortExpression="DvdID" />
         <asp:BoundField DataField="DvdTitle" HeaderText="DvdTitle" SortExpression="DvdTitle" />
         <asp:BoundField DataField="DvdCertificate" HeaderText="DvdCertificate" SortExpression="DvdCertificate" />
         <asp:BoundField DataField="DvdPrice" HeaderText="DvdPrice" SortExpression="DvdPrice" />
     </Columns>
 </asp:GridView>

<br />
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" DeleteMethod="DeleteDvd"  InsertMethod="InsertDvd" SelectMethod="GetDvds" TypeName="DvdRepository" UpdateMethod="UpdateDvd" OnSelecting="ObjectDataSource1_Selecting">
    <DeleteParameters>
        <asp:Parameter Name="DvdID" Type="Int32" />
    </DeleteParameters>
    <InsertParameters>
        <asp:Parameter Name="DvdTitle" Type="String" />
        <asp:Parameter Name="DvdCertificate" Type="String" />
        <asp:Parameter Name="DvdPrice" Type="String" />
    </InsertParameters>
    <UpdateParameters>
        <asp:Parameter Name="DvdID" Type="Int32" />
        <asp:Parameter Name="DvdTitle" Type="String" />
        <asp:Parameter Name="DvdCertificate" Type="String" />
        <asp:Parameter Name="DvdPrice" Type="String" />
    </UpdateParameters>

来自DvdRepository的代码(删除方法)

public void DeleteDvd(int DvdID)
{
    DvdDBContext dvdDBContext = new DvdDBContext();
    Dvd dvd = dvdDBContext.Dvds.SingleOrDefault(Dvd => Dvd.DvdID == DvdID);
    if (dvd != null)
    {
        dvd.DvdID = DvdID;
        //dvdDbContext.Dvds.Attach(dvd);
        dvdDBContext.Dvds.Remove(dvd);
        dvdDBContext.SaveChanges();
    }
    else
    {
        throw new ApplicationException("Cannot find the Dvd");
    }
}

1 个答案:

答案 0 :(得分:0)

终于明白了。忘记在gridview属性中放置DataKeyNames =&#34; DvdID&#34;。