2014-12-09 111 views
0

我的方法一直拋出異常,我不能爲我的生活看到了什麼。任何幫助將不勝感激。objectDataSource刪除方法麻煩

具有數據源和gridview的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"); 
    } 
} 
+0

數據源連接到我的所有方法,我確信我在以前的網站中使用了這種刪除方法,但它每次都會拋出異常。 相當新的asp.net,謝謝。 – 2014-12-09 12:44:22

回答

0

終於得償所願。忘了在gridview屬性中放入DataKeyNames =「DvdID」。