0
votes

My method keeps throwing exception and I cant for the life of me see whats up. Any help would be greatly appreciated.

HTML for manager.aspx page which has the datasource and gridview.

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

Code from DvdRepository (delete method)

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
the datasource is connected to the all my methods, i was sure i used this delete method in a previous websites but it throws the exception every time. fairly new to asp.net, thanks. - David McCartney

1 Answers

0
votes

Finally got it. Forgot to put DataKeyNames="DvdID", in the gridview properties.