locked
ListView Command Delete deletes all the rows of the database RRS feed

  • Question

  • User1356352994 posted

    I am having an issue.  In my listview I am using the parent default commands Select, Insert, Update, Delete, Edit buttons.

    They all work. but my Delete Button works TOO good, it deletes ALL  the rows, and it is supposed to just delete the one record on the selected item row.

    My button

    <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" />
    

    My SQL DataSource

    <asp:SqlDataSource ID="ImageListSource" runat="server" 
            ConnectionString="<%$ ConnectionStrings:DataTesterConnectionString %>" 
            DeleteCommand="DELETE FROM ImagesDB"
            InsertCommand="INSERT INTO ImagesDB(thumb) VALUES (@thumb)" 
            SelectCommand="SELECT thumb FROM ImagesDB" 
            UpdateCommand="UPDATE ImagesDB SET thumb = @thumb">
            <InsertParameters>
                <asp:Parameter Name="thumb" />
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="thumb" />
            </UpdateParameters>
    <DeleteParameters>
    <asp:Parameter Name="thumb" /> <!-- and I have tried id also -->
    </DeleteParameters>
     </asp:SqlDataSource>

    Specifically the DELETE Command

    DeleteCommand="DELETE FROM ImagesDB"

    I Know that I need to have the query like this but this deletes all the records too

    DeleteCommand="DELETE FROM ImagesDB WHERE id = id"

    The database definition is the "id" is the primary key, is set to identity, and not replicated.

    Again all my other buttons work.  Though the INSERT doesn't check for nulls but I will get to that later. It works well enough for testing.


    Sunday, December 9, 2012 12:31 PM

Answers

  • User1356352994 posted

    Welp I answer myself, yet again.  To use the Parent CommandName Delete button, there are several items that must exist.  It is not necessary to give your code-behind a facelift and moonwalk backwards for something so simple. 

    Here it is.

    When you define your ListView

    <asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1"  DataKeyNames="id"
            InsertItemPosition="LastItem">
    DataKeyNames="id" <!-- is needed for the DELETE button to find the database key property-->
    InsertItemPosition="LastItem" <!-- is required for the insert button so it does not write to the beginning of your db which would overwrite any data that was there-->

    My datasource

        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:DataTesterConnectionString %>" 
            DeleteCommand="DELETE FROM ImagesDB WHERE ([id] = @id)" 
            InsertCommand="INSERT INTO ImagesDB(thumb) VALUES (@thumb)" 
            SelectCommand="SELECT ImagesDB.* FROM ImagesDB" 
            UpdateCommand="UPDATE ImagesDB SET thumb = @thumb WHERE (id = @id)">
            <InsertParameters>
                <asp:Parameter Name="id" />
                <asp:Parameter Name="thumb" />
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="thumb" />
                <asp:Parameter Name="id" />
            </UpdateParameters>
            <DeleteParameters>
                <asp:ControlParameter ControlID="ListView1" Name="id" /> <!-- ControlID is required for the DELETE command-->
            </DeleteParameters>
        </asp:SqlDataSource>

    It is great that the Developer interface creates most of the controls and WCF items you need and the SQL query builder literally write the queries for you, but they are a false sense of security.  Especially with this DELETE command. For the obvious set you can now use your parent commandname button

    <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" />

    Here also, is the cool part for you beginners, I have a file uploader in my listview also.

                <InsertItemTemplate>
                    <tr style="">
                        <td>
                            <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
                            <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
                        </td>
                        <td>&nbsp;</td><!-- my id autoicrements set to identity -->
                        <td>
                            <asp:FileUpload ID="FileUpload1" FileName='<%# Bind("thumb") %>' runat="server" />
                        </td>
                    </tr>
                </InsertItemTemplate>

    The FileUpload control automatically places a button in your list view and allows the user to select a file from the computer. When the user then clicks the "Update" button is submits the filename to the database. All without a spec of code-behind.  NOW, codebehind will be done to handle null values to the database.  THAT requires code-behind

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, December 9, 2012 2:44 PM

All replies

  • User1356352994 posted

    I am already looking at it.  What I don't understand is why all the other default commands work and the Delete command is misbehaving.  The Delete command works but erases all records.

    I followed this example and got these errors

    Error 3 'ASP.default_aspx' does not contain a definition for 'ListView1_ItemDeleted' and no extension method 'ListView1_ItemDeleted' accepting a first argument of type 'ASP.default_aspx' could be found (are you missing a using directive or an assembly reference?)
     C:\inetpub\wwwroot\DataTesters\Default.aspx 7
    
    
    
    Error	4	'ASP.default_aspx' does not contain a definition for 'ListView1_PagePropertiesChanging' and no extension method 'ListView1_PagePropertiesChanging' accepting a first argument of type 'ASP.default_aspx' could be found (are you missing a using directive or an assembly reference?)	C:\inetpub\wwwroot\DataTesters\Default.aspx	7	
    

    These methods are in fact in the code behind but the compiler is not recognizing them

    So I removed the two methods and used just the DeleteButton_onClick method, guess what.. it still deletes all the records! So I changed the query to this.  At least the scalar variable is no longer throwing an error, but it also not deleting any record.

        <asp:SqlDataSource ID="ImageListSource" runat="server" 
            ConnectionString="<%$ ConnectionStrings:DataTesterConnectionString %>" 
            DeleteCommand="DELETE FROM [ImagesDB] WHERE [id] = @id" 
            InsertCommand="INSERT INTO ImagesDB(thumb) VALUES (@thumb)" 
            SelectCommand="SELECT thumb FROM ImagesDB" 
            UpdateCommand="UPDATE ImagesDB SET thumb = @thumb">
            <InsertParameters>
                <asp:Parameter Name="thumb" />
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="thumb" />
            </UpdateParameters>
            <DeleteParameters>
                <asp:Parameter Name="id" />
            </DeleteParameters>
        </asp:SqlDataSource>




    Sunday, December 9, 2012 12:52 PM
  • User1356352994 posted

    Welp I answer myself, yet again.  To use the Parent CommandName Delete button, there are several items that must exist.  It is not necessary to give your code-behind a facelift and moonwalk backwards for something so simple. 

    Here it is.

    When you define your ListView

    <asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1"  DataKeyNames="id"
            InsertItemPosition="LastItem">
    DataKeyNames="id" <!-- is needed for the DELETE button to find the database key property-->
    InsertItemPosition="LastItem" <!-- is required for the insert button so it does not write to the beginning of your db which would overwrite any data that was there-->

    My datasource

        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:DataTesterConnectionString %>" 
            DeleteCommand="DELETE FROM ImagesDB WHERE ([id] = @id)" 
            InsertCommand="INSERT INTO ImagesDB(thumb) VALUES (@thumb)" 
            SelectCommand="SELECT ImagesDB.* FROM ImagesDB" 
            UpdateCommand="UPDATE ImagesDB SET thumb = @thumb WHERE (id = @id)">
            <InsertParameters>
                <asp:Parameter Name="id" />
                <asp:Parameter Name="thumb" />
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="thumb" />
                <asp:Parameter Name="id" />
            </UpdateParameters>
            <DeleteParameters>
                <asp:ControlParameter ControlID="ListView1" Name="id" /> <!-- ControlID is required for the DELETE command-->
            </DeleteParameters>
        </asp:SqlDataSource>

    It is great that the Developer interface creates most of the controls and WCF items you need and the SQL query builder literally write the queries for you, but they are a false sense of security.  Especially with this DELETE command. For the obvious set you can now use your parent commandname button

    <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" />

    Here also, is the cool part for you beginners, I have a file uploader in my listview also.

                <InsertItemTemplate>
                    <tr style="">
                        <td>
                            <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
                            <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
                        </td>
                        <td>&nbsp;</td><!-- my id autoicrements set to identity -->
                        <td>
                            <asp:FileUpload ID="FileUpload1" FileName='<%# Bind("thumb") %>' runat="server" />
                        </td>
                    </tr>
                </InsertItemTemplate>

    The FileUpload control automatically places a button in your list view and allows the user to select a file from the computer. When the user then clicks the "Update" button is submits the filename to the database. All without a spec of code-behind.  NOW, codebehind will be done to handle null values to the database.  THAT requires code-behind

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, December 9, 2012 2:44 PM