CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2010
    Posts
    26

    Question Gridview - Delete and Edit button not working

    I have a gridview with an object data source. I have the edit and delete button on each row but cannot get either one of them to work. I do not receive an error, it just doesn't delete. Refreshing does not show deleted either.
    The method within the dataset is

    Code:
     DELETE FROM BookWHERE        (BookID = @Original_BookID)
    Code:
     UPDATE       BookSET                ISBN = @ISBN, Title = @Title, Author = @Author, Status = @Status, Loaned = @Loaned, Series = @Series, Description = @DescriptionWHERE        (BookID = @Original_BookID)
    I have a business layer that defines the methods for update and delete:

    Code:
    [System.ComponentModel.DataObject]
    public class BookManager
    {   
        ///
        /// Gets a list with Address objects for the requested contact person.
    	///
    
        private BookTableAdapter _bookAdapter = null;
        protected BookTableAdapter Adapter
        {
            get
            {
                if (_bookAdapter == null)
                    _bookAdapter = new BookTableAdapter();
                return _bookAdapter;
            }
        }
    ..........
    [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
        public bool UpdateBook(int bookid, string isbn, string title, string author, string status,
        string loaned, string series, string description)        
        {
            BookDataSet.BookDataTable books = Adapter.GetBookByBookID(bookid);
            if (books.Count == 0)
                return false;           // no matching record found, return false
            else
            {
                BookDataSet.BookRow book = books[0];
    
                book.ISBN = isbn;
                book.Title = title;
                book.Author = author;
                book.Status = status;
                if (loaned == null) book.SetLoanedNull();
                else book.Loaned = loaned;
                if (series == null) book.SetSeriesNull();
                else book.Series = series;
                if (description == null) book.SetDescriptionNull();
                else book.Description = description;
    
                // Update book record
                int rowsAffected = Adapter.Update(book);
    
                // Return true if one row updated
                return rowsAffected == 1;
            }
        }
    
    [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Delete, true)]
        public bool DeleteBook(int Original_BookID)
        {     
            // Delete book record
            int rowsAffected = Adapter.Delete(Original_BookID);  
    
            // return true if one row deleted
            return rowsAffected == 1; 
        }
    aspx:
    Code:
    <asp:ObjectDataSource ID="odsBooks" runat="server" SelectMethod="GetBooks" InsertMethod="AddBook"                   
            TypeName="BookManager" DeleteMethod="DeleteBook" UpdateMethod="UpdateBook" 
                OldValuesParameterFormatString="original_{0}">          
           <UpdateParameters>
            <asp:Parameter Name="BookID" Type="Int32" />
                    <asp:Parameter Name="ISBN" Type="String" />
                    <asp:Parameter Name="Title" Type="String" />
                    <asp:Parameter Name="Author" Type="String" />
                    <asp:Parameter Name="Status" Type="String" />
                    <asp:Parameter Name="Loaned" Type="String" />
                    <asp:Parameter Name="Series" Type="String" />
                    <asp:Parameter Name="Description" Type="String" />
           </UpdateParameters>
           <DeleteParameters>
                    <asp:Parameter Name="BookID" Type="Int32" />
           </DeleteParameters>
        </asp:ObjectDataSource>  
    <asp:GridView ID="gvBooks" runat="server" DataKeyNames="BookID" AllowSorting="True" 
            AutoGenerateColumns="False" BackColor="#CC00FF" BorderColor="#FF0066" ForeColor="#FFCCFF" 
            BorderStyle="Double" BorderWidth="5px" CssClass="DataWebContolStyle" Font-Size="Small" 
            Font-Names="Kristen ITC"  DataSourceID="odsBooks">               
          <Columns>
               <asp:TemplateField ShowHeader="False">
                <ItemTemplate>
                <asp:LinkButton ID="btnEdit" runat="server" CausesValidation="False" 
                    CommandName="EditThis" Text="Edit" 
                    Font-Size="Small" ForeColor="#FFCCFF" Font-Bold="True" BackColor="#FF33CC" 
                    BorderColor="#FF0066" BorderStyle="Double" BorderWidth="3">
                </asp:LinkButton>
                <asp:LinkButton ID="btnDelete" runat="server" CausesValidation="False" 
                    CommandName="DeleteThis" Text="Delete" 
                    OnClientClick="return confirm('Are you sure you want to delete this book?');" 
                    Font-Size="Small" ForeColor="#FFCCFF" Font-Bold="True" BackColor="#FF33CC" 
                    BorderColor="#FF0066" BorderStyle="Double" BorderWidth="3">
                </asp:LinkButton>        
                </ItemTemplate>
              </asp:TemplateField> 
            <asp:BoundField DataField="BookID" HeaderText="BookID" ReadOnly="True" 
                InsertVisible="False" />
            <asp:BoundField DataField="ISBN" HeaderText="ISBN" />
            <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />                          
            <asp:BoundField DataField="Author" HeaderText="Author" SortExpression="Author" />                          
            <asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" />                          
            <asp:BoundField DataField="Loaned" HeaderText="Loaned" SortExpression="Loaned" />                          
            <asp:BoundField DataField="Series" HeaderText="Series" SortExpression="Series" />                          
            <asp:BoundField DataField="Description" HeaderText="Description" />                                                 
        </Columns>
    </asp:GridView>
    Any help would be much appreciated. I am new to .Net, so I am not sure if I am just missing something.

  2. #2
    Join Date
    Sep 2010
    Posts
    26

    Re: Gridview - Delete and Edit button not working

    Does anyone know???

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured